zoukankan      html  css  js  c++  java
  • poj1625 ac自动机

    Censored!
    Time Limit: 5000MS   Memory Limit: 10000K
    Total Submissions: 8102   Accepted: 2191

    Description

    The alphabet of Freeland consists of exactly N letters. Each sentence of Freeland language (also known as Freish) consists of exactly M letters without word breaks. So, there exist exactly N^M different Freish sentences. 

    But after recent election of Mr. Grass Jr. as Freeland president some words offending him were declared unprintable and all sentences containing at least one of them were forbidden. The sentence S contains a word W if W is a substring of S i.e. exists such k >= 1 that S[k] = W[1], S[k+1] = W[2], ...,S[k+len(W)-1] = W[len(W)], where k+len(W)-1 <= M and len(W) denotes length of W. Everyone who uses a forbidden sentence is to be put to jail for 10 years. 

    Find out how many different sentences can be used now by freelanders without risk to be put to jail for using it. 

    Input

    The first line of the input file contains three integer numbers: N -- the number of letters in Freish alphabet, M -- the length of all Freish sentences and P -- the number of forbidden words (1 <= N <= 50, 1 <= M <= 50, 0 <= P <= 10). 

    The second line contains exactly N different characters -- the letters of the Freish alphabet (all with ASCII code greater than 32). 

    The following P lines contain forbidden words, each not longer than min(M, 10) characters, all containing only letters of Freish alphabet. 

    Output

    Output the only integer number -- the number of different sentences freelanders can safely use.

    Sample Input

    2 3 1
    ab
    bb
    

    Sample Output

    5
    

    在AC自动机上dp,dp[i][j]表示走了i位,在ac自动机上第j个位置的方案数。ans比较大,要用高精度。

    据说有些鬼畜的数据。所以不要用s[i]-32,用hash比较好

    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    
    using namespace std;
    
    struct node{
        node *fail;
        node *son[100];
        int num;
        bool ar;
        node()
        {
            fail=NULL;
            ar=true;
            num=0;
            for(int i=0;i<100;i++)son[i]=NULL;
        }
    } *que[1011];
    node *root;
    char s[1011];
    int len,n,m,t,i,tot;
    node *ac[1011];
    struct ans{
        int n;
        int a[101];
    }dp[61][601];
    ans xzq;
    int hash[601];
    
    ans plus(ans a,ans b)
    {
        ans c;
        int i;
        for(i=0;i<=100;i++)c.a[i]=0;
        if(a.n>b.n)c.n=a.n;
        else c.n=b.n;
        for(i=1;i<=c.n;i++){
            c.a[i]=c.a[i]+a.a[i]+b.a[i];
            if(c.a[i]>=10){
                c.a[i+1]=c.a[i+1]+c.a[i]/10;
                c.a[i]=c.a[i]%10;
            }
        }
        c.n++;
        if(c.a[c.n]==0)c.n--;
        return c;
    }
    
    void Read()
    {
        len=0;
        char c;
        while(c=getchar(),c=='
    ');
        s[++len]=c;
        while(c=getchar(),c!='
    ')s[++len]=c;
    }
    
    void add()
    {
        int i,ws;
        node *p;
        p=root;
        for(i=1;i<=len;i++){
            ws=hash[s[i]];
            if(p->son[ws]==NULL){
                p->son[ws]=new node();
                p->son[ws]->num=++tot;
                ac[tot]=p->son[ws];
            }
            p=p->son[ws];
        }
        p->ar=false;
    }
    
    void Bfs()
    {
        int l,r,i,j,k;
        node *p;
        node *q;
        que[l=r=1]=root;
        while(l<=r){
            p=que[l];
            for(i=1;i<=n;i++)if(p->son[i]!=NULL){
                r++;
                que[r]=p->son[i];
                if(p==root)p->son[i]->fail=root;
                else{
                    q=p->fail;
                    while(q!=NULL){
                        if(q->son[i]!=NULL){
                            p->son[i]->fail=q->son[i];
                            break;
                        }
                        q=q->fail;
                    }
                    if(p->son[i]->fail==NULL)p->son[i]->fail=root;
                }
            }
            l++;
        }
        for(i=2;i<=r;i++)que[i]->ar&=que[i]->fail->ar;
    }
    
    void Main()
    {
        int i,j,k;
        node *p;
        node *q;
        dp[0][1].n=1;
        dp[0][1].a[1]=1;
        for(i=0;i<m;i++){
            for(j=1;j<=tot;j++){
                for(k=1;k<=n;k++){
                    p=ac[j];
                    if(p->son[k]!=NULL)p=p->son[k];
                    else{
                        q=p->fail;
                        while(q!=NULL){
                            if(q->son[k]!=NULL){
                                p=q->son[k];
                                break;
                            }
                            q=q->fail;
                        }
                        if(q==NULL)p=root;
                    }
                    if(p->ar==true)dp[i+1][p->num]=plus(dp[i+1][p->num],dp[i][j]);
                }
            }
        }
        for(i=1;i<=tot;i++)xzq=plus(xzq,dp[m][i]);
    }
    
    int main()
    {
        tot=0;
        root=new node();
        root->num=++tot;
        ac[tot]=root;
        scanf("%d%d%d",&n,&m,&t);
        Read();
        for(i=1;i<=len;i++)hash[s[i]]=i;
        for(i=1;i<=t;i++){
            Read();
            add();
        }
        Bfs();
        Main();
        if(xzq.n==0)printf("0
    ");
        else{
            for(i=xzq.n;i>=1;i--)printf("%d",xzq.a[i]);
            printf("
    ");
        }
    }
  • 相关阅读:
    【1】BIO与NIO、AIO的区别
    BIO | NIO | AIO (Java版)
    Java NIO 机制分析(一) Java IO的演进
    AbstractQueuedSynchronizer同步队列与Condition等待队列协同机制
    【1】【JUC】Condition和生产者消费者模型
    【JUC】JDK1.8源码分析之CyclicBarrier
    常用排序,查找,树算法集锦
    AFNetworking
    ios推送服务,php服务端
    如何使用subversion管理iOS源代码
  • 原文地址:https://www.cnblogs.com/applejxt/p/3956489.html
Copyright © 2011-2022 走看看