标签:des style color java os io strong for
1 5 she he say shr her yasherhs
3
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
#define sigma_size 26
using namespace std;
char str[55],s[1000001];
struct node
{
int count;
node *next[sigma_size];
node *fail;
};
node *root=new node;
void init(node *p)
{
memset(p->next,0,sizeof p->next);
p->count=0;
p->fail=NULL;
}
void insert(char *s)
{
int l=strlen(s);
node *p=root,*q;
for(int i=0;i<l;++i){
int c=s[i]-'a';
if(p->next[c]==NULL){
q=new node;
init(q);
p->next[c]=q;
}
p=p->next[c];
}
p->count++;
}
void get_fail()
{
node *p;
queue<node *> q;
q.push(root);
while(!q.empty()){
p=q.front();q.pop();
for(int i=0;i<sigma_size;++i){
if(p->next[i]==NULL) continue;
q.push(p->next[i]);
if(p==root){
p->next[i]->fail=root;
}
else{
node *u=p;
while(u->fail!=NULL&&u->fail->next[i]==NULL)
u=u->fail;
if(u->fail==NULL) p->next[i]->fail=root;
else p->next[i]->fail=u->fail->next[i];
}
}
}
}
int find(char *s)
{
node *p=root,*q;
int l=strlen(s),res=0;
for(int i=0;i<l;++i){
int c=s[i]-'a';
while(p!=root&&p->next[c]==NULL) p=p->fail;
if(p->next[c]==NULL) continue;
p=p->next[c];
q=p;
while(q!=root&&q->count!=-1){
res+=q->count;
q->count=-1;
q=q->fail;
}
}
return res;
}
void freedom(node *p)
{
for(int i=0;i<26;++i){
if(p->next[i]!=NULL){
freedom(p->next[i]);
}
}
delete p;
}
int main()
{
int T,n;
scanf("%d",&T) ;
while(T--){
init(root) ;
scanf("%d",&n) ;
getchar();
while(n--){
gets(str) ;
insert(str) ;
}
get_fail() ;
gets(s) ;
printf("%d\n",find(s) ) ;
for(int i = 0;i < 26;i ++){//注意root不能删除
if(root->next[i] != NULL)
freedom(root->next[i]) ;
}
}
return 0 ;
}
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<cstdio>
#define maxnode 500000+10
#define sigma_size 26
#define M 1000000+10
using namespace std;
char str[M];
struct tree
{
int f[maxnode];
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
void reset(){memset(ch[0],0,sizeof ch[0]);memset(f,0,sizeof f);memset(val,0,sizeof val);sz=1;}
int idx(char c){return c-'a';}
void insert(char *s)
{
int l=strlen(s);
int u=0;
for(int i=0;i<l;++i){
int c=idx(s[i]);
if(!ch[u][c]){
memset(ch[sz],0,sizeof ch[sz]);
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]++;
}
void get_fail()
{
queue<int> q;
for(int c=0;c<sigma_size;++c){
int u=ch[0][c];
if(u){f[u]=0;q.push(u);}
}
while(!q.empty()){
int r=q.front();q.pop();
for(int i=0;i<sigma_size;++i){
int u=ch[r][i];
if(!u) continue;
q.push(u);
int v=f[r];//指向父亲的失败指针
while(v&&!ch[v][i]) v=f[v];//父亲的失败指针的儿子不存在c,就继续沿着失败指针走
f[u]=ch[v][i];
}
}
}
int find(char *s)
{
int l=strlen(s);
int u=0,res=0;
for(int i=0;i<l;++i){
int c=idx(s[i]);
while(u&&!ch[u][c]) u=f[u];
if(!ch[u][c]) continue;//不匹配则跳过
u=ch[u][c];//匹配,那么从该节点继续匹配
int tmp=u;//temp指针,寻找较短的前缀
while(tmp&&val[tmp]!=-1){
res+=val[tmp];
val[tmp]=-1;
tmp=f[tmp];
}
}
return res;
}
}Trie;
int main()
{
int T;
cin>>T;
while(T--){
Trie.reset();
int n;
scanf("%d",&n);
while(n--){
scanf("%s",str);
Trie.insert(str);
}
Trie.get_fail();
scanf("%s",str);
printf("%d\n",Trie.find(str));
}
return 0;
}
HDU 2222——Keywords Search(AC自动机),布布扣,bubuko.com
HDU 2222——Keywords Search(AC自动机)
标签:des style color java os io strong for
原文地址:http://blog.csdn.net/u014141559/article/details/38467171