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

来源:互联网 发布:kali linux和centos 编辑:程序博客网 时间:2024/06/05 09:26

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
原创粉丝点击