Keywords Search(AC自動機-結構體數組)

Keywords Search

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自動機模板題;

推薦視頻:UESTCACM 每週算法講堂 AC自動機

模板代碼:

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
const int maxn=1000006;
const int maxm=500005;
struct Aho
{
    struct state
    {
        int ne[26]; //
        int fail; //失配指針
        int cnt; //這一點有多少個串結束
    } stateT[maxm];

    int tot;  //結點個數

    queue<int> qu;

    void init()  //初始化
    {
        while(!qu.empty())
            qu.pop();
        for(int i=0; i<maxm; i++)
        {
            mem(stateT[i].ne,0);
            stateT[i].fail=stateT[i].cnt=0;
        }
        tot=1;
    }

    void Insert(char *str)  //插入字符串
    {
        int ls=strlen(str);
        int rt=0;
        for(int i=0; i<ls; i++)
        {
            int x=str[i]-'a';   //把字符串的字符存入字典樹
            if(!stateT[rt].ne[x])
                stateT[rt].ne[x]=tot++;  //若這一字符沒有在這一分支 ,節點數+1;
            rt=stateT[rt].ne[x];
        }
        stateT[rt].cnt++;  //經過這一節點的++;
     //   printf("stateT_cnt==%d\n",stateT[rt].cnt);
    }

    void Build()  //失配指針,若失配要找到它的父親,進行寬搜(queue)
    {
        stateT[0].fail=-1;  //根節點指向-1
        qu.push(0);
        while(!qu.empty())
        {
            int u=qu.front();
            qu.pop();
            for(int i=0; i<26; i++)  //提出一個點
            {
                if(stateT[u].ne[i]) //如果有
                {
                    if(u==0)  //當它是根節點時指向0;
                        stateT[stateT[u].ne[i]].fail=0;
                    else   //沒有,就找祖先
                    {
                        int v=stateT[u].fail;  
                        while(v!=-1)   //一直找 直到祖先有i這條邊
                        {
                            if(stateT[v].ne[i])
                            {
                                stateT[stateT[u].ne[i]].fail=stateT[v].ne[i];
                                break;  //保證找到的是最長的
                            }
                            v=stateT[v].fail;
                        }
                        if(v==-1)  //沒找到
                            stateT[stateT[u].ne[i]].fail=0;
                    }
                    qu.push(stateT[u].ne[i]);
                }
            }
        }
    }

    int Get(int x)  //得到這個數(加自己)的祖先的個數
    {
        int sum=0;
        while(x)
        {
            sum=sum+stateT[x].cnt;
            stateT[x].cnt=0;
            x=stateT[x].fail;
        }
       // printf("Gsum=%d\n",sum);
        return sum;
    }
    int Match(char *str)
    {
        int ls=strlen(str);
        int ans=0,rt=0;
        for(int i=0; i<ls; i++)  //找的是以i爲後綴有幾個匹配的字符串
        {
            int x=str[i]-'a';
            if(stateT[rt].ne[x])
                rt=stateT[rt].ne[x];
            else  //找祖先
            {
                int f=stateT[rt].fail;

                while(f!=-1 && stateT[f].ne[x]==0) //所指數沒有值
                    f=stateT[f].fail;
                if(f==-1)  //始終沒找到,回到根節點
                    rt=0;
                else
                    rt=stateT[f].ne[x];
            }
            if(stateT[rt].cnt)  //如果這個點還有串沒匹配
              {
                  ans=ans+Get(rt); //rt所有祖先cnt的和。
              }
        }
        return ans;
    }

} aho;
int n;
char S[maxn];
int main()
{
    int ca;
    scanf("%d",&ca);
    while(ca--)
    {
        aho.init();  //初始化
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%s",S);
            aho.Insert(S);  //插入字典樹
        }
        aho.Build();  //建AC自動機的樹
        scanf("%s",S);
        printf("%d\n",aho.Match(S));
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章