zoukankan      html  css  js  c++  java
  • POJ 1625 Censored! [AC自动机 高精度]

    Censored!
    Time Limit: 5000MS   Memory Limit: 10000K
    Total Submissions: 9793   Accepted: 2686

    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
    

    Source

    Northeastern Europe 2001, Northern Subregion

    题意:

    给出p个模式串,求有多少个长度为m的字符串,其中不包含任何一个模式串为子串


    和BZOJ1030一样啊
    字符集未知,所以用个映射
    可恶一开始映射写错了(雾?现在我还不知道为什么错 反正用mp[s[i]]=i就对了
    然后不准取模还要用高精.....
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int N=105,M=55,L=30,B=1e4;
    inline int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
        return x*f;
    }
    int n,m,p,mp[300];
    char s[N];
    struct node{
        int ch[55],fail,val;
    }t[N];
    int sz;
    void ins(char s[]){
        int u=0,n=strlen(s+1);
        for(int i=1;i<=n;i++){
            int c=mp[s[i]];
            if(!t[u].ch[c]) t[u].ch[c]=++sz;
            u=t[u].ch[c];
        }
        t[u].val=1;
    }
    int q[N],head,tail;
    void getAC(){
        head=tail=1;
        for(int i=1;i<=mp[0];i++) if(t[0].ch[i]) q[tail++]=t[0].ch[i];
        while(head!=tail){
            int u=q[head++];
            t[u].val|=t[t[u].fail].val;
            for(int i=1;i<=mp[0];i++){
                int &v=t[u].ch[i];
                if(!v) v=t[t[u].fail].ch[i];
                else{
                    t[v].fail=t[t[u].fail].ch[i];
                    q[tail++]=v;
                }
            }
        }
    }
    
    struct big{
        int size,d[L];
        big():size(1){memset(d,0,sizeof(d));}
        bool has(){return size>1||(size==1&&d[1]!=0);}
    };
    big operator +(big a,big b){
        int g=0,i;
        for(i=1;;i++){
            if(g==0&&i>a.size&&i>b.size) break;
            int t=g;
            t+=i<=a.size?a.d[i]:0;
            t+=i<=b.size?b.d[i]:0;
            a.d[i]=t%B;
            g=t/B;
        }
        a.size=i-1;
        return a;
    }
    void print(big &a){
        printf("%d",a.d[a.size]);
        for(int i=a.size-1;i>=1;i--){
            if(a.d[i]<10) printf("000");
            else if(a.d[i]<100) printf("00");
            else if(a.d[i]<1000) printf("0");
            printf("%d",a.d[i]);
        }
        putchar('
    ');
    }
    
    big f[M][N],ans;
    void dp(){
        f[0][0].d[1]=1;
        for(int i=0;i<m;i++)
            for(int j=0;j<=sz;j++) if(!t[j].val&&f[i][j].has())
                for(int k=1;k<=mp[0];k++) if(!t[t[j].ch[k]].val)
                    f[i+1][t[j].ch[k]]=f[i+1][t[j].ch[k]]+f[i][j];
        for(int i=0;i<=sz;i++) ans=ans+f[m][i];
        print(ans);
    }
    int main(){
        freopen("in","r",stdin);
        n=read();m=read();p=read();
        scanf("%s",s+1);
        for(int i=1;i<=n;i++) mp[s[i]]=i;mp[0]=n;
        for(int i=1;i<=p;i++) scanf("%s",s+1),ins(s);
        //for(int i=1;i<=n;i++) printf("mp %c %d
    ",s[i],mp[s[i]]);
        getAC();
        dp();
    }
     
  • 相关阅读:
    JAVA 8的新特性
    JAVA中map的分类和各自的特性
    设计模式之工厂方法模式(附实例代码下载)
    为什么重写equals还要重写hashcode??
    C# static的用法详解
    1-RadioButton控件的用法
    C#三种常用的读取XML文件的方法
    VS2017 中安装SVN
    pip安装selenium时,报错“You are using pip version 10.0.1, however version 18.0 is available.”的问题
    问题:unknown error: call function result missing 'value' 解决方法
  • 原文地址:https://www.cnblogs.com/candy99/p/6368100.html
Copyright © 2011-2022 走看看