hdu 2222 Keywords Search(AC自动机)
来源:互联网 发布:网络照片女生头像大全 编辑:程序博客网 时间:2024/04/30 17:39
题意:有T组测试数据,每组测试数据给出n个]单词,然后给出一个文本,问文本里出现了多少个单词。
#include <iostream>#include <cstdio>#include <queue>#include <cstring>using namespace std;struct node{int cnt,fail,son[26];void init(){cnt=fail=0;memset(son,0,sizeof(son));}}T[500005];char str[1000005];struct AC_auto{int tot;void init(){tot=0;T[0].init();}void insert(char *str){int len=(int)strlen(str),h=0;for(int i=0;i<len;i++){int ind=str[i]-'a';if(!T[h].son[ind]){T[h].son[ind]=++tot;T[tot].init();}h=T[h].son[ind];}T[h].cnt++;}void build(){queue<int> que;for(int i=0;i<26;i++) if(T[0].son[i]) que.push(T[0].son[i]);while(que.size()){int top=que.front(); que.pop();for(int i=0;i<26;i++){if(T[top].son[i]==0) continue;que.push(T[top].son[i]);int tmp=T[top].fail;while(tmp&&!T[tmp].son[i]) tmp=T[tmp].fail;T[T[top].son[i]].fail=T[tmp].son[i];}}}int query(char *str){int len=(int)strlen(str),h=0,res=0;for(int i=0;i<len;i++){int ind=str[i]-'a';while(h&&!T[h].son[ind]) h=T[h].fail;h=T[h].son[ind];int tmp=h;while(tmp&&T[tmp].cnt!=-1){res+=T[tmp].cnt;T[tmp].cnt=-1;tmp=T[tmp].fail;}}return res;}}AC;int main(){int t;scanf("%d",&t);while(t--){int n;scanf("%d",&n);AC.init();while(n--){scanf("%s",str);AC.insert(str);}AC.build();scanf("%s",str);printf("%d\n",AC.query(str));}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 2222Keywords Search (ac自动机)
- HDU 2222 Keywords Search(AC自动机)
- HDU-2222 Keywords Search(AC自动机)
- 浏览器缓存学习笔记
- 如何优化cocos2d/x程序的内存使用和程序大小
- U-Boot for Mini2440 工作过程
- 分析函数2之(Top/Bottom N、First/Last、NTile)
- adroid动画之横向滚动文字
- hdu 2222 Keywords Search(AC自动机)
- C#常用控件
- iOS News Reader开源项目
- linux注释风格
- 二分图最大匹配
- Android 中的Context (Application Context 和Activity Context)
- 三维图形学
- 关于内存对齐的小总结
- QtCreator: ptrace operation not permitted错误解决方法