AC自动机模板
来源:互联网 发布:威海商友网络 编辑:程序博客网 时间:2024/05/21 09:12
用 HDU2222 这题作为模板
#include <bits/stdc++.h>using namespace std;struct ACautoman{ int endsum; ACautoman *next[26]; ACautoman *fail; ACautoman (){ endsum = 0; fail = NULL; memset(next,NULL,sizeof(next)); }}*root;void CreatTrie(char *str){ ACautoman *p = root; int len = strlen(str); for(int i=0; i<len; i++){ if(!p -> next[ str[i]-'a' ]) p -> next[ str[i]-'a' ] = new ACautoman; p = p -> next[ str[i]-'a' ]; } ++(p -> endsum);}void CreatFail(){ queue <ACautoman *> Q; Q.push(root); while(!Q.empty()){ ACautoman *p = Q.front(); Q.pop(); for(int i=0; i<26; i++){ if(p -> next[i]){ if(p == root) p -> next[i] -> fail = root; else{ ACautoman *now = p; while(now -> fail){ if(now -> fail -> next[i]){ p -> next[i] -> fail = now -> fail -> next[i]; break; } now = now -> fail; } if(!now -> fail) p -> next[i] -> fail = root; } Q.push(p -> next[i]); } } }}int Search(char *str){ int ans = 0; int len = strlen(str); ACautoman *p = root; for(int i=0; i<len; i++){ while(p != root && !p -> next[ str[i]-'a' ]) p = p -> fail; if( p -> next[ str[i]-'a' ] ){ p = p -> next[ str[i]-'a' ]; ACautoman *now = p; while(now != root ){ if(now -> endsum){ ans += now -> endsum; now -> endsum = 0; } now = now -> fail; } } } return ans;}const int maxn = 1000005;char text[maxn];int main(){ ios::sync_with_stdio(false); int T; cin>>T; while(T--){ root = new ACautoman; int N; cin>>N; char word[55]; for(int i=0; i<N; i++){ cin>>word; CreatTrie(word); } cin>>text; CreatFail(); cout<< Search(text) <<endl; } return 0;}
1 0
- 【AC自动机】AC自动机模板
- AC自动机模板
- AC自动机模板
- AC自动机模板
- AC自动机模板
- AC自动机模板
- AC自动机模板
- AC自动机模板 hdu2222
- AC自动机模板
- AC自动机模板
- 数据结构--AC自动机--模板
- ac自动机模板
- AC自动机模板
- AC自动机模板
- hdu2222 ac自动机模板
- ac自动机模板。。。。。
- AC自动机模板 LA4670
- AC自动机模板
- Get raw DOM by AngularJS [Not finished]
- 亲,没有更多数据啦!
- BlockManager
- 大师课徒 - 笔记
- ExternalBlockStore
- AC自动机模板
- processing绘制图形的基本属性
- 调用有参构造函数的三种方法
- session的基本用法--留给自己看
- A - Inversion 归并排序求逆序数
- C 语言字符数组的定义与初始化
- 配置php使其支持段标记<? ?>
- 关于ubuntu16.04拨号上网以及无线驱动安装方法
- 跑马灯