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, 1N150. 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自动机fail指针必须用bfs,因为之前的结点可能指向后面的指针
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cstring> #include<cctype> #include<ctime> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define MAXN (150+10) #define MAXp (70+10) #define MAXT (1000000+10) struct node { node *ch[26],*last,*f; int val,findtime,no; char c; node():val(0),findtime(0){Rep(i,26) ch[i]=NULL;last=NULL;f=NULL;} node(node *_f,int j):val(0),findtime(0){Rep(i,26) ch[i]=NULL;last=NULL;f=_f;c=j+'a';} }*root=new node(); void getfail(node *&x,int j) { node* p=x->f; if (p==root) {x->last=root;return;} for(p=p->last;p!=root&&!p->ch[j];p=p->last); if (!p->ch[j]) x->last=root; else x->last=p->ch[j]; } void insert(char* s,int no) { int len=strlen(s); node *now=root; Rep(i,len) { int j=s[i]-'a'; if (!now->ch[j]) now->ch[j]=new node(now,j);//getfail(now->ch[j],j); now=now->ch[j]; } now->val++; if (now->val==1) now->no=no; } node *q[MAXN*MAXp*8]; void bfs() { q[1]=root; int head=1,tail=1; while (head<=tail) { node *now=q[head]; Rep(j,26) if (now->ch[j]) q[++tail]=now->ch[j],getfail(q[tail],j); head++; } } int ans=0; void increase(node *x) { while (x) { if (x->val) { x->findtime++; ans=max(ans,x->findtime); } x=x->last; } } void find(char *T) { int len=strlen(T); node *now=root; Rep(i,len) { int j=T[i]-'a'; while (now!=root&&!now->ch[j]) now=now->last; if (now->ch[j]) now=now->ch[j],increase(now); } } int n,size=-1; bool b[MAXN]={0}; char st[MAXp]; void print(node *x) { if (x->val&&x->findtime==ans) b[x->no]=1;//st[size+1]=0,printf("%s\n",st); Rep(j,26) if (x->ch[j]) st[++size]=j+'a',print(x->ch[j]),size--; } char s[MAXN][MAXp],T[MAXT]; void delet(node *x) { Rep(j,26) if (x->ch[j]) delet(x->ch[j]); delete x; } int main() { // freopen("la4670.in","r",stdin); // freopen(".out","w",stdout); while (scanf("%d",&n)&&n) { memset(b,0,sizeof(b)); ans=0; root=new node(); For(i,n) scanf("%s",s[i]),insert(s[i],i); bfs(); scanf("%s",T); find(T); printf("%d\n",ans); print(root); For(i,n) if (b[i]) printf("%s\n",s[i]); delet(root); // cout<<size<<endl; } return 0; }