您的位置:首页 > 其它

AC自动机专题——A - Keywords Search HDU - 2222

2017-04-15 20:59 337 查看
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. 

InputFirst 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. 

OutputPrint how many keywords are contained in the description.
Sample Input
1
5
she
he
say
shr
her
yasherhs


Sample Output
3


AC自动机模板题

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef  vector<int> vi;
typedef  long long ll;
typedef  unsigned long long  ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
const int N = 1e6+5;
const int M = 1e6+5;

int n;
struct node{
int ch[26],f,val;
}t[M];
int sz;
char s
;
void ins(char s[]){
int u=0,n=strlen(s+1);
for(int i=1;i<=n;i++){
int c=s[i]-'a';
if(!t[u].ch[c]) t[u].ch[c]=++sz;
u=t[u].ch[c];
}
t[u].val++;
}
int q
,head=1,tail=1;
void getAC(){
head=tail=1;
for(int i=0;i<26;i++)
if(t[0].ch[i]) q[tail++]=t[0].ch[i];
while(head!=tail){
int u=q[head++];
for(int i=0;i<26;i++){
int &v=t[u].ch[i];
if(!v) {v=t[t[u].f].ch[i];continue;}//meiyou ch,zhijie tiaodao fail
t[v].f=t[t[u].f].ch[i];
q[tail++]=v;
}
}
}
int ans,vis
;
void AC(char s[]){
int now=0,n=strlen(s+1);
for(int i=1;i<=n;i++){
int c=s[i]-'a';
now=t[now].ch[c];
for(int _=now;_!=0&&!vis[_];_=t[_].f)
ans+=t[_].val,vis[_]=1;
}
}
void init(){
memset(t,0,sizeof(t));
memset(vis,0,sizeof(vis));
sz=ans=0;
}
int main(){
ios::sync_with_stdio(false);
//freopen("in.txt","r",stdin);
int T;
cin>>T;
while(T--){
cin >>n;
init();
for(int i=1;i<=n;i++) cin>>s+1,ins(s);
getAC();
cin>>s+1;
AC(s);
cout<<ans<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: