hdu2222-Keywords Search

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 <bits/stdc++.h>
#define ll long long
using namespace std;
const int p=10007;
const int N=1e6+10;
const int M=5e5+10;
int n,T;
char s[N];
struct acmach
{
    int Next[M][26],Fail[M],End[M];
    int root,L;
    int newnode()
    {
        for (int i=0;i<26;i++) Next[L][i]=-1;
        End[L]=0;
        return L++;
    }
    void init()
    {
        L=0;
        root=newnode();
    }
    void Insert(char s[])
    {

        int len=strlen(s);
        int now=root;
        for (int i=0;i<len;i++)
        {
            if (Next[now][s[i]-'a']==-1)
                Next[now][s[i]-'a']=newnode();
            now=Next[now][s[i]-'a'];
        }
        End[now]++;
    }
    void build()
    {
        queue<int>q;
        Fail[root]=root;    //当前节点now的失败指针指向的地方
        for (int i=0;i<26;i++)
        if (Next[root][i]==-1) Next[root][i]=root; //下一个字母为i+'a'的节点的下标为Next[now][i]
        else
        {
            Fail[Next[root][i]]=root;
            q.push(Next[root][i]);
        }
        while (!q.empty())
        {
            int now=q.front(); q.pop();
            for (int i=0;i<26;i++)
            if (Next[now][i]==-1) Next[now][i]=Next[Fail[now]][i]; //指向当前节点fail指针的这个子节点
            else
            {
                Fail[Next[now][i]]=Next[Fail[now]][i]; //这个节点的失败指针指向(((他父亲节点)的失败指针所指向的那个节点)的下一个节点)
                q.push(Next[now][i]);
            }
        }
    }
    int query(char s[])
    {
       int len=strlen(s);
       int now=root;
       int res=0;
       for (int i=0;i<len;i++)
       {
           now=Next[now][s[i]-'a'];
           int tmp=now;
           while (tmp!=root)
           {
               res+=End[tmp];
               End[tmp]=0;
               tmp=Fail[tmp];
           }
       }
       return res;
    }
}ac;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        ac.init();
        scanf("%d",&n);
        for (int i=1;i<=n;i++)
        {
            scanf("%s",s);
            ac.Insert(s);
        }
        ac.build();
        scanf("%s",s);
        printf("%d\n",ac.query(s));
    }

    return 0;
}
View Code

 

posted @ 2019-07-29 10:39  特特w  阅读(185)  评论(0编辑  收藏  举报