Dominating Patterns
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 to 106.
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解决方案:只要在查询的时候修改一下,记录每个单词出现的次数,同时更新最大出现次数,为了防止输入的时候出现相同的模式串,可以用map防止建立字典树的时候模式串重复。最后按顺序输出出现次数与最大次数相同的字符即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
struct trie
{
struct trie *child[26];
struct trie *fail;
//int cnt;
int v;
trie()
{
for(int i=0; i<26; i++)
child[i]=NULL;
fail=NULL;
v=0;
}
};
char text[1000005];
//char word[160][80];
map<string ,int>H;
map<int ,string>IH;
trie *root;
int cnt[160];
int Max=0,ans;
int N;
void create(char *word,int k)
{
trie *p=root;
int i=0;
while(word[i]!='\0')
{
int c=word[i]-'a';
if(p->child[c]==NULL)
{
p->child[c]=new trie();
}
p=p->child[c];
i++;
}
p->v=k;
}
void make_fail()
{
queue<trie*>Q;
Q.push(root);
trie *p;
while(!Q.empty())
{
p=Q.front();
Q.pop();
for(int i=0; i<26; i++)
{
if(p->child[i]!=NULL)
{
if(p==root) p->child[i]->fail=root;
else
{
trie *temp=p;
while(temp->fail!=NULL)
{
if(temp->fail->child[i]!=NULL)
{
p->child[i]->fail=temp->fail->child[i];
break;
}
temp=temp->fail;
}
if(temp->fail==NULL)
{
p->child[i]->fail=root;
}
}
Q.push(p->child[i]);
}
}
}
}///构建fail指针
void AC_search()
{
int i=0;
trie *p=root;
while(text[i]!='\0')
{
int id=text[i]-'a';
while(p!=root&&p->child[id]==NULL) p=p->fail;///匹配失败,沿fail指针走
if(p->child[id]!=NULL)
{
p=p->child[id];
p=(p==NULL)?root:p;
trie *temp=p;
while(temp!=root&&temp->v>0)
{
// cout<<"sd";
cnt[temp->v]++;
if(cnt[temp->v]>Max) Max=cnt[temp->v];
temp=temp->fail;
}
}
i++;
}
}
int main()
{
while(~scanf("%d",&N)&&N)
{
root =new trie();
char str[100];
int j=0;
H.clear();
memset(cnt,0,sizeof(cnt));
for(int i=0; i<N; i++)
{
scanf("%s",str);
if(!H[str])
{
create(str,++j);
H[str]=1;
IH[j]=str;
}
}
Max=0;
scanf("%s",text);
make_fail();
AC_search();
printf("%d\n",Max);
for(int i=1;i<=j;i++){
if(cnt[i]==Max){
cout<<IH[i]<<endl;
}
}
}
return 0;
}