UVA 1449 Dominating Patterns

AC自动机模板。。

4670 - Dominating Patterns
Asia - Hefei - 2009/2010
The archaeologists are going to decipher a very mysterious ``language". Now, they know many language
patterns; each pattern can be treated as a string on English letters (only lower case). As a sub string, these
patterns may appear more than one times in a large text string (also only lower case English letters).
What matters most is that which patterns are the dominating patterns. Dominating pattern is the pattern whose
appearing times is not less than other patterns.
It is your job to find the dominating pattern(s) and their appearing times.
Input 
The entire input contains multi cases. The first line of each case is an integer, which is the number of patterns
N, 1 N 150. Each of the following N lines contains one pattern, whose length is in range [1, 70]. The rest
of the case is one line contains a large string as the text to lookup, whose length is up to 106.
At the end of the input file, number `0' indicates the end of input file.
Output 
For each of the input cases, output the appearing times of the dominating pattern(s). If there are more than one
dominating pattern, output them in separate lines; and keep their input order to the output.
Sample Input 

aba 
bab 
ababababac 

beta 
alpha 
haha 
delta 
dede 
tata 
dedeltalphahahahototatalpha 
0
Sample Output 

aba 

alpha 
haha
Hefei 2009-2010
4670 - Dominating Patterns 1/1

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define prt(k) cout<<#k"="<<k<<endl;
#define ll long long
#include<queue>
const int N=1e6+8;
const int SIZE=26;
int cnt[N];
#include<map>
map<string ,int> ms;
struct AC
{
    int ch[N][33];
    int val[N];
    int sz;
    void init()
    {
        sz=1;
        memset(ch[0],0,sizeof ch[0]);
        memset(cnt,0,sizeof cnt);
        ms.clear();
    }
    AC()
    {
        init();
    }
    int idx(char a)
    {
        return a-'a';
    }
    int last[N];
    int f[N];
    void insert(char* s,int v)
    {
        int u=0,n=strlen(s);//s.size();
        for(int i=0; i<n; i++)
        {
            int c=idx(s[i]);
            if(!ch[u][c])
            {
                memset(ch[sz],0,sizeof ch[sz]);
                val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]=v;
        ms[string(s)] =v;
    }
    void solve(int j)
    {
        if(j)
        {
            cnt[val[j]]++;
            solve(last[j]);
        }
    }
    int find(char* t)
    {
        int len=strlen(t);
        int j=0;
        for(int i=0; i<len; i++)
        {
            int c=idx(t[i]);
            /// while(j&&!ch[j][c]) j=f[j];
            j=ch[j][c];
            if(val[j]) solve(j);
            else if(last[j]) solve(last[j]);
        }
    }

    int getfail()
    {
        queue<int> q;
        f[0]=0;
        for(int c=0; c<SIZE; c++)
        {
            int u=ch[0][c];
            if(u)
            {
                f[u]=0;
                q.push(u);
                last[u]=0;
            }
        }
        while(!q.empty())
        {
            int r=q.front();
            q.pop();
            for(int c=0; c<SIZE; c++)
            {
                int u=ch[r][c];
                if(!u)
                {
                    ch[r][c]=ch[f[r]][c];
                    continue;
                }
                q.push(u);
                int v=f[r];
                while(v&&!ch[v][c])v=f[v];
                f[u]=ch[v][c];
                last[u] = val[f[u]] ? f[u] : last[f[u]];
            }
        }
    }
};
AC ac;
char t[N],p[333][114];
int n;
int main()
{
    while(scanf("%d",&n)==1&&n)
    {
        ac.init();
        for(int i=1; i<=n; i++)
        {
            scanf("%s",p[i]);
            ac.insert(p[i],i);
        }
        ac.getfail();
        scanf("%s",t);
        ac.find(t);
        int best=-1;
        for(int i=1; i<=n; i++)
        {
            best=max(best,cnt[i]);
        }
        printf("%d\n",best);
        for(int i=1; i<=n; i++)
            if(cnt[ms[string(p[i])]]==best) printf("%s\n",p[i]);
    }
}


你可能感兴趣的:(Algorithm,ACM,iostream,uva,CString)