hdu 2222 Keywords Search ac自动机
来源:互联网 发布:海洋cms仿迅影网模板 编辑:程序博客网 时间:2024/06/05 19:26
/* ***********************************************Author :xryzEmail :xiang578@foxmail.comCreated Time :10-18 14:16:32File Name :ac自动机.cpp************************************************ */#include <bits/stdc++.h>using namespace std;const int N=500010;struct Trie{ int next[N][26],fail[N],last[N]; int root,cnt; int newnode() { for(int i=0;i<26;i++) next[cnt][i]=-1; last[cnt]=0; cnt++; return cnt-1; } void init() { cnt=0; root=newnode(); } void insert(char buf[]) { int len=strlen(buf); int now=root; for(int i=0;i<len;i++) { if(next[now][buf[i]-'a']==-1) next[now][buf[i]-'a']=newnode(); now=next[now][buf[i]-'a']; } last[now]++; } void getfail() { queue<int>q; fail[root]=root; for(int i=0;i<26;i++) if(next[root][i]==-1) next[root][i]=root; else { fail[next[root][i]]=root; q.push(next[root][i]); } while(!q.empty()) { int now=q.front(); q.pop(); for(int i=0;i<26;i++) if(next[now][i]==-1) next[now][i]=next[fail[now]][i]; else { fail[next[now][i]]=next[fail[now]][i]; q.push(next[now][i]); } } } int query(char buf[]) { int len=strlen(buf); int now=root; int res=0; for(int i=0;i<len;i++) { now=next[now][buf[i]-'a']; int tmp=now; while(tmp!=root) { res+=last[tmp]; last[tmp]=0; tmp=fail[tmp]; } } return res; }};char buf[1000010];Trie ac;int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int _,n; scanf("%d",&_); while(_--) { scanf("%d",&n); ac.init(); for(int i=0;i<n;i++) { scanf("%s",buf); ac.insert(buf); } ac.getfail(); scanf("%s",buf); printf("%d\n",ac.query(buf)); } return 0;}
0 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自动机)
- sharepoint 2016 学习系列篇(15)-自定义列表应用篇-(4)数据权限配置
- Binder
- Android Java连接MySQL数据库
- Android6.0新特性??
- POJ-2209
- hdu 2222 Keywords Search ac自动机
- 使用HttpURLConnection发送Post/Get请求
- JAVAIO之压缩流(zip,Gzip)
- 线性表---顺序存储结构与链式存储结构比较
- C语言——getc()和putc()
- C#中的string与stringbuilder有什么区别
- Oracle 去重复的数据
- UI - Block
- Spark 1.5.0 远程调试