zoukankan      html  css  js  c++  java
  • poj 1625 Censored!

    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

    换姿势记路径
    #include<queue>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int bi=1e4,MN=40;
    char c[1000];
    
    struct big{
        int a[MN];
        inline big(){
            memset(a,0,sizeof(a));
            a[0]=1;
        }
        inline void read(){
            register int i,j;
            scanf("%s",c);
            a[0]=(strlen(c)+3)/4;
            for (i=0;i<strlen(c);i++) j=(strlen(c)-i+3)/4,a[j]=a[j]*10+c[i]-48;
        }
        inline void pr(){
            register int i;
            printf("%d",a[a[0]]);
            for (i=a[0]-1;i;i--) printf("%04d",a[i]);
        }
        inline big operator =(int x){
            if (x==0){
                memset(a,0,sizeof(a));
                a[0]=1;
            }
            a[0]=0;
            while (x){
                a[0]++;
                a[a[0]]=x%bi;
                x/=bi;
            }
            if (!a[0]) a[0]=1;
        }
        inline big(int x){
            *this=x;
        }
        inline void gl(){
            while(!a[a[0]]&&a[0]>1) a[0]--;
        }
        inline big operator =(big x){
            register int i;
            a[0]=x.a[0];
            for (i=1;i<=a[0];i++) a[i]=x.a[i];
        }
        inline bool operator >(big y){
            if (a[0]!=y.a[0]) return a[0]>y.a[0];
            for (register int i=a[0];i;i--){
                if (a[i]!=y.a[i]) return a[i]>y.a[i];
            }
            return 0;
        }
        inline bool operator >=(const big y){
            if (a[0]!=y.a[0]) return a[0]>y.a[0];
            for (register int i=a[0];i;i--){
                if (a[i]!=y.a[i]) return a[i]>y.a[i];
            }
            return 1;
        }
        inline bool operator <(big y){
            if (a[0]!=y.a[0]) return a[0]<y.a[0];
            for (register int i=a[0];i;i--){
                if (a[i]!=y.a[i]) return a[i]<y.a[i];
            }
            return 0;
        }
        inline bool operator <=(big y){
            if (a[0]!=y.a[0]) return a[0]<y.a[0];
            for (register int i=a[0];i;i--){
                if (a[i]!=y.a[i]) return a[i]<y.a[i];
            }
            return 1;
        }
        inline bool operator ==(big y){
            if (a[0]!=y.a[0]) return 0;
            for (register int i=a[0];i;i--){
                if (a[i]!=y.a[i]) return 0;
            }
            return 1;
        }
        inline bool operator !=(big y){
            return !(*this==y);
        }
        inline bool operator ==(int y){
            big x=y;
            return *this==x;
        }
        inline bool operator !=(int y){
            return !(*this==y);
        }
        inline void swap(big &a,big &b){
            big x=a;a=b;b=x;
        }
        inline big operator +(big x){
            big r;
            if (a[0]<x.a[0]) r.a[0]=x.a[0];else r.a[0]=a[0];
            for (register int i=1;i<=r.a[0];i++) r.a[i]=a[i]+x.a[i];
            for (register int i=1;i<=r.a[0];i++)
            if (r.a[i]>=bi){
                r.a[i]-=bi;r.a[i+1]++;
                if (i==r.a[0]) r.a[0]++;
            }
            return r;
        }
        inline big operator -(big x){
            if (*this<x) swap(*this,x);
            register int i;
            big r;
            if (a[0]<x.a[0]) r.a[0]=x.a[0];else r.a[0]=a[0];
            for (i=1;i<=r.a[0];i++) r.a[i]=a[i]-x.a[i];
            for (i=1;i<=r.a[0];i++)
            if (r.a[i]<0){
                r.a[i+1]--;r.a[i]+=bi;
            }
            r.gl();
            return r;
        }
        inline big operator *(big y){
            register int i,j;
            big r;r.a[0]=a[0]+y.a[0]-1;
            for (i=1;i<=a[0];i++)
            for (j=1;j<=y.a[0];j++) r.a[i+j-1]+=a[i]*y.a[j];
            for (i=0;i<=r.a[0];i++)
            if (r.a[i]>=bi){
                r.a[i+1]+=r.a[i]/bi;
                r.a[i]%=bi;
                if (i==r.a[0]) r.a[0]++;
            }
            return r;
        }
        inline big half(){
            register int i,j;
            for (i=a[0];i>1;i--) a[i-1]+=(a[i]%2)*bi,a[i]/=2;
            a[1]/=2;
            gl();
            return *this;
        }
        inline big operator /(big y){
            register int i,j;
            big r,l,mid,rq=*this;
            r.a[0]=rq.a[0]+1;r.a[r.a[0]]=1;
            while(r>l){
                mid=(l+r+big(1)).half();
                if (mid*y<=rq) l=mid;else r=mid-big(1);
            }
            return l;
        }
        inline big operator %(big y){
            register int i,j;
            big rq=*this;
            return rq-(rq/y*y);
        }
        inline big operator ^(int y){
            big ans=1;
            big rq=*this;
            while(y){
                if (y&1) ans=ans*rq;
                y>>=1;
                rq=rq*rq;
            }
            return ans;
        }
        inline big operator ^(big y){
            big ans=1;
            big rq=*this;
            while(y!=0){
                if (y.a[1]&1) ans=ans*rq;
                y=y/2;
                rq=rq*rq;
            }
            return ans;
        }
    };
    const int LO=51;
    int nnuu[300];
    inline int f(char u){
        return nnuu[u];
    }
    struct tree{
        int f;
        bool w;
        int t[LO];
        int v[LO];
    }t[101];
    int tt,n,m,p,num=0;
    char s[1000];
    bool ma[105],us[105];
    queue <int> q;
    big dp[51][101];
    inline bool dfs(int x){
        if (x==0) return 1;
        if (t[x].w) return 0;
        if (us[x]) return ma[x];
        us[x]=1;
        return ma[x]=dfs(t[x].f);
    }
    inline void in(){
        int p=0,l,m=strlen(s);
        for (register int i=0;i<m;i++){
            l=f(s[i]);
            if (!t[p].t[l]) t[p].t[l]=++num;
            p=t[p].t[l];
        }
        t[p].w=1;
    }
    inline void mafa(){
        register int i;int k,p;
        q.push(0);t[0].f=0;
        while(!q.empty()){
            k=q.front();q.pop();
            for (i=0;i<n;i++)
            if (t[k].t[i]){
                p=t[k].f;
                while((!t[p].t[i])&&p) p=t[p].f;
                t[t[k].t[i]].f=(k==p)?0:t[p].t[i];
                q.push(t[k].t[i]);
            }
        }
    }
    int main(){
        register int i,j,k;int u;big ans;
        while(~scanf("%d%d%d",&n,&m,&p)){
            num=0;u=0;ans=0;
               scanf("%s",s);
            for (i=0;i<n;i++) nnuu[s[i]]=i;
            for (i=1;i<=p;i++){
                   scanf("%s",s);
                in();
            }
            mafa();
               for (i=0;i<=num;i++)
            ma[i]=dfs(i);
            dp[0][0]=1;
            for (i=0;i<=num;i++)
            if (ma[i])
            for (j=0;j<n;j++){
                if (!t[i].t[j]){
                    u=t[i].f;
                    while(!t[u].t[j]&&u)u=t[u].f;
                    u=t[u].t[j];
                }else u=t[i].t[j];
                t[i].v[j]=u;
            }
            for (i=0;i<m;i++)
            for (j=0;j<=num;j++)
            if (ma[j])
            if (dp[i][j]!=0)
            for (k=0;k<n;k++)
            if (ma[t[j].v[k]]) dp[i+1][t[j].v[k]]=dp[i][j]+dp[i+1][t[j].v[k]];
            for (i=0;i<=num;i++)
            if (ma[i]) ans=ans+dp[m][i];
            ans.pr();printf("
    ");
            for (i=0;i<=m;i++)
            for (j=0;j<=num;j++) dp[i][j]=0;
            for (i=0;i<=num;i++)
            for (j=0;j<n;j++) t[i].t[j]=0;
            for (i=0;i<=num;i++) t[i].w=t[i].f=0;
        }
    }
    View Code
     
  • 相关阅读:
    POJ 1988 Cube Stacking(带权并查集)
    POJ 1414 Life Line(搜索)
    POJ 3468 A Simple Problem with Integers (线段树多点更新模板)
    二叉树前序遍历+中序遍历->后序遍历
    POJ 1061 青蛙的约会(扩展欧几里得)
    XDOJ 1020 ACMer去刷题吧(基础dp)
    POJ 2823 Sliding Window (线段树区间查询)
    线段树学习(一)
    UVA 294 Divisors( 因子分解)
    YYN图论学习路线(推荐)
  • 原文地址:https://www.cnblogs.com/Enceladus/p/5308727.html
Copyright © 2011-2022 走看看