文章标题 UVALive 4670 : Dominating Patterns (AC自动机模板题)

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 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个字符串,每个串小于等于70个字符,然后有一个长串,小于等于1e6个字符,然后要找出这N个字符串在长串的出现次数最多的,如果最多有多个,按输入的顺序输出来。
分析:有多个模式串,一个长的文本串,就是AC自动机来解嘛,但然后就是怎么记录每个串出现的次数了,可以用map映射成一个整数,然后通过一个计数数组cnt来记录出现的次数。
代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <map>
using namespace std;
const int maxn=205*105;
int n;
int ch[maxn][26];
int fail[maxn];//失败指针 
int last[maxn];//后缀链接 
int cnt[maxn];//记录每个字符串出现的次数 
int val[maxn];//标记每个字符串最后一个字符,然后其值可以用来标记第几个字符 
int sz;//点的规模 
int root;//根 
char s[155][80];
char text[1000006];
map<string,int>ms;//用来映射字符串的 
void init(){//初始化 
    sz=1;
    root=0;
    val[0]=0;
    memset (ch[0],0,sizeof (ch[0]));
    memset (cnt,0,sizeof (cnt));
    ms.clear();//记得清空 
}
int insert (char *str,int id){//id表示第几个字符串 
    int len=strlen(str);
    int now=root;
    for (int i=0;i<len;i++){
        int c=str[i]-'a';
        if (!ch[now][c]){
            memset (ch[sz],0,sizeof (ch[sz]));
            val[sz]=0;
            ch[now][c]=sz++;
        }
        now=ch[now][c];
    }
    val[now]=id;//标记是第几个字符串 
    ms[string(str)]=id;//将这个字符串映射成这个id,然后就可以ms[val[now]]来确定第几个字符了 
    return now;
}
void getfail(){//求fail指针 
    queue <int> q;
    fail[root]=root;
    for (int i=0;i<26;i++){
        int u=ch[root][i];
        if (u){
            fail[u]=0;
            last[u]=0;
            q.push(u);
        }
    } 
    while (!q.empty()){
        int now=q.front();q.pop();
        for (int i=0;i<26;i++){
            int u=ch[now][i];
            if (!u){
                ch[now][i]=ch[fail[now]][i];
            }
            else {
                fail[u]=ch[fail[now]][i];
                last[u]=val[fail[u]]?fail[u]:last[fail[u]];
                q.push(u);
            }
        }
    }
}
void quary(char* str){
    int len=strlen(str);
    int now=root;
    for (int i=0;i<len;i++){
        now=ch[now][str[i]-'a'];
        int tmp=now;
        while (tmp!=root&&val[tmp]){//如果找到一个就将相应的字符串+1 
            cnt[val[tmp]]++;
            tmp=last[tmp];
        }
    }
}
int main()
{
    while (scanf ("%d",&n)!=EOF){
        if (n==0)break;
        init();
        for (int i=1;i<=n;i++){
            scanf ("%s",s[i]);
            insert(s[i],i);//插入 
        }
        getfail();
        scanf ("%s",text);
        quary(text);
        int ans=-1;
        for (int i=1;i<=n;i++){
            ans=max(ans,cnt[ms[string(s[i])]]);//找出现次数最多 
        }
        printf ("%d\n",ans);
        for (int i=1;i<=n;i++){
            if (cnt[ms[string(s[i])]]==ans){//如果次数一样就输出来 
                printf ("%s\n",s[i]);
            }
        }
    }
    return 0;
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值