UVA 1449

来源:互联网 发布:xinnet域名管理 编辑:程序博客网 时间:2024/05/19 02:21

1449 - Dominating Patterns

Time limit: 3.000 seconds

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 N1$ \le$N$ \le$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 to106.

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 

2 aba bab ababababac 6 beta alpha haha delta dede tata dedeltalphahahahototatalpha 0

Sample Output 

4 aba 2 alpha haha
AC自动机的模板题,白书上的例题

#include <cstdio>#include <cmath>#include <algorithm>#include <iostream>#include <cstring>#include <map>#include <string>#include <stack>#include <cctype>#include <vector>#include <queue>#include <set>#include <iomanip>using namespace std;//#define Online_Judge#define outstars cout << "***********************" << endl;#define clr(a,b) memset(a,b,sizeof(a))#define lson l , mid  , rt << 1#define rson mid + 1 , r , rt << 1|1#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)#define mk make_pairconst int inf = 1 << 30;const int MAXN = 11000 + 50;const int maxw = 100 + 20;const int MAXNNODE = 1000 +10;const long long LLMAX = 0x7fffffffffffffffLL;const long long LLMIN = 0x8000000000000000LL;const int INF = 0x7fffffff;const int IMIN = 0x80000000;#define eps 1e-8#define mod 20071027typedef long long LL;const double PI = acos(-1.0);typedef double D;typedef pair<int , int> pii;const D e = 2.718281828459;const int SIGMA_SIZE = 30;map<string , int> ms;struct Automata{    int ch[MAXN][SIGMA_SIZE];    int val[MAXN];    int cnt [200];    int last[MAXN];    int sz;    int f[MAXN];    int idx(char c){return c - 'a';}   void init()   {       sz = 1;       clr(ch[0] , 0) , clr(cnt , 0);       ms.clear();   }   void insert(char * s , int v)   {       int u = 0  , n = strlen(s);       FOR(i , 0 , n)       {           int c=  idx(s[i]);           if(!ch[u][c])           {               clr(ch[sz] , 0);               val[sz]= 0;               ch[u][c] = sz ++;           }           u = ch[u][c];       }       val[u] = v;       ms[string(s)] = v;   }   void print(int i , int j)   {       if(j)       {           cnt[val[j]]++;           print(i , last[j]);       }   }   void find(char * T)   {       int n = strlen(T);       int j = 0;       FOR(i , 0 , n)       {           int c = idx(T[i]);//           while(j && !ch[j][c]) j = f[j];           j = ch[j][c];           if(val[j])print(i , j);           else if(last[j])print(i , last[j]);       }   }   void getfail()   {       queue <int> q;       f[0] = 0;       FOR(c ,0 ,  SIGMA_SIZE )       {           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(c , 0 ,  SIGMA_SIZE )           {               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;int n , T;char text[1000010] , p[151][80];int main(){    //ios::sync_with_stdio(false);#ifdef Online_Judge    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);#endif // Online_Judge    while(scanf("%d" , &n) == 1  , n)    {        ac.init();        FORR(i , 1 , n)        {            scanf("%s" ,  p[i]);            ac.insert(p[i] , i);        }        ac.getfail();        scanf("%s" , text);        ac.find(text);        int best = -1;        FORR(i , 1 , n)        {            if(ac.cnt[i] > best)best = ac.cnt[i];        }        printf("%d\n" , best);        FORR(i , 1 , n)        {            if(ac.cnt[ms[string(p[i])]] == best)printf("%s\n" , p[i]);        }    }    return 0;}