Light OJ Substring Frequency (II)
来源:互联网 发布:js bind 参数 编辑:程序博客网 时间:2024/06/06 01:45
A string is a finite sequence of symbols that are chosen from an alphabet. In this problem you are given a string T and n queries each with a string Pi, where the strings contain lower case English alphabets only. You have to find the number of times Pi occurs as a substring of T.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 500). The next line contains the string T (1 ≤ |T| ≤ 106). Each of the next n lines contains a string Pi (1 ≤ |Pi| ≤ 500).
Output
For each case, print the case number in a single line first. Then for each string Pi, report the number of times it occurs as a substring of T in a single line.
Sample Input
Output for Sample Input
2
5
ababacbabc
aba
ba
ac
a
abc
3
lightoj
oj
light
lit
Case 1:
2
3
1
4
1
Case 2:
1
1
0
分析:AC自动机模版
#include<cstdio>#include<queue>#include<cstring>using namespace std;char str[1000002];char s[502][502];int tot,cnt;struct trie{int flag,fail,next[26],index;void init(){flag=0;fail=index=-1;for(int i=0;i<26;i++)next[i]=0;}}q[500002];void insert(char *S){int t=0,i=0,c;while(S[i]){c=S[i]-'a';if(!q[t].next[c]){q[++tot].init();q[t].next[c]=tot;}t=q[t].next[c];i++;}q[t].flag=1;q[t].index=cnt;}void get_fail(){int i;queue<int> Q;Q.push(0);while(!Q.empty()){int p=Q.front();Q.pop();for(i=0;i<26;i++){if(q[p].next[i]){int a=q[p].fail,b=q[p].next[i];while(a!=-1&&!q[a].next[i]) a=q[a].fail;if(a==-1) q[b].fail=0;else q[b].fail=q[a].next[i];Q.push(b);}}}}int ans[502];void match(){int c,i=0,p,t=0;while(str[i]){c=str[i]-'a';p=t;while(p!=-1&&!q[p].next[c]) p=q[p].fail;if(p==-1) t=0;else t=q[p].next[c];p=t;while(p!=0){ans[q[p].index]+=q[p].flag;//q[p].flag=0;p=q[p].fail;}i++;}int j;for(i=0;i<cnt;i++){if(ans[i]>0)printf("%d\n",ans[i]);else {for(j=0;j<cnt;j++)if(i!=j&&strcmp(s[i],s[j])==0&&ans[j]>0) {printf("%d\n",ans[j]);break;}if(j==cnt)puts("0");}}}int main(){int t,n,ca;scanf("%d",&t);for(ca=1;ca<=t;ca++){scanf("%d",&n);scanf("%s",str);tot=0;q[0].init();cnt=0;while(n--){scanf("%s",s[cnt]);insert(s[cnt]);cnt++;}printf("Case %d:\n",ca);get_fail();memset(ans,0,sizeof(ans));match();}return 0;}
- Light OJ Substring Frequency (II)
- light oj 1255 - Substring Frequency (KMP)
- lightoj 1427 Substring Frequency (II) (ac自动机)
- LightOJ 题目1427 - Substring Frequency (II)(AC自动机)
- LightOJ 1427 Substring Frequency (II)(AC自动机裸)
- LightOJ 1427 Substring Frequency (II)(ac自动机水题)
- LightOJ - 1427 - Substring Frequency (II)(AC自动机)
- Light oj 1245 - Harmonic Number (II)
- Light OJ 1090 - Trailing Zeroes (II)
- [Light oj] 1245 - Harmonic Number (II)
- light oj 1090 Trailing Zeroes (II)
- Light OJ 1245 - Harmonic Number (II) 【规律】
- light oj 1245 - Harmonic Number (II)【规律】
- Light OJ 1429 - Assassin`s Creed (II)
- Light OJ 1193 Dice (II) (DP)
- Light OJ 1232 Coin Change (II)
- Light OJ 1270 Tiles (II) (状压DP)
- lightoj-1427:Substring Frequency
- Ueditor入门小结
- 解决ExtJS在firefox里字体显示偏小问题
- C#实现打印功能
- Android的framework层音量控制原理分析
- ZOJ 3631 Watashi's BG(DFS)
- Light OJ Substring Frequency (II)
- 学习Linux下的汇编语言
- ATTILAX项目进展表VC425
- Shell编程初接触
- 测 T 试
- java学习笔记---理解内部类
- 设计模式之"策略模式"
- Joomla Strict Standards: Non-static method JLoader::import() 解决方法
- 篮球英文术语翻译与解释 (以A至E为限)