首页 > 代码库 > HDU 2222——Keywords Search(AC自动机)

HDU 2222——Keywords Search(AC自动机)

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 34020    Accepted Submission(s): 11009


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
 

————————————————————————————————————————————————


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
#define sigma_size 26
using namespace std;
char str[55],s[1000001];
struct node
{
    int count;
    node *next[sigma_size];
    node *fail;
};

node *root=new node;
void init(node *p)
{
    memset(p->next,0,sizeof p->next);
    p->count=0;
    p->fail=NULL;
}

void insert(char *s)
{
    int l=strlen(s);
    node *p=root,*q;
    for(int i=0;i<l;++i){
        int c=s[i]-'a';
        if(p->next[c]==NULL){
            q=new node;
            init(q);
            p->next[c]=q;
        }
        p=p->next[c];
    }
    p->count++;
}

void get_fail()
{
    node *p;
    queue<node *> q;
    q.push(root);
    while(!q.empty()){
        p=q.front();q.pop();
        for(int i=0;i<sigma_size;++i){
            if(p->next[i]==NULL) continue;
            q.push(p->next[i]);

            if(p==root){
                p->next[i]->fail=root;
            }
            else{
                node *u=p;
                while(u->fail!=NULL&&u->fail->next[i]==NULL)
                    u=u->fail;
                if(u->fail==NULL) p->next[i]->fail=root;
                else p->next[i]->fail=u->fail->next[i];
            }

        }
    }
}

int find(char *s)
{
    node *p=root,*q;
    int l=strlen(s),res=0;
    for(int i=0;i<l;++i){
        int c=s[i]-'a';
        while(p!=root&&p->next[c]==NULL) p=p->fail;
        if(p->next[c]==NULL) continue;
        p=p->next[c];
        q=p;
        while(q!=root&&q->count!=-1){
            res+=q->count;
            q->count=-1;
            q=q->fail;
        }
    }
    return res;
}

void freedom(node *p)
{
    for(int i=0;i<26;++i){
        if(p->next[i]!=NULL){
            freedom(p->next[i]);
        }
    }
    delete p;
}

int main()
{
    int T,n;
    scanf("%d",&T) ;
    while(T--){
        init(root) ;
        scanf("%d",&n) ;
        getchar();
        while(n--){
            gets(str) ;
            insert(str) ;
        }
        get_fail() ;
        gets(s) ;
        printf("%d\n",find(s) ) ;
        for(int i = 0;i < 26;i ++){//注意root不能删除

            if(root->next[i] != NULL)
                freedom(root->next[i]) ;
        }
    }
    return 0 ;
}








#include<iostream>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<cstdio>
#define maxnode 500000+10
#define sigma_size 26
#define M 1000000+10
using namespace std;
char str[M];
struct tree
{
    int f[maxnode];
    int ch[maxnode][sigma_size];
    int val[maxnode];
    int sz;
    void reset(){memset(ch[0],0,sizeof ch[0]);memset(f,0,sizeof f);memset(val,0,sizeof val);sz=1;}
    int idx(char c){return c-'a';}

    void insert(char *s)
    {
        int l=strlen(s);
        int u=0;
        for(int i=0;i<l;++i){
            int c=idx(s[i]);
            if(!ch[u][c]){
                memset(ch[sz],0,sizeof ch[sz]);
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]++;
    }

    void get_fail()
    {
        queue<int> q;
        for(int c=0;c<sigma_size;++c){
            int u=ch[0][c];
            if(u){f[u]=0;q.push(u);}
        }
        while(!q.empty()){
            int r=q.front();q.pop();
            for(int i=0;i<sigma_size;++i){
                int u=ch[r][i];
                if(!u) continue;
                q.push(u);
                int v=f[r];//指向父亲的失败指针
                while(v&&!ch[v][i]) v=f[v];//父亲的失败指针的儿子不存在c,就继续沿着失败指针走
                f[u]=ch[v][i];
            }
        }
    }

    int find(char *s)
    {
        int l=strlen(s);
        int u=0,res=0;
        for(int i=0;i<l;++i){
            int c=idx(s[i]);

            while(u&&!ch[u][c]) u=f[u];
            if(!ch[u][c]) continue;//不匹配则跳过
            u=ch[u][c];//匹配,那么从该节点继续匹配
            int tmp=u;//temp指针,寻找较短的前缀
            while(tmp&&val[tmp]!=-1){

                res+=val[tmp];
                val[tmp]=-1;
                tmp=f[tmp];
            }
        }
        return res;
    }
}Trie;
int main()
{
    int T;
    cin>>T;
    while(T--){
        Trie.reset();
        int n;
        scanf("%d",&n);
        while(n--){
            scanf("%s",str);
            Trie.insert(str);
        }
        Trie.get_fail();
        scanf("%s",str);
        printf("%d\n",Trie.find(str));
    }
    return 0;
}