AC自动机
来源:互联网 发布:用友网络 编辑:程序博客网 时间:2024/06/08 04:24
PS:http://www.cppblog.com/mythit/archive/2009/04/21/80633.html
PS:http://www.cnblogs.com/kuangbin/category/402395.html
#include<iostream>
#include<cstdio>#include<string.h>
#include<string>
#include<set>
#include<algorithm>
#include<cmath>
#include<queue>
#define ll __int64
#define MAX 1000009
const int maxnode = MAX;
const int sigma_size = 26;
using namespace std;
struct Trie
{
int ch[maxnode][sigma_size];
int val[maxnode];
int next[maxnode];
int sz,root;
int idx(char c)
{
return c - 'a';
}
int newnode()
{
for(int i = 0; i<26; i++)
ch[sz][i] = 0;
val[sz++] = 0;
return sz - 1;
}
void init()
{
sz = 0;
root = newnode();
}
void Insert(char* s)
{
int u = root;
int n = strlen(s);
for(int i = 0; i<n; i++)
{
int c = idx(s[i]);
if(!ch[u][c])
ch[u][c] = newnode();
u = ch[u][c];
}
val[u]++;
}
void build()
{
queue<int>Q;
next[root] = root;
for(int i = 0; i<26; i++)
{
if(!ch[root][i])
{
ch[root][i] = root;
}
else
{
next[ch[root][i]] = root;
Q.push(ch[root][i] );
}
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
for(int i = 0; i<26; i++)
{
if(!ch[now][i])
{
ch[now][i] = ch[next[now]][i];
}
else
{
next[ch[now][i]] = ch[next[now]][i];
Q.push(ch[now][i]);
}
}
}
}
int query(char* s)
{
int len = strlen(s);
int now = root;
int res = 0;
for(int i = 0; i<len; i++)
{
int c = idx(s[i]);
now = ch[now][c];
int temp = now;
while(temp!= root)
{
res+=val[temp];
val[temp] = 0;
temp = next[temp];
}
}
return res;
}
void debug()
{
for(int i = 0; i<sz; i++)
{
printf("id = %3d,next = %3d,val = %3d,chi = [",i,next[i],val[i]);
for(int j = 0; j < 26; j++)
printf("%2d",ch[i][j]);
printf("]\n");
}
}
};
char str[MAX];
Trie Ac;
int main()
{
int T;
int n;
scanf("%d",&n);
Ac.init();
for(int i = 0; i<n; i++)
{
scanf("%s",str);
Ac.Insert(str);
}
Ac.build();
scanf("%s",str);
// Ac.debug();
if(Ac.query(str)>0)
{
puts("YES");
}
else
{
puts("NO");
}
return 0;
}
0 0
- AC自动机...
- AC自动机
- AC 自动机
- AC自动机
- AC自动机
- ac自动机
- ac自动机
- AC自动机
- AC自动机
- AC自动机
- AC自动机
- AC自动机
- AC 自动机
- ac自动机
- AC自动机
- AC自动机
- AC自动机
- AC自动机
- Algorithms—67.Add Binary
- Leetcode46: Power of Two
- #136 Single Number
- hbase-1.0.1的60010页面无法打开
- xcode debug 2
- AC自动机
- Shell脚本编程入门(一)
- STL算法设计理念 - 预定义函数对象
- emgu在VS2010下可能出现的几种常见问题解决方案
- Struts2.之HelloWorld简单演示
- 使用 TFDConnection 的 pooled 连接池
- span中内容随着数字长度的增加而增大
- 寻找所有的素数的python实现
- 编程10大算法概念汇总