HDU2222 Keywords Search ac自动机第一题

 指针我一般都会出错,所以还是自己写数组版本。

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

 第一版:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int maxn=1000010;
int Next[maxn][26],End[maxn],fail[maxn];
int cnt,root=1,ans;//root不能为0 
void _init()
{
     memset(Next,0,sizeof(Next));
     memset(End,0,sizeof(End));
     memset(fail,0,sizeof(fail));
     ans=0;cnt=1;
}
void _insert(char s[])
{
    int L=strlen(s);
    int now=root;
    for(int i=0;i<L;i++){
        if(!Next[now][s[i]-'a']) Next[now][s[i]-'a']=++cnt;
        now=Next[now][s[i]-'a'];
    }
    End[now]++;
}
void _build()//bfs 
{
    queue<int>q;
    q.push(root);
    while(!q.empty()){
        int Now=q.front();q.pop();
        for(int i=0;i<26;i++){
            if(Next[Now][i]){
                if(Now==root) fail[Next[Now][i]]=root;
                else{
                    int p=fail[Now];
                    while(p){//给儿子们找对象 
                        if(Next[p][i]){
                            fail[Next[Now][i]]=Next[p][i];
                            break;//找到了就停止 
                        }
                        p=fail[p];
                    }
                    if(!p) fail[Next[Now][i]]=root;//不晓得? 
                }
                q.push(Next[Now][i]);
            }
        }    
    }
}
void _query(char s[])
{
    int L=strlen(s);
    int Now=root;
    for(int i=0;i<L;i++){
          int x=s[i]-'a';
          while(Now!=root&&!Next[Now][x]) Now=fail[Now];
          Now=Next[Now][x];
          if(!Now) Now=root;//不晓得? 
          int tmp=Now;
          while(tmp!=root){
                if(End[tmp]>=0){
                   ans+=End[tmp];
                   End[tmp]=-1;//避免重复 
                }
                else break;
                tmp=fail[tmp];
          }
    }
}
int main()
{
    char s[55];
    char c[maxn] ;
    int n,j,i,T;
    scanf("%d",&T);
    while(T--){
        
        _init();
        scanf("%d",&n);
        for(i=1;i<=n;i++){
           scanf("%s",s);
           _insert(s);//单词 
        }
        scanf("%s",c);
        
        _build();
        _query(c);//文章 
    
        printf("%d
",ans);
   
    } 
    return 0;
}
View Code

稍微改进版:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int maxn=1000010;
int Next[maxn][26],End[maxn],fail[maxn];
int cnt,root=0,ans;
int q[10000000],tail,head;
void _init()
{
    //不要memset只要200ms 
     ans=0;cnt=0;fail[root]=-1;//root的fail不能等于本身,不然不能重新开始。 
     head=tail=0;End[root]=0;
     for(int i=0;i<26;i++) Next[root][i]=0;
}
void _insert(char s[])
{
    int now=root;
    for(int i=0;s[i];i++){
        if(!Next[now][s[i]-'a']) { 
           Next[now][s[i]-'a']=++cnt;
           fail[cnt]=0;
           End[cnt]=0;
           for(int i=0;i<26;i++) Next[cnt][i]=0;
        }
        now=Next[now][s[i]-'a'];
    }
    End[now]++;
}
void _build()//bfs 
{
    q[++head]=root;
    while(tail<head){
        int Now=q[++tail];
        for(int i=0;i<26;i++){
            if(Next[Now][i]){
                if(Now==root) fail[Next[Now][i]]=root;
                else{
                    int p=fail[Now];
                    while(p!=-1){//给儿子们找对象 
                        if(Next[p][i]){
                            fail[Next[Now][i]]=Next[p][i];
                            break;//找到了就停止 
                        }
                        p=fail[p];//配对 
                    }
                    if(p==-1) {
                       fail[Next[Now][i]]=root;//重新开始 
                    }
                }
                q[++head]=Next[Now][i];
            }
        }    
    }
}
void _query(char s[])
{
    int L=strlen(s);
    int Now=root;
    for(int i=0;i<L;i++){
          int x=s[i]-'a';
          while(Now!=root&&!Next[Now][x]) Now=fail[Now];
          Now=Next[Now][x];
          if(Now==-1)  Now=root;//即使失败,也不气馁,一切从零开始 
          int tmp=Now;
          while(tmp!=root){
                if(End[tmp]>0){
                   ans+=End[tmp];
                   End[tmp]=-1;//避免重复 
                }
                else break;
                tmp=fail[tmp];
          }
    }
}
int main()
{
    char s[55];
    char c[maxn] ;
    int n,j,i,T;
    scanf("%d",&T);
    while(T--){
        
        _init();
        scanf("%d",&n);
        for(i=1;i<=n;i++){
           scanf("%s",s);
           _insert(s);//单词 
        }
        scanf("%s",c);
        
        _build();
        _query(c);//文章 
    
        printf("%d
",ans);
   
    } 
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/hua-dong/p/7726311.html