您的位置:首页 > 其它

hdu 2222 AC自动机 first

2017-05-20 23:24 375 查看
传送门


Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 62476    Accepted Submission(s): 20681


Problem Description

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.

 

Input

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.

 

Output

Print how many keywords are contained in the description.

 

Sample Input

1
5
she
he
say
shr
her
yasherhs

 

Sample Output

3

 

Author

Wiskey

 

Recommend

lcy   |   We have carefully selected several similar problems for you:  2896 3065 2243 2825 3341 

 

题意没什么说的 就是问匹配几个串。

#include <bits/stdc++.h>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define me(x) memset(x,0,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0);
#define srand() srand(time(0));
typedef long long LL;
typedef pair<int, int> pii;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
//const int dx[]={-1,0,1,0,-1,-1,1,1};
//const int dy[]={0,1,0,-1,1,-1,1,-1};
const int maxn=1e3+5;
const int maxx=3e5+100;
const int MAX_N=1000006;
const int MAX_Tot=500005;
const double EPS=1e-9;
const int MOD=1000000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
/*lch[root] = build(L1,p-1,L2+1,L2+cnt);
rch[root] = build(p+1,R1,L2+cnt+1,R2);中前*/
/*lch[root] = build(L1,p-1,L2,L2+cnt-1);
rch[root] = build(p+1,R1,L2+cnt,R2-1);中后*/
long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}

inline int Scan()
{
int res=0,ch,flag=0;
if((ch=getchar())=='-')flag=1;
else if(ch>='0' && ch<='9')res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0';
return flag ? -res : res;
}
struct Aho
{
struct state
{
int next[26];
int fail,cnt;//fail 指针 cnt 到这个地方有多少个串结束
}stateTable[MAX_Tot];
int size;//节点个数
queue<int >que;
void init()
{
while(que.size()) que.pop();
for(int i=0;i<MAX_Tot;i++)
{
me(stateTable[i].next);
stateTable[i].fail=stateTable[i].cnt=0;
}
size=1;
}
void insert(char *S)
{
int n=strlen(S);
int now=0;
for(int i=0;i<n;i++)
{
char c=S[i];
if(!stateTable[now].next[c-'a'])//没有c对应的边
stateTable[now].next[c-'a']=size++;//新开节点
now=stateTable[now].next[c-'a'];//继续往下走
}
stateTable[now].cnt++;//结束位置++
}
void build()
{
stateTable[0].fail=-1;
que.push(0);
while(que.size())
{
int u=que.front();//提出字符
que.pop();
for(int i=0;i<26;i++)//遍及儿子
{
if(stateTable[u].next[i])
{
if(u==0) stateTable[stateTable[u].next[i]].fail=0;
//如果 u是根的话 fail只能指向0
else
{
int v=stateTable[u].fail;//指向fail
while(v!=-1)//找父亲的fail
{
if(stateTable[v].next[i])
{
stateTable[stateTable[u].next[i]].fail=stateTable[v].next[i];
break;
}
v=stateTable[v].fail;
}
if(v==-1) stateTable[stateTable[u].next[i]].fail=0;
}
que.push(stateTable[u].next[i]);
}
}
}
}
int Get(int u)
{
int res=0;
while(u)
{
res+=stateTable[u].cnt;
stateTable[u].cnt=0;//这个题的问题是匹配了多少个 所以匹配一次就清0
u=stateTable[u].fail;
}
return res;
}

int match(char *S)
{
int n=strlen(S);
int res=0,now=0;
for(int i=0;i<n;i++)
{
char c=S[i];
if(stateTable[now].next[c-'a'])//如果有当前这个位置
now=stateTable[now].next[c-'a'];//就干嘛2333
else//不然就找父亲
{
int p=stateTable[now].fail;
while(p!=-1&&stateTable[p].next[c-'a']==0)
p=stateTable[p].fail;//一直找父亲
if(p==-1) now=0;
else now=stateTable[p].next[c-'a'];//不然就找到父亲的位置往下跳一位
}
if(stateTable[now].cnt)
res+=Get(now);//如果这个点还有串没被匹配,以这个点的后缀还有几个
}
return res;
}
}aho;

int T,N;
char S[MAX_N];

int main()
{
scanf("%d",&T);
while(T--)
{
aho.init();
scanf("%d",&N);
for(int i=0;i<N;i++)
{
scanf("%s",S);
aho.insert(S);
}
aho.build();
scanf("%s",S);
printf("%d\n",aho.match(S));
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: