hdu 2222 Keywords Search(AC自动机模版题)
2014-02-09 15:37
330 查看
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29897 Accepted Submission(s): 9746
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
Print how many keywords are contained in the description.
[align=left]Sample Input[/align]
1 5 she he say shr her yasherhs
[align=left]Sample Output[/align]
3
AC自动机算法详解见/article/2390636.html
AC代码:
AC自动机:
#include <iostream> #include <cmath> #include <cstdlib> #include <cstring> #include <cstdio> #include <queue> #include <stack> #include <ctime> #include <vector> #include <algorithm> #define ll __int64 #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) using namespace std; const int INF = 2000000000; const int maxn = 1000005; const int kind = 26; typedef struct tire{ int end; tire *next[kind], *fail; }tire; tire memory[maxn]; int tot; char text[maxn], word[55]; tire *create(){ tire *p; p = &memory[tot++]; for(int i = 0; i < kind; i++) p->next[i] = NULL; p->end = 0; return p; } void insert(tire *p, char *s){ while(*s) { int k = *s++ - 'a'; if(!p->next[k]) p->next[k] = create(); p = p->next[k]; } p->end++; } void build_ac_auto(tire *root){ queue<tire *>Q; Q.push(root); tire *tmp, *p; root->fail = NULL; while(!Q.empty()) { tmp = Q.front(); Q.pop(); for(int i = 0; i < kind; i++) { if(tmp->next[i]) { if(tmp == root) tmp->next[i]->fail = root; else { p = tmp->fail; while(p) { if(p->next[i]) { tmp->next[i]->fail = p->next[i]; break; } p = p->fail; } if(!p) tmp->next[i]->fail = root; } Q.push(tmp->next[i]); } } } } int query(tire *root, char *s){ int cnt = 0; tire *tmp = root, *t; while(*s) { int k = *s++ - 'a'; while(!tmp->next[k] && tmp != root) tmp = tmp->fail; tmp = tmp->next[k]; if(!tmp) tmp = root; t = tmp; while(t != root) { cnt += t->end; t->end = 0; t = t->fail; } } return cnt; } int main() { int t, n; scanf("%d", &t); while(t--) { scanf("%d", &n); tot = 0; tire *root = create(); for(int i = 0; i < n; i++) { scanf("%s", word); insert(root, word); } scanf("%s", text); build_ac_auto(root); printf("%d\n", query(root, text)); } return 0; }
tire图:
#include <iostream> #include <cmath> #include <cstdlib> #include <cstring> #include <cstdio> #include <queue> #include <stack> #include <ctime> #include <vector> #include <algorithm> #define ll __int64 #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) using namespace std; const int INF = 2000000000; const int maxn = 1000005; const int kind = 26; struct node{ int next[kind], cnt, fail; void init(){ memset(next, 0, sizeof(next)); fail = -1, cnt = 0; } }T[maxn]; int root, tot, head, tail; int que[maxn]; int n; char str[maxn]; void init(){ root = tot = 0; T[root].init(); } void insert(char *s){ int p = root, id; while(*s) { id = *s - 'a'; if(!T[p].next[id]) { T[++tot].init(); T[p].next[id] = tot; } p = T[p].next[id]; s++; } T[p].cnt++; } void build_ac_auto(){ head = tail = 0; que[tail++] = root; while(head < tail) { int u = que[head++]; for(int i = 0; i < kind; i++) { if(T[u].next[i]) { int son = T[u].next[i]; int p = T[u].fail; if(u == root) T[son].fail = root; else T[son].fail = T[p].next[i]; que[tail++] = son; } else //trie图,设定虚拟节点 { int p = T[u].fail; if(u == root) T[u].next[i] = root; else T[u].next[i] = T[p].next[i]; } } } } int query(char *s){ int p = root, cnt = 0; while(*s) { int id = *s - 'a'; p = T[p].next[id]; int tmp = p; while(tmp != root) { cnt += T[tmp].cnt; T[tmp].cnt = 0; tmp = T[tmp].fail; } s++; } return cnt; } int main() { int t; char ss[55]; scanf("%d", &t); while(t--) { scanf("%d", &n); init(); for(int i = 0; i < n; i++) { scanf("%s", ss); insert(ss); } scanf("%s", str); build_ac_auto(); printf("%d\n", query(str)); } return 0; }
相关文章推荐
- HDU2222 Keywords Search(AC自动机模版题)
- hdu 2222 Keywords Search(AC自动机入门模版题)
- HDU 2222 Keywords Search(AC自动机)
- hdu 2222 Keywords Search (AC自动机)
- HDU2222 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自动机专题——A - Keywords Search HDU - 2222
- HDU 2222 Keywords Search (AC自动机)
- HDU 2222 Keywords Search(AC自动机)
- HDU 2222 Keywords Search(AC自动机模板题)
- hdu 2222Keywords 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自动机入门 模板)