Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 76397 Accepted Submission(s): 26376
Problem Description
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
Input
First 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.
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.
Output
Print how many keywords are contained in the description.
Sample Input
1 5 she he say shr her yasherhs
Sample Output
3
Author
Wiskey
Recommend
分析:
就是多给模板串和一个文本串进行匹配,问你进行匹配成功的次数
也可以进行多次kmp,但是效率不高
KMP加字典树就是AC自动机
还不是很理解AC自动机
套的模板
ac自动机第一题!!!
加油!
code:
#include<queue> #include<set> #include<cstdio> #include <iostream> #include<algorithm> #include<cstring> #include<cmath> using namespace std; /* ac自动机 */ struct acnode { int sum; acnode* next[26]; acnode* fail; acnode() { for(int i =0; i<26; i++) next[i]=NULL; fail= NULL; sum=0; } }; acnode *root; int cnt; acnode* newnode() { acnode *p = new acnode; for(int i =0; i<26; i++) p->next[i]=NULL; p->fail = NULL; p->sum=0; return p; } //插入函数 void Insert(char *s) { acnode *p = root; for(int i = 0; s[i]; i++) { int x = s[i] - 'a'; if(p->next[x]==NULL) { acnode *nn=newnode(); for(int j=0; j<26; j++) nn->next[j] = NULL; nn->sum = 0; nn->fail = NULL; p->next[x]=nn; } p = p->next[x]; } p->sum++; } //获取fail指针,在插入结束之后使用 void getfail() { queue<acnode*> q; for(int i = 0 ; i < 26 ; i ++ ) { if(root->next[i]!=NULL) { root->next[i]->fail = root; q.push(root->next[i]); } } while(!q.empty()) { acnode* tem = q.front(); q.pop(); for(int i = 0; i<26; i++) { if(tem->next[i]!=NULL) { acnode *p; if(tem == root) { tem->next[i]->fail = root; } else { p = tem->fail; while(p!=NULL) { if(p->next[i]!=NULL) { tem->next[i]->fail = p->next[i]; break; } p=p->fail; } if(p==NULL) tem->next[i]->fail = root; } q.push(tem->next[i]); } } } } //匹配函数 void ac_automation(char *ch) { acnode *p = root; int len = strlen(ch); for(int i = 0; i < len; i++) { int x = ch[i] - 'a'; while(p->next[x]==NULL && p != root)//没匹配到,那么就找fail指针。 p = p->fail; p = p->next[x]; if(!p) p = root; acnode *temp = p; while(temp != root) { if(temp->sum >= 0) /* 在这里已经匹配成功了,执行想执行的操作即可,怎么改看题目需求+ */ { cnt += temp->sum; temp->sum = -1; } else break; temp = temp->fail; } } } int main() { int t; int n; scanf("%d",&t); char c[1000005]; while(t--) { cnt = 0; scanf("%d",&n); root = newnode(); for(int i = 0 ; i < n; i++) { scanf("%s",c); Insert(c); } getfail(); scanf("%s",c); ac_automation(c); printf("%d\n",cnt); } return 0; }