CUGB课题训练之数据结构:E - Keywords Search(HDU 2222 AC自动机经典入门模板题)

CUGB专题训练之数据结构:E - Keywords Search(HDU 2222 AC自动机经典入门模板题)

E - Keywords Search
Time Limit:1000MS     Memory Limit:32768KB     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

这题根据书中的模板敲的,咦……不过有点坑……

比如下面这组样例:

1

2

aa

aa

aaa

如果模板不改一点的话,输出是:4

但是根据题目意思,最大只为2,即最大为n,因为最大匹配的单词嘛!所以上面就错了。所以为什么错呢……

因为算法经典入门中有一个优化,就是最后一个字母如果last指针指向根的话,那这个需要next数组配合的,所以如果在查询时不用next的话,那就是优化了失配函数了,直接不需要失配函数了。但是最后一个字母 last指针 需要自己指向自己,所以在上面的例子中就会出现重复计算的情况,所以把val 数组设置为初始状态就可以了……

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define pri(a) printf("%d\n",a)
#define MM 1000005
#define MN 250005
#define INF 10000007
using namespace std;
char str[MM];
int next[MN],last[MN],sum;
struct Trie
{
    int ch[MN][27],val[MN],sz;
    int idx(char c) {return c-'a';}
    void reset() { sz=1; mem(ch,0); mem(val,0); }
    void insert(char *s)
    {
        int u=0,i,c,l=strlen(s);
        for(i=0;i<l;i++)
        {
            c=idx(s[i]);
            if(!ch[u][c]) ch[u][c]=sz++;
            u=ch[u][c];
        }
        val[u]++;
    }
}T;
void getfail()
{
    queue<int>q;
    int c,u;
    next[0]=0;
    for(c=0;c<26;c++)
    {
        u=T.ch[0][c];
        if(u) {next[u]=0;q.push(u);last[u]=0;}
    }
    while(!q.empty())
    {
        int r=q.front(); q.pop();
        for(c=0;c<26;c++)
        {
            u=T.ch[r][c];
            if(!u)
            {
                T.ch[r][c]=T.ch[next[r]][c]; //优化next
                continue;
            }
            q.push(u);
            int v=next[r];
            while(v&&!T.ch[v][c]) v=next[v];
            next[u]=T.ch[v][c];
            last[u]=T.val[next[u]]?next[u]:last[next[u]];
        }
    }
}
void print(int j)
{
    if(j)
    {
        sum+=T.val[j];
        T.val[j]=0;  //因为最后一个可能有自环,所以得把自环去掉
        print(last[j]);
    }
}
void query(char *ss)
{
    int l=strlen(ss),i,j=0,c;
    for(i=0;i<l;i++)
    {
        c=T.idx(ss[i]);
        j=T.ch[j][c];
        if(T.val[j]) print(j);
        else if(last[j]) print(last[j]);
    }
}
int main()
{
    int t;
    sca(t);
    while(t--)
    {
        int n;
        char s[53];
        T.reset(); sum=0;
        mem(next,0);
        mem(last,0);
        sca(n);
        getchar();
        while(n--)
        {
            gets(s);
            T.insert(s);
        }
        gets(str);
        getfail();
        query(str);
        pri(sum);
    }
    return 0;
}