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, 1N
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
题意:有n个小写字母组成的字符串和一个文本串T,找出在文本中出现次数最多的字符串。
思路:在Trie中插入单词,单词的最后结点存这是第几个单词。Query的时候找到末结点就不断fail更新单词出现次数。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define maxn 15000 struct node { node * fail; node * next[26]; int cnt; node() { fail = NULL; for(int i = 0;i < 26;i++) next[i] = NULL; cnt = 0; } }*q[maxn]; char str[158][78],T[1000080]; int first,rear; int num[maxn]; void Creat_Trie(char * s,node * root,int k) { int i,id,len; node * p = root; len = strlen(s); for(int i = 0;i < len;i++) { id = s[i] - 'a'; if(p -> next[id] == NULL) p -> next[id] = new node(); p = p -> next[id]; } p -> cnt = k; } void Build_ac_automation(node * root) { int i; root -> fail = NULL; q[rear++] = root; while(first != rear) { node * temp = q[first++]; node * p = NULL; for(i = 0;i < 26;i++) { if(temp -> next[i] != NULL) { if(temp == root) temp -> next[i] -> fail = root; else { p = temp -> fail; while(p != NULL) { if(p -> next[i] != NULL) { temp -> next[i] -> fail = p -> next[i]; break; } p = p -> fail; } if(p == NULL) temp -> next[i] -> fail = root; } q[rear++] = temp -> next[i]; } } } } void Query(node * root,char * s) { node * p = root; int len = strlen(s); for(int i = 0;i < len;i++) { int id = s[i] - 'a'; while(p -> next[id] == NULL && p != root) p = p -> fail; p = p -> next[id]; if(p == NULL) p = root; node * temp = p; while(temp != root && temp -> cnt != 0) { num[temp -> cnt]++; temp = temp -> fail; } } } int main() { int n; while(scanf("%d",&n)!=EOF && n) { memset(num,0,sizeof(num)); first = rear = 0; node * root = new node(); for(int i = 1;i <= n;i++) { scanf("%s",str[i]); Creat_Trie(str[i],root,i); } Build_ac_automation(root); scanf("%s",T); Query(root,T); int ans = 0; for(int i = 1;i <= n;i++) { if(num[i] > ans) ans = num[i]; } printf("%d\n",ans); for(int i = 1;i <= n;i++) if(num[i] == ans) printf("%s\n",str[i]); } return 0; }