Light OJ Substring Frequency (II)
2013-04-25 23:26
155 查看
[align=center]1427 - Substring Frequency (II)[/align]
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 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).
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.
分析:AC自动机模版
PDF (English) | Statistics | Forum |
Time Limit: 5 second(s) | Memory Limit: 128 MB |
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.
|
|
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 |
#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; }
相关文章推荐
- LeetCode OJ Binary Tree Level Order Traversal II
- 南邮 OJ 1984 比赛现场气氛营造问题II
- LeetCode OJ Path Sum II
- LeetCode OJ Linked List Cycle II
- 南邮 OJ 2097 2_A+B(II)
- LeetCode OJ-5.Longest Palindromic Substring(最长回文子串)
- [后缀自动机 模板题] SPOJ 1812 Longest Common Substring II & BZOJ 2946 [Poi2000]公共串
- [LeedCode OJ]#142 Linked List Cycle II
- 【杭电oj1090】A+B for Input-Output Practice (II)
- Light-oj-1045 lightoj1045 - Digits of Factorial (N!不同进制的位数)
- [spoj1812]Longest Common Substring II && 后缀自动机
- SPOJ LCS2(Longest Common Substring II-后缀自动机向父亲更新)
- Crazy Calendar Light OJ 1393 (Nim博弈)
- 【light-oj】-1047 - Neighbor House(dp)
- 九度OJ 1342:寻找最长合法括号序列II (DP)
- 哈理工OJ 1316 移动 II (广度优先搜索+路径打印)
- HDU oj A + B Problem II
- Light OJ Integer Divisibility 【取模】
- <LeetCode OJ> 54 / 59 Spiral Matrix( I / II )
- light--oj--1116--Ekka Dokka(数学问题)