您的位置:首页 > 其它

AC自动机模板 LA4670 Dominating Patterns 出现次数最多的字串 BNUOJ11552 UVA1449

2015-05-27 22:35 525 查看
题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=11552

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 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



有n个小写字母组成的字符串和一个文本串T,找出哪些字符串在T中出现的次数最多。
AC自动机,代码来自刘汝佳的大白书

//有n个小写字母组成的字符串和一个文本串T,找出哪些字符串在T中出现的次数最多。
//AC自动机,代码来自刘汝佳的大白书
#include<cstring>
#include<queue>
#include<cstdio>
#include<map>
#include<string>
using namespace std;

const int SIGMA_SIZE = 26;
const int MAXNODE = 11000;
const int MAXS = 150 + 10;

map<string,int>ms;

struct AhoCorasickAutomata
{
int ch[MAXNODE][SIGMA_SIZE];
int f[MAXNODE];//fail数组
int val[MAXNODE];//val>0就是单词节点
int last[MAXNODE];//链表的下一个节点
int cnt[MAXS];
int sz;//节点总数

void init(){
sz=1;//初始时一个根节点
memset(ch[0],0,sizeof(ch[0]));
memset(cnt,0,sizeof(cnt));
ms.clear();
}

int idx(char c){
return c-'a';
}

//插入字符串。v必须非0
void Insert(char *s,int v){
int u=0,n=strlen(s);
for(int i=0;i<n;i++){
int c=idx(s[i]);
if(!ch[u][c]){//节点不存在
memset(ch[sz],0,sizeof(ch[sz]));
val[sz]=0;//中间节点是0
ch[u][c]=sz++;//新建节点
}
u=ch[u][c];//往下走
}
val[u]=v;//字符串最后一个字符标志信息为v
ms[string(s)]=v;
}

//递归打印以节点j结尾的所有字符串
void print(int j){
if(j){
cnt[val[j]]++;
print(last[j]);
}
}

//在T中找模板
int Find(char *T){
int n = strlen(T);
int j=0;//初试为根,当前节点编号
for(int i=0;i<n;i++){ //文本串当前指针
int c=idx(T[i]);
while(j&&!ch[j][c]) j=f[j];//顺着失配走,直到可以匹配到
j=ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]);//找到了
}
}

void getFail(){
queue<int>q;
f[0]=0;

for(int c=0;c<SIGMA_SIZE;c++){//初始化队列
int u=ch[0][c];
if(u){
f[u]=0;q.push(u);last[u]=0;
}
}

while(!q.empty()){
int r=q.front();q.pop();
for(int c=0;c<SIGMA_SIZE;c++){
int u=ch[r][c];
if(!u) continue;
q.push(u);
int v=f[r];
while(v&&!ch[v][c]) v=f[v];
f[u]=ch[v][c];
last[u]=val[f[u]]?f[u]:last[f[u]];
}
}
}

};

AhoCorasickAutomata ac;
char text[1000005],P[151][80];
int n,T;

int main()
{
while(scanf("%d", &n) == 1 && n)
{
ac.init();
for(int i=1;i<=n;i++){
scanf("%s",P[i]);
ac.Insert(P[i],i);//注意同时更新ms映射
}
ac.getFail();
scanf("%s",text);
ac.Find(text);//计算每个模板的cnt
int best = -1;
for(int i=1;i<=n;i++)
if(ac.cnt[i]>best) best=ac.cnt[i];//最大值
printf("%d\n",best);
for(int i=1;i<=n;i++)
if(ac.cnt[ms[string(P[i])]]==best) printf("%s\n",P[i]);//输出那些模板串
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: