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();
    }
     
  • 相关阅读:
    ThinkPHP运算符 与 SQL运算符 对比表
    [Java 8] (6) Lambda与资源管理
    Codeforces Round #275 (Div. 2) C
    HOJ 2245 浮游三角胞(数学啊 )
    [UVALive 6663 Count the Regions] (dfs + 离散化)
    浅解ARC中的 __bridge、__bridge_retained和__bridge_transfer
    SpringMVC: web.xml中声明DispatcherServlet时一定要加入load-on-startup标签
    Unity3d 4.3.4f1执行项目
    更新Windows ActiveX,Ios
    C++11: final与override
  • 原文地址:https://www.cnblogs.com/candy99/p/6368100.html
Copyright © 2011-2022 走看看