HDU 2896 AC自动机
2012-02-11 14:26
281 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2896
典型的模板题,用了2个模板,有一个就是不过,卡了我快一天了TvT,超级郁闷,改用另一个模板就ac了。两个模板的差别就是队列一个用STL,一个数组自己模拟。结果STL的过了,数组的就是过不了~~纠结郁闷,下次遇到还是果断STL算了~~
典型的模板题,用了2个模板,有一个就是不过,卡了我快一天了TvT,超级郁闷,改用另一个模板就ac了。两个模板的差别就是队列一个用STL,一个数组自己模拟。结果STL的过了,数组的就是过不了~~纠结郁闷,下次遇到还是果断STL算了~~
#include <stdio.h> #include <string.h> #include <math.h> #include <vector> #include <algorithm> #include <queue> using namespace std; #define KIND 128 #define M 10010 struct node { node *fail; node *next[KIND]; int id; node () { fail = NULL; id = 0; memset(next, 0, sizeof(next)); } }; char ch[M]; queue<node *> q; vector<int> g; int vis[M]; void insert(node *&root, char *ch, int id) { node *p = root; int i = 0, t; while(ch[i]) { t = ch[i] - 31; if(!p->next[t]) p->next[t] = new node(); p = p->next[t]; i++; } p->id = id; } void AC(node *&root) { q.push(root); while(!q.empty()) { node *p = NULL; node *t = q.front(); q.pop(); for(int i = 0; i < KIND; i++) { if(t->next[i]) { p = t->fail; while(p) { if(p->next[i]) { t->next[i]->fail = p->next[i]; break; } p = p->fail; } if(!p) t->next[i]->fail = root; q.push(t->next[i]); } } } } bool query(node *&root, char *ch,int count) { g.clear(); int i = 0, t, top = 0; bool flag = false; node *p = root, *tmp; while(ch[i]) { t = ch[i] - 31; while(!p->next[t] && p != root) p = p->fail; p = p->next[t]; if(!p) p = root; tmp = p; while(tmp != root && tmp->id ) { flag = true; if(!vis[tmp->id]) { g.push_back(tmp->id); vis[tmp->id]=1; } tmp = tmp->fail; } i++; } if(!flag) return false; sort(g.begin(), g.end()); printf("web %d:", count); for(int i = 0; i < g.size(); i++) { printf(" %d", g[i]); } printf("\n"); return true; } int main() { int n, total; while(~scanf("%d", &n)) { node *root = new node(); total = 0; for(int i = 0; i < n; i++) { scanf("%s", ch); insert(root, ch, i + 1); } AC(root); scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%s", ch); memset(vis,0,sizeof(vis)); if(query(root, ch,i+1)) total++; } printf("total: %d\n", total); } return 0; }
相关文章推荐
- hdu 2896 病毒侵袭 (AC自动机模版)
- HDU 2896 病毒侵袭 【AC自动机】
- hdu 2896 ac自动机
- HDU 2896 病毒侵袭 AC自动机
- hdu 2896 病毒侵袭 AC自动机基础
- hdu 2896 病毒侵袭 -- AC自动机
- hdu 2896 ac自动机(last指针)
- HDU 2896 病毒侵袭 AC自动机
- HDu-2896 病毒侵袭,AC自动机模板题!
- HDU 2896 病毒侵袭 (AC自动机模板)
- HDU 2896 病毒侵袭 AC自动机
- [HDU 2896] 病毒侵袭 AC自动机
- HDU-2896-AC自动机
- hdu 2896 病毒侵袭(AC自动机,3级)
- hdu 2896 病毒侵袭 AC自动机入门题
- hdu 2896 病毒入侵 AC自动机
- HDU 2896 AC自动机
- HDU 2896 病毒侵袭 AC自动机题解
- hdu 2896 病毒侵袭(AC自动机,3级)
- HDU 2896 AC自动机 模式串匹配