uva 1449 AC自动机

题意:

 求在匹配串中出现次数最多的模式串。

思路:

 AC自动机裸题。 稍微改一下。。

code:

    #include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
int hs[1600];
int n;
int Next[50010][26],fail[50010],End[50010];
char a[151][160];
map<string, int> m;
map<int, string> m2;
int cnt;
struct Trie
{
    int root,L;
    int newnode()
    {
        for(int i = 0;i < 26;i++)
            Next[L][i] = -1;
        End[L++] = 0;
        return L-1;
    }
    void init()
    {
        L = 0;
        root = newnode();
    }
    void insert(const char buf[], int id)
    {
        int len = strlen(buf);
        int now = root;
        for(int i = 0;i < len;i++)
        {
            if(Next[now][buf[i]-'a'] == -1)
                Next[now][buf[i]-'a'] = newnode();
            now = Next[now][buf[i]-'a'];
        }
        End[now] = id;
    }
    void build()
    {
        queue<int>Q;
        fail[root] = root;
        for(int i = 0;i < 26;i++)
            if(Next[root][i] == -1)
                Next[root][i] = root;
            else
            {
                fail[Next[root][i]] = root;
                Q.push(Next[root][i]);
            }
        while( !Q.empty() )
        {
            int now = Q.front();
            Q.pop();
            for(int i = 0;i < 26;i++)
                if(Next[now][i] == -1)
                    Next[now][i] = Next[fail[now]][i];
                else
                {
                    fail[Next[now][i]]=Next[fail[now]][i];
                    Q.push(Next[now][i]);
                }
        }
    }
    void query(char buf[])
    {
        memset(hs, 0, sizeof(hs));
        int len = strlen(buf);
        int now = root;
        for(int i = 0;i < len;i++)
        {
            now = Next[now][buf[i]-'a'];
            int temp = now;
            while( temp != root )
            {
                if(End[temp])
                    hs[End[temp]]++;
                temp = fail[temp];
            }
        }
        int res1 = *max_element(hs+1, hs+cnt+1);
        printf("%d\n", res1);
        for(int i=1; i<=cnt; i++){
            if(hs[i] == res1){
                printf("%s\n", m2[i].c_str());
            }
        }
    }
};
char str[1001000];
string tmp;
int main(){
    Trie t;
    while(cin>>n&&n){
        m.clear(); m2.clear();
        t.init();
        cnt = 0;
        for(int i=1; i<=n; i++){
            cin>>tmp;
            if(m[tmp]) continue;
            m[tmp] = ++cnt;
            m2[cnt] = tmp;
            t.insert(tmp.c_str(), cnt);
        }
        t.build();
        scanf("%s", str);
        t.query(str); 
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值