您的位置:首页 > 其它

AC自动机 白书模板

2016-04-21 19:00 381 查看
模板:

struct ACauto
{
int ch[maxn][26];
int sz;
int f[maxn],last[maxn],val[maxn],cnt[maxn];
void init()
{
sz=1;
memset(ch[0],0,sizeof ch[0]);
memset(cnt,0,sizeof cnt);
}
int idx(char c)
{
return c-'A';
}
void add(char *s,int v)
{
int u=0,len=strlen(s);
for(int i=0;i<len;i++)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz],0,sizeof ch[sz]);
val[sz]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]=v;
}
void getfail()
{
queue<int>q;
f[0]=0;
for(int c=0;c<26;c++)
{
int u=ch[0][c];
if(u)
{
f[u]=0;
q.push(u);
last[u]=0;
}
}
while(!q.empty())
{
int r=q.front();q.pop();
for(int c=0;c<26;c++)
{
int u=ch[r][c];
if(!u)
{
ch[r][c]=ch[f[r]][c];
continue;
}
q.push(u);
f[u]=ch[f[r]][c];
last[u]=val[f[u]]?f[u]:last[f[u]];
}
}
}
void print(int j)
{
if(j)
{
cnt[val[j]]++;
print(last[j]);
}
}
void Find(char *T)
{
int n=strlen(T);
int j=0;
for(int i=0;i<n;i++)
{
int c=idx(T[i]);
while(j&&!ch[j][c])j=f[j];
j=ch[j][c];
if(val[j])print(j);
else if(last[j])print(last[j]);
}
}
}ac;
http://acm.hdu.edu.cn/showproblem.php?pid=2222

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38991    Accepted Submission(s): 12571


[align=left]Problem Description[/align]In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
 
[align=left]Input[/align]First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
 
[align=left]Output[/align]Print how many keywords are contained in the description. 
[align=left]Sample Input[/align]
1
5
she
he
say
shr
her
yasherhs 
[align=left]Sample Output[/align]

[align=left]Author[/align]Wiskey 
[align=left]Recommend[/align]lcy 
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<string>
#include<queue>
#define maxn 250010
using namespace std;
struct ACauto
{
int ch[maxn][26];
int sz;
int f[maxn],last[maxn],val[maxn][2];
int num;
int used[maxn];
void init()
{
num=0;
sz=1;
memset(ch[0],0,sizeof(ch[0]));
memset(used,0,sizeof used);
memset(val,0,sizeof(val));
}
int idx(char c)
{
return c-'a';
}
void add(char *s,int v)
{
int u=0,len=strlen(s);
for(int i=0;i<len;i++)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz],0,sizeof(ch[sz]));
val[sz][0]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u][0]=v;//???????á??±ê??
val[u][1]++;
}
void print(int j)
{
if(j&&!used[val[j][0]])
{
used[val[j][0]]=1;
num+=val[j][1];
print(last[j]);
}
}
void getfail()
{
queue<int>q;
f[0]=0;
for(int c=0;c<26;c++)
{
int u=ch[0][c];
if(u)
{
f[u]=0;
q.push(u);
last[u]=0;
}
}
while(!q.empty())
{
int r=q.front();q.pop();
for(int c=0;c<26;c++)
{
int u=ch[r][c];
if(!u)
{
ch[r][c]=ch[f[r]][c];
continue;
}
q.push(u);
f[u]=ch[f[r]][c];
last[u]=val[f[u]]?f[u]:last[f[u]];
}
}
}
void Find(char*T)
{
int n=strlen(T);
int j=0;
for(int i=0;i<n;i++)
{
int c=idx(T[i]);
while(j&&!ch[j][c])j=f[j];
j=ch[j][c];
if(val[j])print(j);
else if(last[j])print(last[j]);
}
}
}ac;
char s[60],T[1000010];
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
ac.init();
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",s);
ac.add(s,i);
}
ac.getfail();
scanf("%s",T);
ac.Find(T);
printf("%d\n",ac.num);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: