UVAlive 4670 Dominating Patterns [AC自动机]

来源:互联网 发布:apache post 日志 编辑:程序博客网 时间:2024/05/16 05:26

Dominating Patterns
Time Limit: 3000MS
64bit IO Format: %lld & %llu

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


出现重复的用hash判重,以hash值为下表统计出现次数
可以不处理冲突,开大一点

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;#define smax(x,tmp) x=max((x),(tmp))#define smin(x,tmp) x=min((x),(tmp))#define maxx(x1,x2,x3) max(max(x1,x2),x3)#define minn(x1,x2,x3) min(min(x1,x2),x3)const int INF=0x3f3f3f3f;const int maxn = 150 * 70; // maxlength * maxnconst int maxlen = 1000005;const int SIGMA_SIZE = 26;#define idx(c) c - 'a'char ss[155][75];char t[maxlen];int cnt[155];int hash[155];int n;int f[10010];const int mod = 10007;inline int get_hash(char s[],int lens){    int pos = 0;    unsigned int ret = 0;    while(pos < lens)        ret = (ret<<5) ^ (ret>>27) ^ (unsigned int)s[pos++];    return ret % mod;}struct Node{    int id;    int ch[SIGMA_SIZE];    int fail,last;}node[maxn];int maxnode; // root = 0#define id(x) node[x].id#define ch(x,i) node[x].ch[i]#define fail(x) node[x].fail#define last(x) node[x].lastinline void insert(char s[],int index){    int lens = strlen(s);    int root=0;    int pos=0;    while(pos<lens)    {        int c = idx(s[pos]);        if(!ch(root,c)) ch(root,c) = ++maxnode;        root = ch(root,c);        pos++;    }    hash[index] = get_hash(s,lens);    if(!f[hash[index]]) f[hash[index]] = index;    id(root) = f[hash[index]];}void build_fail(){    queue <int> que;    for(int i=0;i<SIGMA_SIZE;i++)        if(ch(0,i)) que.push(ch(0,i));    while(!que.empty())    {        int u=que.front();que.pop();        for(int i=0;i<SIGMA_SIZE;i++)        {            int v = ch(u,i);            if(!v) { ch(u,i) = ch(fail(u),i); continue; }            que.push(v);            int t = fail(u);            while(t && !ch(t,i)) t = fail(t);            fail(v) = ch(t,i);            last(v) = id(fail(v))? fail(v) : last(fail(v));        }    }}void print(int root){    if(!root) return;    cnt[id(root)]++;    print(last(root));}void find(char t[]){    int lens = strlen(t);    int root = 0;    int pos = 0;    while(pos < lens)    {        int c = idx(t[pos]);        root = ch(root,c);        if(id(root)) print(root);        else if(last(root)) print(last(root));        pos++;    }}inline bool init(){    if(!~scanf("%d",&n) || !n) return false;    memset(node,0,sizeof(node));    memset(cnt,0,sizeof(cnt));    memset(hash,0,sizeof(hash));    memset(f,0,sizeof(f));    maxnode=0;    for(int i=1;i<=n;i++)    {        scanf("%s",ss+i);        insert(ss[i],i);    }    build_fail(); // !! always forgets!!    return true;}int main(){    freopen("aho.in","r",stdin);    freopen("aho.out","w",stdout);    while(init())    {        scanf("%s",t);        find(t);        int ans = -INF;        for(int i=1;i<=n;i++)            smax(ans,cnt[i]);        printf("%d\n",ans);        for(int i=1;i<=n;i++)            if(ans == cnt[f[hash[i]]]) printf("%s\n",ss[i]);    }    return 0;}
0 0
原创粉丝点击