hdu 2222 Keywords Search ac自动机模板
来源:互联网 发布:北京学软件测试 编辑:程序博客网 时间:2024/06/05 12:41
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 33671 Accepted Submission(s): 10899
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
15shehesayshrheryasherhs
Sample Output
3
Author
Wiskey
Recommend
lcy
ac自动机,指针版代码
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <queue>using namespace std;const int N = 55005;int cnt = 0;struct trie{ trie *next[26],*fail,*last; int id; int cnt; trie(){ for(int i = 0; i < 26; ++i) next[i] = NULL; fail = last = 0; id = 0; cnt = 0; }} *e[N * 6];void init(){ for(int i = 0; i < cnt; i++){ delete e[i]; } cnt = 0;}class AAM{ public: trie *root; void init(){ root = e[cnt++] = new trie(); } void insert(char *s){ int len = strlen(s); trie * p = root; for(int i = 0; i < len; ++i){ int index = s[i] - 'a'; if(p -> next[index] == NULL) p -> next[index] = e[cnt++] = new trie(); p = p -> next[index]; } p -> id ++; } void getfail(){ queue<trie *>Q; trie *p; root -> last = root -> fail = root; for(int i = 0; i < 26; ++i){ if(!root -> next[i]) root -> next[i] = root; else Q.push(root -> next[i]); root -> next[i] -> fail = root; root -> next[i] -> last = root; } while(!Q.empty()){ p = Q.front(); Q.pop(); for(int i = 0; i < 26; ++i){ if(!p -> next[i]){ p -> next[i] = p -> fail -> next[i]; }else{ p -> next[i] -> fail = p -> fail -> next[i]; p -> next[i] -> last = (p -> next[i] ->fail -> id) ? p -> next[i] -> fail : p -> next[i] -> fail -> last; Q.push(p -> next[i]); } } } } int num; void count(trie *p) { if(p == root) return; if(p -> id && p -> cnt == 0){ num += p -> id; p -> cnt = 1; } count(p -> last); } int query(char *s){ num = 0; trie *p = root; int len = strlen(s); for(int i = 0; i < len; ++i){ int index = s[i] - 'a'; p = p -> next[index]; if(p -> id) count(p); else count(p -> last); } return num; }};char s[10005];int main(){ int t,n; AAM T; scanf("%d",&t); while(t--){ init();T.init(); scanf("%d",&n); while(n--) scanf("%s",s),T.insert(s); T.getfail(); scanf("%s",s); printf("%d\n",T.query(s)); } return 0;}数组版
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <queue>using namespace std;const int N = 300000;int ch[N][26];int fail[N],last[N],val[N],vis[N];class AAM{ int cnt,root;public: void init(){ cnt = 0, root = cnt++; memset(vis,0,sizeof(vis)); memset(ch[0],0,sizeof(ch[0])); memset(val,0,sizeof(val)); } void insert(char *s){ int len = strlen(s); int p = root; for(int i = 0; i < len; ++i){ int index = s[i] - 'a'; if(!ch[p][index]){ memset(ch[cnt],0,sizeof(ch[cnt])); ch[p][index] = cnt++; } p = ch[p][index]; } val[p]++; } void getfail(){ int p = root; queue<int>Q; fail[p] = last[p] = p; for(int i = 0;i < 26; ++i){ int u = ch[p][i]; if(u) {last[u] = fail[u] = p;Q.push(u);} else ch[p][i] = p; } while(!Q.empty()){ int p = Q.front(); Q.pop(); for(int i = 0; i < 26; ++i){ int &u = ch[p][i]; if(u) { fail[u] = ch[fail[p]][i]; last[u] = val[fail[u]]?fail[u]:last[fail[u]]; Q.push(u); }else u = ch[fail[p]][i]; } } } int num; void count(int p){ if(p == root) return; if(!vis[p] && val[p])num += val[p],vis[p] = 1; count(last[p]); } int query(char *s){ num = 0; int len = strlen(s); int p = root; for(int i = 0; i < len ; ++i){ int index = s[i] - 'a'; p = ch[p][index]; count(p); } return num; }};char s[1000000];int main(){ int t,n; AAM T; scanf("%d",&t); while(t--){ T.init(); scanf("%d",&n); while(n--) scanf("%s",s),T.insert(s); T.getfail(); scanf("%s",s); printf("%d\n",T.query(s)); }}
0 0
- hdu 2222 Keywords Search AC自动机 模板
- hdu 2222 Keywords Search AC自动机 模板
- hdu 2222 Keywords Search(AC自动机模板)
- HDU 2222 Keywords Search 【AC自动机模板】
- hdu 2222 Keywords Search ac自动机模板
- HDU 2222 Keywords Search(AC自动机 模板)
- HDU 2222 Keywords Search AC自动机模板
- HDU 2222 Keywords Search (AC自动机模板)
- HDU 2222 Keywords Search ac自动机模板
- HDU 2222 Keywords Search(AC自动机模板)
- HDU 2222 Keywords Search AC自动机 可做模板
- hdu 2222:Keywords Search(AC自动机模板)
- HDU 2222 Keywords Search(AC自动机模板题)
- hdu 2222 Keywords Search(AC自动机模板题)
- AC自动机学习小记 Hdu 2222 Keywords Search (模板)
- HDU 2222 Keywords Search (AC自动机模板题)
- hdu 2222 Keywords Search(AC自动机模板题)
- HDU 2222 Keywords Search AC自动机模板题
- request和response中一些方法的区别
- 【精】适配器模式装饰模式
- hdu1026
- Angular学习(八)——List、Table和其他重复元素
- Drupal7 集成 Solr4.4模块
- hdu 2222 Keywords Search ac自动机模板
- html5 学习之路 二(canvas)
- 最小生成树
- 可变参数学习笔记 转载
- [ZOJ 3353] Chess Board [搜索+状态压缩]
- 二叉查找树
- 射频电路设计实录第二章,产品技术指标
- 拓扑排序
- DOM JDOM DOM4J