Dominating Patterns

Dominating Patterns

Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu

Description

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 patternsN, 1N150. 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 to106.

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分析:ac自动机;代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
#define freopen freopen("in.txt","r",stdin)
const int maxn=1e5+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
int n,m,k,t,ma;
char a[1000010],b[151][80];
struct node
{
    int ch[maxn][26],f[maxn],val[maxn],last[maxn],cnt[maxn],sz;
    void init()
    {
        sz=0;
        memset(ch[0],0,sizeof(ch[0]));
        memset(cnt,0,sizeof(cnt));
    }
    int idx(char x)
    {
        return x-‘a‘;
    }
    void insert(char *p,int v)
    {
        int pos=0;
        for(int i=0;p[i];i++)
        {
            int x=idx(p[i]);
            if(!ch[pos][x])
            {
                ch[pos][x]=++sz;
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz]=0;
            }
            pos=ch[pos][x];
        }
        val[pos]=v;
    }
    void out(int x)
    {
        if(x)
        {
            cnt[val[x]]++;
            out(last[x]);
        }
    }
    void find(char *p)
    {
        int pos=0;
        for(int i=0;p[i];i++)
        {
            int x=idx(p[i]);
            pos=ch[pos][x];
            if(val[pos])out(pos);
            else if(last[pos])out(last[pos]);
        }
    }
    void fail()
    {
        queue<int>p;
        f[0]=0;
        for(int i=0;i<26;i++)
        {
            int x=ch[0][i];
            if(x)f[x]=0,p.push(x),last[x]=0;
        }
        while(!p.empty())
        {
            int x=p.front();
            p.pop();
            for(int i=0;i<26;i++)
            {
                int y=ch[x][i];
                if(!y)
                {
                    ch[x][i]=ch[f[x]][i];
                    continue;
                }
                p.push(y);
                int v=f[x];
                while(v&&!ch[v][i])v=f[v];
                f[y]=ch[v][i];
                last[y]=val[f[y]]?f[y]:last[f[y]];
            }
        }
    }
}ac;
int main()
{
    int i,j;
    while(~scanf("%d",&n)&&n)
    {
        ac.init();
        rep(i,1,n)
        {
            scanf("%s",b[i]);
            ac.insert(b[i],i);
        }
        ac.fail();
        scanf("%s",a);
        ac.find(a);
        ma=0;
        rep(i,1,n)ma=max(ma,ac.cnt[i]);
        printf("%d\n",ma);
        rep(i,1,n)if(ma==ac.cnt[i])printf("%s\n",b[i]);
    }
    //system("Pause");
    return 0;
}
时间: 2024-09-30 22:55:47

Dominating Patterns的相关文章

uva 1449 - Dominating Patterns(AC自动机)

题目练级:uva 1449 - Dominating Patterns 题目大意:有一个由小写字母组成的字符串集和一个文本T,要求找出那些字符串在文本中出现的次数最多. 解题思路:将字符串集建立AC自动机,然后传入T进行匹配,对每个匹配上的字符串多应次数加1,最后找出最大值.出现次数与最大值相同的字符串输出.注意字符集中出现相同字符的情况. #include <cstdio> #include <cstring> #include <queue> #include &l

[UVA1149]Dominating Patterns

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

LA4670 Dominating Patterns AC自动机模板

Dominating Patterns 每次看着别人的代码改成自己的模板都很头大...空间少了个0卡了好久 裸题,用比map + string更高效的vector代替蓝书中的处理方法 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7

UVa 1449 Dominating Patterns

方法:AC自动机 题意比较明显,用ac 自动机.陷阱是可能会出现重复的string. code: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <string> 6 #include <vector> 7 #include <stack> 8 #include <bits

UVALive 4670 Dominating Patterns --AC自动机第一题

题意:多个模板串,一个文本串,求出那些模板串在文本串中出现次数最多. 解法:AC自动机入门模板题. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <string> #include <vector> #in

uvalive 4670 Dominating Patterns

在文本串中找出现次数最多的子串. 思路:AC自动机模板+修改一下print函数. 1 #include<stdio.h> 2 #include<math.h> 3 #include<stdio.h> 4 #include<stdlib.h> 5 #include<iostream> 6 #include<string> 7 #include<memory.h> 8 #include<map> 9 #includ

LA 4670 (AC自动机 模板题) Dominating Patterns

AC自动机大名叫Aho-Corasick Automata,不知道的还以为是能自动AC的呢,虽然它确实能帮你AC一些题目.=_=|| AC自动机看了好几天了,作用就是多个模式串在文本串上的匹配. 因为有多个模式串构成了一颗Tire树,不能像以前一样线性递推失配函数f了,于是改成了BFS求失配函数. 白书上那个last数组(后缀链接)的含义就是:在Tire树的某个分支上虽然没有遇到单词节点,但是某个单词可能是已经匹配上的字串的后缀. 举个栗子: 有两个模式串:aaabbb, ab 现在已经匹配了a

LA 4670 Dominating Patterns (AC自动机)

题意:给定一个一篇文章,然后下面有一些单词,问这些单词在这文章中出现过几次. 析:这是一个AC自动机的裸板,最后在匹配完之后再统计数目就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <ios

UVALive4670 Dominating Patterns

题解: ac自动机模板题...稍微改改板子就行了 代码: #include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define se second #define fs first #define ll long long #define CLR(x) memset(x,0,sizeof x) #define MC(x,y) memcpy(x,y,sizeof(x)) #de