描述
给出一个字符串和一些子串,求其中出现次数最多的子串.
分析
在AC自动机上面跑就行了.但是有一个要注意的地方,就是在输入文件里同一个子串重复出现.如果不特殊处理的话,后一个子串就会把Trie里的前一个子串覆盖掉.,所以我们可以用个map...
#include <bits/stdc++.h>
using namespace std; const int maxn=+,maxl=1e6+,maxnode=*+;
int n;
char text[maxl],p[maxn][+];
map <string,int> ms;
struct Aho_Corasick{
int ch[maxnode][];
int f[maxnode],val[maxnode],last[maxnode],cnt[maxn];
int sz;
inline int idx(char c){ return c-'a'; }
void init(){
sz=;
memset(ch[],,sizeof ch[]);
memset(cnt,,sizeof cnt);
ms.clear();
}
void insert(char *s,int v){
ms[string(s)]=v;
int u=;
for(;*s;s++){
int c=idx(*s);
if(!ch[u][c]){
memset(ch[++sz],,sizeof ch[]);
val[sz]=;
ch[u][c]=sz;
}
u=ch[u][c];
}
val[u]=v;
}
void get_fail(){
queue <int> q;
f[]=;
for(int c=;c<;c++){
int u=ch[][c];
if(u){ f[u]=; q.push(u); }
}
while(!q.empty()){
int r=q.front(); q.pop();
for(int c=;c<;c++){
int u=ch[r][c];
if(!u){ ch[r][c]=ch[f[r]][c]; continue; }
q.push(u);
int v=f[r];
f[u]=ch[v][c];
last[u]=val[f[u]]?f[u]:last[f[u]];
}
}
}
void work(int j){
if(j){
cnt[val[j]]++;
work(last[j]);
}
}
void find(char *T){
int j=;
for(;*T;T++){
int c=idx(*T);
while(j&&!ch[j][c]) j=f[j];
j=ch[j][c];
if(val[j]) work(j);
else work(last[j]);
}
}
}ac;
int main(){
while(scanf("%d",&n)&&n){
ac.init();
for(int i=;i<=n;i++){
scanf("%s",p[i]);
ac.insert(p[i],i);
}
ac.get_fail();
scanf("%s",text);
ac.find(text);
int best=-;
for(int i=;i<=n;i++) best=max(best,ac.cnt[i]);
printf("%d\n",best);
for(int i=;i<=n;i++) if(ac.cnt[ms[string(p[i])]]==best) printf("%s\n",p[i]);
}
return ;
}
4670
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 , 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
to 10 6 .
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