标签:front mem ++ des sample arch -- ant sea
InputFirst line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters ‘a‘-‘z‘, and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
OutputPrint how many keywords are contained in the description.Sample Input
1 5 she he say shr her yasherhs
Sample Output
3
题意就是说给你一些模式串,问你有哪几个在目标串中出现过,(⊙o⊙)…,和洛谷那道末班体一模一样,就是多了一个多组数据。
1 #include<cstdio> 2 #include<algorithm> 3 #include<cmath> 4 #include<iostream> 5 #include<cstring> 6 #include<queue> 7 using namespace std; 8 9 int n,cnt=1; 10 char s[1000007]; 11 struct Node 12 { 13 int c[26],suf,flag; 14 void init() 15 { 16 suf=flag=0; 17 memset(c,0,sizeof(c)); 18 } 19 }tree[1000007]; 20 21 void init() 22 { 23 for (int i=0;i<=cnt;i++) 24 tree[i].init(); 25 cnt=1; 26 } 27 void Ins() 28 { 29 int head=1,l=strlen(s); 30 for (int i=0;i<l;i++) 31 { 32 int now=s[i]-‘a‘; 33 if (!tree[head].c[now]) tree[head].c[now]=++cnt; 34 head=tree[head].c[now]; 35 } 36 tree[head].flag++; 37 } 38 void Make_AC() 39 { 40 for (int i=0;i<26;i++) tree[0].c[i]=1; 41 int head=0,tail=1; 42 queue<int>q; 43 while (!q.empty()) q.pop(); 44 tree[1].suf=0; 45 q.push(1); 46 while (!q.empty()) 47 { 48 int u=q.front(); 49 q.pop(); 50 for (int i=0;i<26;i++) 51 if (tree[u].c[i]) 52 { 53 tree[tree[u].c[i]].suf=tree[tree[u].suf].c[i]; 54 q.push(tree[u].c[i]); 55 } 56 else tree[u].c[i]=tree[tree[u].suf].c[i]; 57 } 58 } 59 void Solve() 60 { 61 scanf("%s",s); 62 int ans=0,head=1,len=strlen(s); 63 for (int i=0;i<len;i++) 64 { 65 int now=s[i]-‘a‘; 66 head=tree[head].c[now]; 67 for (int j=head;j&&tree[j].flag!=-1;j=tree[j].suf) 68 ans+=tree[j].flag,tree[j].flag=-1; 69 } 70 printf("%d\n",ans); 71 } 72 int main() 73 { 74 int Cas; 75 scanf("%d",&Cas); 76 while (Cas--) 77 { 78 init(); 79 scanf("%d",&n); 80 while (n--){scanf("%s",s);Ins();} 81 Make_AC(); 82 Solve(); 83 } 84 }
标签:front mem ++ des sample arch -- ant sea
原文地址:http://www.cnblogs.com/fengzhiyuan/p/7241826.html