hihoCoder 1261 String Problem II
- 例子输入
-
5 5
dsxmlkxp
asxglqkdxp
asxgavxp
asxglp
sxglkx
kebvpyky
hjpntqft
asxglkxp
polbmzgq
jdczlmtd例子输出
描写叙述
我们有一个字符串集合S,当中有N个两两不同的字符串。还有M个询问,每一个询问都会先给出一个字符串w,你须要回答下面三个问题:
1. 输出全部S的串中。能够由w恰好加入两个字符得到的串中。编号最小的3个。
2. 输出全部S的串中,能够由w改动不超过2个字符得到的串中,编号最小的3个。
3. 输出全部S的串中,能够由w删除恰好两个字符得到的串中,编号最小的3个。
字母能够加入在包含开头结尾在内的任何位置,比方在"abc"中加入"x"和"y"。可能能够得到"yxabc","aybxc","axbyc"等等的串。
全部字符串仅仅由小写字母构成。
输入
第一行两个数N和M,表示集合S中字符串的数量和询问的数量。
接下来N行。当中第i行给出S中第i个字符串。
第i个字符串的编号就是i。
接下来M行。当中第i行给出第i个询问串。
数据范围:
N,M<=10000。
S中字符串长度和<=100000。
全部询问中字符串长度和<=100000。
输出
对每一个询问输出三行,每行三个数,分别表示每一个问题编号最小的3个串的编号。从小到大排列。
假设不到3个串,则在最后用-1补到3个输出,比方假设结果是1和2,那么输出1 2 -1。假设S中没有满足条件的串,就输出-1 -1 -1。
-1
-1 -1
-1 -1 -1
-1 -1 -1
-1 -1 -1
1 -1 -1
-1 -1 -1
2
-1 -1
1 3 -1
4 5 -1
-1
-1 -1
-1 -1 -1
-1 -1 -1
-1 -1 -1
-1 -1 -1
-1 -1 -1
解题思路:
跟上一题一样,仅仅是上一题的拓展。
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
const int maxn=100000+100;
char s[maxn];
const int Max=27;
struct node
{
int sign;
int next[Max];
} a[maxn];
set<int> ve;
int cur=0;
void insert(char *s,int flg)
{
int len,ans;
int p=0;
len=strlen(s);
for(int i=0; i<len; i++)
{
ans=s[i]-'a';
if(a[p].next[ans]!=0)
{
p=a[p].next[ans];
}
else
{
a[p].next[ans]=++cur;
a[cur].sign=0;
p=a[p].next[ans];
}
}
a[p].sign=flg;
}
int len;
void find0(int sign,int p,int x)
{
if(x==len&&sign==2&&a[p].sign)
{
ve.insert(a[p].sign);
return;
}
if(sign<2&&x<=len)
{
for(int i=0; i<26; i++)
{
if(a[p].next[i]>0)
{
int tp=a[p].next[i];
find0(sign+1,tp,x);
}
}
}
if(x<len)
{
int ne=s[x]-'a';
if(a[p].next[ne]>0)
{
int tp=a[p].next[ne];
find0(sign,tp,x+1);
}
}
}
void find1(int sign,int p,int x)
{
if(x==len&&sign<=2&&a[p].sign)
{
ve.insert(a[p].sign);
return;
}
int ne=s[x]-'a';
if(sign<2&&x<=len)
{
for(int i=0; i<26; i++)
{
if(a[p].next[i]>0&&i!=ne)
{
int tp=a[p].next[i];
find1(sign+1,tp,x+1);
}
}
}
if(x<len)
{
//int ne=s[x]-'a';
if(a[p].next[ne]>0)
{
int tp=a[p].next[ne];
find1(sign,tp,x+1);
}
}
}
void find2(int sign,int p,int x)
{
if(x+(2-sign)==len&&sign<=2&&a[p].sign)
{
ve.insert(a[p].sign);
return;
}
int ne=s[x]-'a';
if(sign<=1)
find2(sign+1,p,x+1);
if(x<len)
{
if(a[p].next[ne]>0)
{
int tp=a[p].next[ne];
find2(sign,tp,x+1);
}
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=0; i<maxn; i++)
{
cur=0;
a[i].sign=0;
memset(a[i].next,0,sizeof(a[i].next));
}
for(int i=0; i<n; i++)
{
scanf("%s",s);
insert(s,i+1);
}
for(int i=0; i<m; i++)
{
ve.clear();
scanf("%s",s);
len=strlen(s);
find0(0,0,0);
int lo=ve.size();
set<int>::iterator it;
if(lo>0)
it=ve.begin();
for(int j=0;j<3;j++)
{
if(j!=0)
printf(" ");
if(j>=lo)
printf("-1");
else
{
printf("%d",*it);
it++;
}
}
printf("\n");
ve.clear();
find1(0,0,0);
lo=ve.size();
if(lo>0)
it=ve.begin();
for(int j=0;j<3;j++)
{
if(j!=0)
printf(" ");
if(j>=lo)
printf("-1");
else
{
printf("%d",*it);
it++;
}
}
printf("\n");
ve.clear();
find2(0,0,0);
// sort(ve.begin(),ve.end());
lo=ve.size();
if(lo>0)
it=ve.begin();
for(int j=0;j<3;j++)
{
if(j!=0)
printf(" ");
if(j>=lo)
printf("-1");
else
{
printf("%d",*it);
it++;
}
}
printf("\n");
} }
return 0;
}
hihoCoder 1261 String Problem II的更多相关文章
- hihocoder #1260 : String Problem I
题目链接 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 我们有一个字符串集合S,其中有N个两两不同的字符串. 还有M个询问,每个询问给出一个字符串w,求有多少S中的 ...
- (string高精度)A + B Problem II hdu1002
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- HDU 1002 A + B Problem II
A + B Problem II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted ...
- A + B Problem II
之前总是在查阅别人的文档,看着其他人的博客,自己心里总有一份冲动,想记录一下自己学习的经历.学习算法有一段时间了,于是想从算法开始自己的博客生涯O(∩_∩)O~~ 今天在网上看了一道大数相加(高精度) ...
- nyoj 103 A + B problem II
点击打开链接 A+B Problem II 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 I have a very simple problem for you. G ...
- hdu 1023 Train Problem II
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Train Problem II Description As we all know the ...
- HDU1002 -A + B Problem II(大数a+b)
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdoj 1002 A + B Problem II
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- canconfig 移植记录
can 在Linux 里面是作为一个网络设备存在的,记录一下 canconfig 移植过程. 一. 下载 canutils 和 libsocketcan libsocketcan 地址: http:/ ...
- 关于python安全性的问题
收集总结了一下python安全方面的知识点以及近年来的相关漏洞,如果有需要修正或补充的地方,欢迎各位师傅的指出. 常见web漏洞在python中的示例. xss python下的xss其原理跟php是 ...
- iTerm2配置
1.颜色Solarized 首先下载 Solarized: $ git clone git://github.com/altercation/solarized.git Terminal/iTerm2 ...
- FastReport.Net使用:[2]添加MSSQL数据源一
如何使用MSSQL表作为数据源 1.点击FastReport设计器中Data->Add Data Source菜单项,打开数据源添加向导. 2.添加新的数据连接. 点击 New connecti ...
- [BZOj4336][BJOI2015]骑士的旅行(树链剖分+线段树)
树链剖分,对每个叶子用multiset记录前K大士兵,其余节点通过从儿子归并维护前K大士兵.过于模板. #include<set> #include<cstdio> #incl ...
- bzoj1002 生成树计数 找规律
这道题第一眼是生成树计数,n是100,是可以用O(n^3)的求基尔霍夫矩阵的n-1阶的子矩阵的行列式求解的,但是题目中并没有说取模之类的话,就不好办了. 用高精度?有分数出现. 用辗转相除的思想,让它 ...
- activemq消息重发机制[转]
大家知道,JMS规范中,Message消息头接口中有setJMSRedelivered(boolean redelivered)和getJMSRedelivered()方法,用于设置和获取消息的重发标 ...
- Debounce 和 Throttle 的原理及实现---防止频繁触发某事件
原文:http://blog.csdn.net/redtopic/article/details/69396722 在处理诸如 resize.scroll.mousemove 和 keydown/ke ...
- 如果内容很长ueditor编辑辑器怎么出现滚动条
在开发网站的时候,有的页面需要加载ueditor编辑器,如果内容很长,默认设置的时候编辑器会根据内容拉长,而不是页面出现滚动条,如果拖动页面滚条,会比较麻烦,要拖动很长才能看到提交按钮. 如何才能让编 ...
- 蓝屏代码详解(更新WIN7蓝屏代码)
6位代码含意 0 0x0000 作业完成. 1 0x0001 不正确的函数. 2 0x0002 系统找不到指定的档案. 3 0x0003 系统找不到指定的路径. 4 0x0004 系统无法开启 ...