【模板】AC自动机

传送门

//by sdfzchy
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long LL;
const int inf=(1<<30),N=1000010;
int n,m;

struct AC_automaton
{
    int ch[26],fail,cnt;
}t[N];



inline int in()
{
    char ch=getchar();
    int f=1,tmp=0;
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') {tmp=(tmp<<1)+(tmp<<3)+(ch-'0');ch=getchar();}
    return tmp*f;
}
char s[N];
int ans,cnt;

void ins()
{
    int u=0,v,len=strlen(s);
    for(int i=0;i<len;i++)
    {
        int c=s[i]-'a';
        if(!t[u].ch[c]) t[u].ch[c]=++cnt;
        u=t[u].ch[c];
    }
    t[u].cnt++;
}
queue<int> q;
void gi_fail()
{
    for(int i=0;i<26;i++)
        if(t[0].ch[i])
            q.push(t[0].ch[i]),t[t[0].ch[i]].fail=0;
    while(!q.empty())
    {
        int u=q.front(); q.pop();
        for(int i=0;i<26;i++)
        {
            int &v=t[u].ch[i];
            if(v) t[v].fail=t[t[u].fail].ch[i],q.push(v);
            else v=t[t[u].fail].ch[i];
        }
    }   
}

void gi_sol()
{
    int u=0,v,c,len=strlen(s);
    for(int i=0;i<len;i++)
    {
        c=s[i]-'a';
        u=t[u].ch[c];
        for(int j=u;t[j].cnt!=-1;j=t[j].fail)
        {
            ans+=t[j].cnt;
            t[j].cnt=-1;
        }
    }
}

int main()
{
    n=in();
    for(int i=1;i<=n;i++) scanf("%s",s),ins();
    gi_fail();
    scanf("%s",s);
    gi_sol();
    printf("%d\n",ans);
    return 0;
}

传送门

//by sdfzchy
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long LL;
const int inf=(1<<30),N=1000100;
int n,m;
inline int in()
{
    char ch=getchar();
    int f=1,tmp=0;
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') {tmp=(tmp<<1)+(tmp<<3)+(ch-'0');ch=getchar();}
    return tmp*f;
}

struct AC_atm
{
    int ch[26],fail,id;
}t[N];

char s[200][100];
int tot;

void ins(char *s,int id)
{
    int c,u=0,len=strlen(s);
    for(int i=0;i<len;i++)
    {
        c=s[i]-'a';
        if(!t[u].ch[c]) t[u].ch[c]=++tot;
        u=t[u].ch[c];
    }
    t[u].id=id;
}
queue<int> q;
void gi_fail()
{
    while(!q.empty()) q.pop();
    for(int i=0;i<26;i++)
        if(t[0].ch[i])
            q.push(t[0].ch[i]),t[t[0].ch[i]].fail=0;
    while(!q.empty())
    {
        int u=q.front(); q.pop();
        for(int i=0;i<26;i++)
        {
            int &v=t[u].ch[i];
            if(v) t[v].fail=t[t[u].fail].ch[i],q.push(v);
            else v=t[t[u].fail].ch[i];
        }
    }
}
char S[N];
int ans,cnt[N];

void gi_sol()
{
    int u=0,c,len=strlen(S);
    for(int i=0;i<len;i++)
    {
        c=S[i]-'a';
        u=t[u].ch[c];
        for(int j=u;j;j=t[j].fail)
            if(t[j].id)
                cnt[t[j].id]++;
    }
}

int main()
{
    while(n=in())
    {
        memset(t,0,sizeof(t));
        memset(cnt,0,sizeof(cnt)); 
        ans=0;
        for(int i=1;i<=n;i++) scanf("%s",s[i]),ins(s[i],i);
        scanf("%s",S);
        gi_fail();
        gi_sol();
        for(int i=1;i<=n;i++) ans=max(ans,cnt[i]);
        printf("%d\n",ans);
        for(int i=1;i<=n;i++)
            if(cnt[i]==ans)
                printf("%s\n",s[i]);
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值