1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| #include<iostream> #include<string.h> #include<stdio.h> #include<queue> #include<map> #include<algorithm> using namespace std; typedef struct A{ int word; A* fail; A* next[26]; void aaa(){ word=0; fail=NULL; for(int i=0;i<26;i++) next[i]=NULL; } }node; node *root; queue<node*>q; map<string,int>tmap; string word[200]; char t[1000010]; int num[200]; void insert(int x){ node *p=root; int len=word[x].length(); for(int i=0;i<len;i++){ int k=word[x][i]-'a'; if(p->next[k]==NULL){ node *new_node=new node; new_node->aaa(); p->next[k]=new_node; } p=p->next[k]; } p->word=x; } void build_fail(){ q.push(root); while(!q.empty()){ node *p=q.front(); q.pop(); for(int i=0;i<26;i++){ if(p->next[i]!=NULL){ node *k=p->next[i]; q.push(k); if(p==root){ k->fail=root; } else{ node *fafail=p->fail; while(fafail!=NULL){ if(fafail->next[i]!=NULL){ k->fail=fafail->next[i]; break; } fafail=fafail->fail; } if(fafail==NULL)k->fail=root; } } } } } void query(){ int len=strlen(t); node *p=root; for(int i=0;i<len;i++){ int k=t[i]-'a'; while(p->next[k]==NULL&&p!=root)p=p->fail; p=p->next[k]; if(p==NULL)p=root; else{ node *temp=p; while(temp!=root){ if(temp->word){ num[temp->word]++; } temp=temp->fail; } } } } int cmp1(int a,int b){ return a>b; } int cmp(string a,string b){ if(num[tmap[a]]==num[tmap[b]])return tmap[a]<tmap[b]; return num[tmap[a]]>num[tmap[b]]; } int main(){ int n; while(scanf("%d",&n)&&n){ memset(num,0,sizeof(num)); tmap.clear(); root=new node; root->aaa(); for(int i=1;i<=n;i++){ cin>>word[i]; tmap[word[i]]=i; insert(i); } build_fail(); scanf("%s",t); query(); sort(word+1,word+1+n,cmp); sort(num+1,num+1+n,cmp1); int maxnum=num[1]; cout<<maxnum<<endl; for(int i=1;i<=n;i++){ if(num[i]==maxnum){ cout<<word[i]<<endl; } } } return 0; }
|