HDU 2222 Keywords Search(AC自动机)
来源:互联网 发布:php 读取图片 编辑:程序博客网 时间:2024/05/23 20:24
第一道AC自动机留念,HDU第500题留念。
学习了Just Me牛的代码风格。
#include <cstdio>#include <queue>#include <cstdlib>using namespace std;const int maxn=1e6+5;char s[maxn];struct Trie{ int count; Trie *fail, *next[26]; Trie() { count=0; for(int i=0 ; i<26 ; i++ ) next[i] = NULL; fail = NULL; }};Trie *root;void insert(char *s){ Trie *p = root; for(int i = 0 ; s[i] ; i++) { int id = s[i]-'a'; if(p -> next[id] == NULL) p -> next[id]= new Trie(); p = p -> next[id]; } p->count++;}void Build_AC(){ queue<Trie *>q; q.push(root); while(!q.empty()) { Trie *p = q.front(); q.pop(); for(int i = 0; i < 26 ;i++) if(p -> next[i] != NULL){ if(p == root) p ->next[i] ->fail = root; else { Trie *temp = p ->fail; while(temp != NULL) { if(temp -> next[i] != NULL){ p ->next[i] ->fail = temp ->next[i]; break; } temp=temp ->fail; } if(temp == NULL) p ->next[i] ->fail = root; } q.push(p ->next[i]); } }}int AC(char *s){ Build_AC(); Trie *p=root; int ret=0,index; for(int i = 0; s[i] ; i++ ){ index = s[i]-'a'; while(p != root && p -> next[index] == NULL) p = p -> fail; p = p -> next[index]; if( p == NULL) { p = root; continue; } Trie *temp=p; while(temp != root && temp -> count != 0) { ret += temp -> count; temp -> count = 0 ; temp = temp -> fail; } } return ret;}void deal(Trie *T){ for(int i = 0 ; i<26 ; i++) if(T->next[i] != NULL) deal(T->next[i]); free(T);}int main(){ int T,n; scanf("%d", &T); while( T-- ) { scanf("%d",&n); root=new Trie(); getchar(); while(n--){ scanf("%s",s); insert(s); } scanf("%s",s); printf("%d\n",AC(s));
deal(root); } return 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自动机)
- hdu - 2222 - Keywords Search(AC自动机)
- [HDU 2222]Keywords Search[AC自动机]
- hdu 2222 Keywords Search----AC自动机
- hdu 2222 Keywords Search(AC自动机)
- IE 10下动态添加html 无法绑定事件
- PDF全屏快捷键
- 25个增强iOS应用程序性能的提示和技巧
- Java中数据库连接池基本工作原理的详细讲解
- python 列表函数
- HDU 2222 Keywords Search(AC自动机)
- day1 test03
- mysql中用关键字作为列名
- 学习UML实现、泛化、依赖、关联、聚合、组合
- BT5-R3开启SSH服务
- Lecroy 813Zi-A User Note For USB 3.0
- uva 10127 - Ones
- C++ string::npos
- *.gwt.xml