AC自动机
来源:互联网 发布:asp.net网上商城源码 编辑:程序博客网 时间:2024/05/22 13:20
/* ***********************************************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]=-1; 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(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("%d",&buf); ac.insert(buf); } ac.getfail(); scanf("%s",buf); printf("%d\n",ac.query(buf)); } return 0;}
0 0
- AC自动机...
- AC自动机
- AC 自动机
- AC自动机
- AC自动机
- ac自动机
- ac自动机
- AC自动机
- AC自动机
- AC自动机
- AC自动机
- AC自动机
- AC 自动机
- ac自动机
- AC自动机
- AC自动机
- AC自动机
- AC自动机
- 在main中写迷你DVD管理器
- lightOJ 1258 【KMP变形题】
- #python+java#同样功能的代码两种语言实现
- Android NDK 官方下载地址
- Swap Nodes in Pairs(LintCode)
- AC自动机
- 读书笔记----变量、作用域、内存问题
- 记录
- 《R语言实战》绘图代码记录
- pos设备维护的收获
- 安全盒子装B公开课: 装B架构师-K0r4dji
- Java笔记集合之Vector
- [Android基础]布局属性
- Linux中网络编程常用函数