自己写的模板,请大家指错...Trie树模板用hdu1075试了,AC自动机用hdu2222试了。
下面是动态的Trie树和AC自动机 (静态的放假再补上,顺便复习...)
Trie树:
//Trie树的简单实现(动态)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxc 26 //26小写英文字符trie树
struct node
{
bool is;
char ch;
node* child[maxc];
};
node* ROOT;
int CtoI(char c) //将字符转换成下标
{
return c-'a';
}
void Init()
{
int i;
ROOT=new node();
ROOT->is=0;
ROOT->ch=' ';
for(i=0;i<maxc;++i)
ROOT->child[i]=NULL;
}
node* CreateNode(char c)
{
int i;
node *t=new node();
t->is=false;
t->ch=c;
for(i=0;i<maxc;++i)
t->child[i]=NULL;
return t;
}
void Insert(char chars[],int len)
{
int i,index;
node* t=ROOT;
char c;
for(i=0;i<len;++i)
{
c=chars[i];
index=CtoI(c);
if(t->child[index]==NULL)
t->child[index]=CreateNode(c);
t=t->child[index];
}
t->is=true;
}
bool Find(char chars[],int len)
{
int i,index;
node* t=ROOT;
char c;
for(i=0;i<len;++i)
{
c=chars[i];
index=CtoI(c);
if(t->child[index]==NULL)
break;
t=t->child[index];
}
if(i==len&&t->is==true)
return true;
else
return false;
}
void DeleteTrie(node *t)
{
int i;
for(i=0;i<maxc;++i)
{
if(t->child[i]!=NULL)
DeleteTrie(t->child[i]);
}
delete t;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxc 26
struct Node
{
bool is;
char ch;
Node *child[maxc];
Node ()
{
is=false;
ch=' ';
for(int i=0;i<maxc;++i)
child[i]=NULL;
}
Node (char c)
{
is=false;
ch=c;
for(int i=0;i<maxc;++i)
child[i]=NULL;
}
};
Node *Root;
void Init()
{
Root=new Node();
}
void Insert(char chs[])
{
int i,id,len=strlen(chs);
Node *t=Root;
char c;
for(i=0;i<len;++i)
{
c=chs[i];
id=c-'a';
if(t->child[id]==NULL)
t->child[id]=new Node(c);
t=t->child[id];
}
t->is=true;
}
bool Find(char chs[])
{
int i,id,len=strlen(chs);
Node *t=Root;
char c;
for(i=0;i<len;++i)
{
c=chs[i];
id=c-'a';
if(t->child[id]==NULL)
break;
t=t->child[id];
}
if(i==len&&t->is==true)
return true;
else
return false;
}
void DeleteTrie(Node *t)
{
int i;
for(i=0;i<maxc;++i)
{
if(t->child[i]!=NULL)
DeleteTrie(t->child[i]);
}
delete t;
}
AC自动机:
//Aho-Corasick automation(动态)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
#define maxc 26
struct node
{
char ch;
bool IsStr; //int count
node *child[maxc];
node *fail;
};
node *ROOT;
int CtoI(char c)
{
return c-'a';
}
void Init()
{
int i;
ROOT=new node();
ROOT->ch=' ';
ROOT->IsStr=false;
for(i=0;i<maxc;++i)
ROOT->child[i]=NULL;
ROOT->fail=NULL;
}
node* CreateNode(char c)
{
int i;
node *t=new node();
t->ch=c;
t->IsStr=false;
for(i=0;i<maxc;++i)
t->child[i]=NULL;
t->fail=NULL;
return t;
}
void Insert(char chars[])
{
int i,index,len=strlen(chars);
char c;
node *t=ROOT;
for(i=0;i<len;++i)
{
c=chars[i];
index=CtoI(c);
if(t->child[index]==NULL)
t->child[index]=CreateNode(c);
t=t->child[index];
}
t->IsStr=true;
}
void DeleteTrie(node *t)
{
int i;
for(i=0;i<maxc;++i)
{
if(t->child[i]!=NULL)
DeleteTrie(t->child[i]);
}
delete t;
}
void PreAC()
{
queue<node*> q;
q.push(ROOT);
int i;
node *p,*t;
while(!q.empty())
{
p=q.front();
q.pop();
for(i=0;i<maxc;++i)
{
if(p->child[i]!=NULL)
{
t=p->fail;
while(t!=NULL&&t->child[i]==NULL)
t=t->fail;
if(t==NULL)
p->child[i]->fail=ROOT;
else
p->child[i]->fail=t->child[i];
q.push(p->child[i]);
}
}
}
}
int Find(char* T)
{
int i,index,ans=0,len=strlen(T);
node *p,*t;
p=ROOT;
char c;
for(i=0;i<len;++i)
{
c=T[i];
index=CtoI(c);
while(p!=NULL&&p->child[index]==NULL)
p=p->fail;
if(p==NULL)
p=ROOT;
else
{
p=p->child[index];
t=p;
while(t!=NULL) //t!=NULL&&count!=-1
{
if(t->IsStr)
ans++;
t=t->fail;
}
}
}
return ans;
}
附hdu1075,hdu2222代码:
1075:
//Trie树的简单实现(动态)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxc 26 //26小写英文字符trie树
struct node
{
bool is;
char *value;
node* child[maxc];
};
node* ROOT;
int CtoI(char c) //将字符转换成下标
{
return c-'a';
}
void Init()
{
int i;
ROOT=new node();
ROOT->is=0;
ROOT->value=NULL;
for(i=0;i<maxc;++i)
ROOT->child[i]=NULL;
}
node* CreateNode()
{
int i;
node *t=new node();
t->is=false;
t->value=NULL;
for(i=0;i<maxc;++i)
t->child[i]=NULL;
return t;
}
void Insert(char english[],char martin[])
{
int i,index;
node* t=ROOT;
char c;
for(i=0;i<strlen(martin);++i)
{
c=martin[i];
index=CtoI(c);
if(t->child[index]==NULL)
t->child[index]=CreateNode();
t=t->child[index];
}
t->is=true;
t->value=new char[strlen(english)+1];
strcpy(t->value,english);
}
const char* Find(char martin[])
{
int i,index;
node* t=ROOT;
char c;
for(i=0;i<strlen(martin);++i)
{
c=martin[i];
index=CtoI(c);
if(t->child[index]==NULL)
break;
t=t->child[index];
}
if(i==strlen(martin)&&t->is==true)
{
return t->value;
}
else
return NULL;
}
void DeleteTrie(node *t)
{
int i;
for(i=0;i<maxc;++i)
{
if(t->child[i]!=NULL)
DeleteTrie(t->child[i]);
}
delete t;
}
int main()
{
Init();
char english[15],martin[15];
int cnt;
char ch;
const char *t;
scanf("%s",english);
getchar();
cnt=0;
while(scanf("%s",english)&&english[0]!='E')
{
scanf("%s",martin);
Insert(english,martin);
}
scanf("%s",english);
getchar();
while((ch=getchar())!='E')
{
if(ch>='a'&&ch<='z')
martin[cnt++]=ch;
else
{
if(cnt)
{
martin[cnt]='\0';
t=Find(martin);
if(t!=NULL)
printf("%s",t);
else
printf("%s",martin);
}
cnt=0;
putchar(ch);
}
}
getchar();
getchar();
return 0;
}
2222:
//Aho-Corasick automation(动态)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
#define maxc 26
struct node
{
char ch;
int IsStr;
node *child[maxc];
node *fail;
}*q[500001];
node *ROOT;
int CtoI(char c)
{
return c-'a';
}
void Init()
{
int i;
ROOT=new node();
ROOT->ch=' ';
ROOT->IsStr=0;
for(i=0;i<maxc;++i)
ROOT->child[i]=NULL;
ROOT->fail=NULL;
}
node* CreateNode(char c)
{
int i;
node *t=new node();
t->ch=c;
t->IsStr=0;
for(i=0;i<maxc;++i)
t->child[i]=NULL;
t->fail=NULL;
return t;
}
void Insert(char chars[])
{
int i,index,l=strlen(chars);
char c;
node *t=ROOT;
for(i=0;i<l;++i)
{
c=chars[i];
index=CtoI(c);
if(t->child[index]==NULL)
t->child[index]=CreateNode(c);
t=t->child[index];
}
t->IsStr++;
}
void DeleteTrie(node *t)
{
int i;
for(i=0;i<maxc;++i)
{
if(t->child[i]!=NULL)
DeleteTrie(t->child[i]);
}
delete t;
}
void PreAC()
{
// queue<node*> q;
int head=0,tail=0;
// q.push(ROOT);
q[tail++]=ROOT;
int i;
node *p,*t;
while(head!=tail)
{
// p=q.front();
p=q[head++];
// q.pop();
for(i=0;i<maxc;++i)
{
if(p->child[i]!=NULL)
{
t=p->fail;
while(t!=NULL&&t->child[i]==NULL)
t=t->fail;
if(t==NULL)
p->child[i]->fail=ROOT;
else
p->child[i]->fail=t->child[i];
// q.push(p->child[i]);
q[tail++]=p->child[i];
}
}
}
}
int Find(char* T)
{
int i,index,ans=0,l=strlen(T);
node *p,*t;
p=ROOT;
char c;
for(i=0;i<l;++i)
{
c=T[i];
index=CtoI(c);
while(p!=NULL&&p->child[index]==NULL)
p=p->fail;
if(p==NULL)
p=ROOT;
else
{
p=p->child[index];
t=p;
while(t!=NULL&&t->IsStr!=-1)
{
ans+=t->IsStr;
t->IsStr=-1;
t=t->fail;
}
}
}
return ans;
}
int main()
{
char T[1000005],K[55];
int C,N,ans;
scanf("%d",&C);
while(C--)
{
scanf("%d",&N);
Init();
while(N--)
{
scanf("%s",K);
Insert(K);
}
PreAC();
scanf("%s",T);
ans=Find(T);
printf("%d\n",ans);
DeleteTrie(ROOT);
}
// system("pause");
return 0;
}