• hdu2222(ac自动机模板)


    #include<iostream>
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int maxn=50*10000+10;
    struct node{
        int son[26],f,v,cnt;
        void rnw(){memset(son,0,sizeof(son));f=v=cnt=0;}
    }ch[maxn];
    char s[100],t[1000010];
    int tot,q[maxn],head,tail,T,n;
    void add(char s[]){
        int u=0,len;
        len=strlen(s);
        for(int i=0;i<len;++i){
            int c=s[i]-'a';
            if(!ch[u].son[c]){ch[u].son[c]=++tot;}
            u=ch[u].son[c];
        }
        ch[u].cnt++;
    }
    void pre(){
        head=tail=0;
        ch[0].f=0;
        for(int i=0;i<26;++i){
            int u=ch[0].son[i];
            if(u){
                ch[u].f=0;
                q[++tail]=u;
            }
        }
        while(head<tail){
            int now=q[++head];
            for(int i=0;i<26;++i){
                int u=ch[now].son[i];
                if(!u){
                    ch[now].son[i]=ch[ch[now].f].son[i];
                    continue;
                }
                q[++tail]=u;
                int v=ch[now].f;
                while(v&&!ch[v].son[i])v=ch[v].f;
                ch[u].f=ch[v].son[i];
            }
        }
    }
    ll solve(char t[]){
        ll res=0;
        int len,u=0;
        len=strlen(t);
        for(int i=0;i<len;++i){
            int c=t[i]-'a';
            u=ch[u].son[c];
            //if(ch[u].cnt){
                int tmp=u;
                while(tmp){
                    if(ch[tmp].cnt>=0){
                        res+=ch[tmp].cnt;
                        ch[tmp].cnt=-1;
                    }
                    else{break;}
                    tmp=ch[tmp].f;
                }
            //}
        }
        return res;
    }
    int main(){
        cin>>T;
        while(T--){
            tot=0;
            for(int i=0;i<=500000;++i)ch[i].rnw();
            scanf("%d",&n);
            for(int i=1;i<=n;++i){
                scanf("%s",s);add(s);
            }
            scanf("%s",t);
            pre();
            printf("%lld
    ",solve(t));
        }
        system("pause");
        return 0;
    }
    /*
    1
    2
    abcdefgh
    efg
    abcdefgh
    */

    PS:那个注释掉的if不能加,这个串就算不是一个结尾也要往前跳,hack数据就写在下面。

  • 相关阅读:
    C#基础知识简单梳理
    knearest neighbor
    二叉查找树的实现
    Unix/Linux 那些系统启动后的进程
    Nginx反向代理IIS
    线程漫谈——线程同步之信号量和互斥量
    BtxCMS@B.T.X 项目及界面展示 [下载]
    MVC in MFC or WTL
    HTTP HTTPS WebService
    ASP.NET WebAPI RC 竟然不支持最常用的json传参
  • 原文地址:https://www.cnblogs.com/dibaotianxing/p/7967053.html
Copyright © 2020-2023  润新知