您的位置:首页 > 其它

大二训练第一周 G - Keywords Search 艾斯atman

2015-11-07 14:08 423 查看
G - Keywords Search
Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d
& %I64u
Submit Status

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

 
AC奥特曼 tire+kmp
ACcode:
#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 500015
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
#define MAX 26
using namespace std;
struct Trie{
int next[maxn][MAX],fail[maxn],end[maxn];
int root,L;
int newnode(){
FOR(i,0,MAX-1)next[L][i]=-1;
end[L++]=0;
return L-1;
}
void init(){
L=0;
root=newnode();
}
void insert(char *buf){
int now=root;
for(int i=0;buf[i]!='\0';++i){
if(next[now][buf[i]-'a']==-1)
next[now][buf[i]-'a']=newnode();
now=next[now][buf[i]-'a'];
}
end[now]++;
}
void build(){
queue<int> Q;
fail[root]=root;
FOR(i,0,MAX-1)
if(next[root][i]==-1)
next[root][i]=root;
else {
fail[next[root][i]]=root;
Q.push(next[root][i]);
}
while(!Q.empty()){
int now=Q.front();
Q.pop();
FOR(i,0,MAX-1)
if(next[now][i]==-1)
next[now][i]=next[fail[now]][i];
else {
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
int query(char *buf){
int len=strlen(buf);
int now=root;
int res=0;
FOR(i,0,len-1){
now=next[now][buf[i]-'a'];
int temp=now;
while(temp!=root){
res+=end[temp];
end[temp]=0;
temp=fail[temp];
}
}
return res;
}
};
char str[1000000+100];
Trie AC_Automan;
int main(){
int loop,n;rd(loop);
while(loop--){
rd(n);AC_Automan.init();
FOR(i,1,n){
rds(str);
AC_Automan.insert(str);
}
AC_Automan.build();rds(str);
printf("%d\n",AC_Automan.query(str));
}
return 0;
}
/*
1
5
she
he
say
shr
her
yasherhs
*/


换个清晰的模板
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#define ACnodeQueue queue<int>
#define MAX 26
#define maxn 1000100
using namespace std;
struct ACnode{
ACnode *fail;
ACnode *next[MAX];
int id, val, cnt;
void reset(int _id){
id=_id;
cnt=val=0;
fail=NULL;
memset(next,0,sizeof(next));
}
bool isReceiving(){return val;}
};
class ACAutoman{
public:
ACnode *nodes[maxn];
int nodesMax;
ACnode *root;
int node_cnt;
int ID[256],IDsize;
public:
ACAutoman(){nodesMax=0;}
void init(){
node_cnt=IDsize=0;
root=getNode();
memset(ID,-1,sizeof(ID));
}
ACnode *getNode(){
if(node_cnt>=nodesMax){
nodes[nodesMax++]=new ACnode();
}
ACnode *p=nodes[node_cnt];
p->reset(node_cnt++);
return p;
}
int getCharID(unsigned char c,int needcreate){
if(!needcreate)return ID[c];
return ID[c]!=-1?ID[c]:ID[c]=IDsize++;
}
void  insert(char *str,int val){
ACnode *p=root;
int id;
for(int i=0;str[i];++i){
id=getCharID(str[i],true);
if(p->next[id]==NULL)p->next[id]=getNode();
p=p->next[id];
}
p->val|=val;
p->cnt++;
}
void bfs(){
queue<ACnode *>q;
ACnode *now,*son,*tmp;
root->fail=NULL;
q.push(root);
while(!q.empty()){
now=q.front();
q.pop();
if(now->fail){
if(now->isReceiving()){
for(int i=0;i<MAX;++i)
now->next[i]=now;
continue;
}
}
for(int i=0;i<MAX;++i){
son=now->next[i];
tmp=(now==root)?root:now->fail->next[i];
if(son==NULL)now->next[i]=tmp;
else {
son->fail=tmp;
q.push(son);
}
}
}
}
int query_str(char *str){
ACnode *p=root,*tmp=NULL;
int id;
int cnt=0;
for(int i=0;str[i];++i){
id=getCharID(str[i],false);
if(id==-1){
p=root;
continue;
}
p=p->next[id];
tmp=p;
while(tmp!=root&&tmp->cnt!=-1){
if(tmp->cnt){
cnt+=tmp->cnt;
tmp->cnt=-1;
}
tmp=tmp->fail;
}
}
return cnt;
}
}AC;
char t[maxn];
int main(){
int loop;
scanf("%d",&loop);
while(loop--){
int n;
scanf("%d",&n);
AC.init();
char tmp[55];
for(int i=0;i<n;++i){
scanf("%s",tmp);
AC.insert(tmp,0);
}
AC.bfs();
scanf("%s",t);
printf("%d\n",AC.query_str(t));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: