标签:node sys ant struct wan time amp arc 经历
1 5 she he say shr her yasherhsSample Output
3
题目就是给定一些串,让你在一个字符串中搜出现几个串。这就是ac自动机的基本模板。
注意事项:memset是非常慢的,尽量少用,注意节点的跳转,就是我代码中的u节点,注意scanf printf 而不是cin cout
ps:动态开节点,动态初始化,注意是动态。
惨痛的经历,刷了一面半的wa tle re
加!!为犯错误的地方,引起重视。
#pragma GCC optimize("O2")
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#define maxnode 501000//!!
using namespace std;
struct Trie
{
int ch[maxnode][26],val[maxnode],fail[maxnode];
int sz=0,root=0;
int newnode()//!!
{
for(int i=0;i<26;i++)
ch[sz][i]=-1;
val[sz++]=0;
return sz-1;
}
void clear(){sz=0;root=newnode();}//!!
int idx(char s){return s-‘a‘;}
void insert(char* s)
{
int u=0,len=strlen(s);
for(int i=0;i<len;i++)
{
int v=idx(s[i]);
if(ch[u][v]==-1)
ch[u][v]=newnode();//!!
u=ch[u][v];
}
val[u]++;
}
void build()
{
queue<int> q;
fail[root]=root;
for(int i=0;i<26;i++)
{
if(ch[root][i]==-1)//!!
ch[root][i]=root;
else
{
fail[ch[root][i]]=root;
q.push(ch[root][i]);
}
}
while(!q.empty())
{
int u=q.front();q.pop();//!!
for(int i=0;i<26;i++)
{
if(ch[u][i]==-1)
ch[u][i]=ch[fail[u]][i];//!!
else
{
fail[ch[u][i]]=ch[fail[u]][i];//!!
q.push(ch[u][i]);
}
}
}
}
int query(char* s)
{
int u=root,len=strlen(s);
int ans=0;
for(int i=0;i<len;i++)//!!
{
int v=idx(s[i]);
u=ch[u][v];//!!
int tmp=u;
while(tmp!=root)
{
ans+=val[tmp];
val[tmp]=0;//!!
tmp=fail[tmp];
}
}
return ans;
}
};
Trie ac;
char str[maxnode*2];//!!
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
ac.clear();//!!
while(n--)
{
scanf("%s",str);
ac.insert(str);
}
ac.build();
scanf("%s",str);
printf("%d\n",ac.query(str));
}
}
标签:node sys ant struct wan time amp arc 经历
原文地址:http://www.cnblogs.com/foreverpiano/p/7082771.html