Dominating Patterns uvalive+一道简单的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 N1$ \le$N$ \le$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
解决方案:只要在查询的时候修改一下,记录每个单词出现的次数,同时更新最大出现次数,为了防止输入的时候出现相同的模式串,可以用map防止建立字典树的时候模式串重复。最后按顺序输出出现次数与最大次数相同的字符即可。
code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
struct trie
{
    struct trie *child[26];
    struct trie *fail;
    //int cnt;
    int v;
    trie()
    {
        for(int i=0; i<26; i++)
            child[i]=NULL;
        fail=NULL;
        v=0;
    }

};
char text[1000005];
//char word[160][80];
map<string ,int>H;
map<int ,string>IH;
trie *root;
int cnt[160];
int Max=0,ans;
int N;
void create(char *word,int k)
{
    trie *p=root;
    int i=0;
    while(word[i]!='\0')
    {
        int c=word[i]-'a';
        if(p->child[c]==NULL)
        {
            p->child[c]=new trie();
        }
        p=p->child[c];
        i++;
    }
    p->v=k;
}
void make_fail()
{
    queue<trie*>Q;
    Q.push(root);
    trie *p;
    while(!Q.empty())
    {
        p=Q.front();
        Q.pop();
        for(int i=0; i<26; i++)
        {
            if(p->child[i]!=NULL)
            {
                if(p==root) p->child[i]->fail=root;
                else
                {
                    trie *temp=p;
                    while(temp->fail!=NULL)
                    {
                        if(temp->fail->child[i]!=NULL)
                        {
                            p->child[i]->fail=temp->fail->child[i];
                            break;
                        }
                        temp=temp->fail;
                    }
                    if(temp->fail==NULL)
                    {
                        p->child[i]->fail=root;
                    }
                }
                Q.push(p->child[i]);
            }
        }
    }



}///构建fail指针
void AC_search()
{
    int i=0;
    trie *p=root;
    while(text[i]!='\0')
    {
        int id=text[i]-'a';
        while(p!=root&&p->child[id]==NULL) p=p->fail;///匹配失败,沿fail指针走
        if(p->child[id]!=NULL)
        {
            p=p->child[id];
            p=(p==NULL)?root:p;
            trie *temp=p;
            while(temp!=root&&temp->v>0)
            {
               // cout<<"sd";
               cnt[temp->v]++;
                if(cnt[temp->v]>Max) Max=cnt[temp->v];
                temp=temp->fail;
            }

        }
        i++;

    }


}
int main()
{
    while(~scanf("%d",&N)&&N)
    {
        root =new trie();
        char str[100];
        int j=0;
        H.clear();
        memset(cnt,0,sizeof(cnt));
        for(int i=0; i<N; i++)
        {
            scanf("%s",str);
            if(!H[str])
            {
                create(str,++j);
                H[str]=1;
                IH[j]=str;
            }
        }
        Max=0;
        scanf("%s",text);
        make_fail();
        AC_search();
        printf("%d\n",Max);
        for(int i=1;i<=j;i++){
            if(cnt[i]==Max){
               cout<<IH[i]<<endl;
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值