| Time Limit:3000MS | Memory Limit:Unknown | 64bit IO Format:%lld & %llu |
Description
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 patternsN, 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 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个小写字母组成的字符床和一个文本串 你的任务是找出哪些字符串在文本中出现的次数最多 例如 aba 在ababa中出现2次 但是bab只出现了一次 输入n 之后n个字符串 长度为1-70 n小于等于150 之后一个文本串 长度最长为10的6次方 输出出现最多的次数 以及出现最多的字符串为什么 如果存在多个 按输入顺序排列 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36265 思路: 很明显的ac自动机#include<stdio.h> #include<string.h> #include<malloc.h> #include<queue> #include<set> using namespace std; struct node { int count,id; struct node *next[26]; struct node *fail; void init() { int i; for(i=0;i<26;i++) next[i]=NULL; count=0; fail=NULL; id=-1; } }*root; void insert(char *str,int id) { int len,k; node *p=root; len=strlen(str); for(k=0;k<len;k++) { int pos=str[k]-'a'; if(p->next[pos]==NULL) { p->next[pos]=new node; p->next[pos]->init(); p=p->next[pos]; } else p=p->next[pos]; } p->count++; p->id=id; } void getfail() { int i; node *p=root,*son,*temp; queue<struct node *>que; que.push(p); while(!que.empty()) { temp=que.front(); que.pop(); for(i=0;i<26;i++) { son=temp->next[i]; if(son!=NULL) { if(temp==root) {son->fail=root;} else { p=temp->fail; while(p) { if(p->next[i]) { son->fail=p->next[i]; break; } p=p->fail; } if(!p) son->fail=root; } que.push(son); } } } } int num[200]; char str[1000000+100]; void query() { int len,i,cnt=0; len=strlen(str); node *p,*temp; p=root; for(i=0;i<len;i++) { int pos=str[i]-'a'; while(!p->next[pos]&&p!=root) p=p->fail; p=p->next[pos];// if(!p) p=root;// temp=p; /*不要用*temp=*p 因为*p表示一个node,而*temp也表示一个node 但是由于*temp没有分配空间 所以是不能进行赋值的 但是可以用temp指针去指向p*/ while(temp!=root) { if(temp->count>=1) { if(temp->id!=-1) num[temp->id]++; // temp->count=-1; } temp=temp->fail; } } //printf("%d\n",cnt); } char rem[160][100]; int main() { int cas,n; while(scanf("%d",&n)!=EOF) { if(!n) break; root=new node; root->init(); root->fail=NULL; int i; getchar(); for(i=0;i<n;i++) { gets(rem[i]); insert(rem[i],i); } getfail(); memset(num,0,sizeof(num)); gets(str); query(); int maxnum=-1; for(i=0;i<n;i++) { // printf("num[%d]=%d\n",i,num[i]); if(num[i]>maxnum) maxnum=num[i]; } printf("%d\n",maxnum); for(i=0;i<n;i++) if(maxnum==num[i]) printf("%s\n",rem[i]); } return 0; }