Light OJ Substring Frequency (II)

来源:互联网 发布:js bind 参数 编辑:程序博客网 时间:2024/06/06 01:45
1427 - Substring Frequency (II)
PDF (English)StatisticsForum
Time Limit: 5 second(s)Memory Limit: 128 MB

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;}


 

原创粉丝点击