POJ 1625 Censored!

AC自动机+高精度DP 

trick:一个串可能包含另一个串。。。。。

4 3 2
ACGT
TCT
C

答案应该是27!!!


Censored!
Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 7557   Accepted: 2042

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

[Submit]   [Go Back]   [Status]   [Discuss]



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>

using namespace std;

const int maxn=210;

int ch[maxn][75],fail[maxn],end[maxn];
int root,sz;
char str[11000];
int N,M,P;
map<char,int> mp;

int newnode()
{
    memset(ch[sz],-1,sizeof(ch[sz]));
    end[sz++]=0;
    return sz-1;
}

void ac_init()
{
    sz=0;
    root=newnode();
}

void ac_insert(char str[])
{
    int len=strlen(str);
    int now=root;
    for(int i=0;i<len;i++)
    {
        if(ch[now][mp[str[i]]]==-1)
            ch[now][mp[str[i]]]=newnode();
        now=ch[now][mp[str[i]]];
    }
    end[now]++;
}

void ac_build()
{
    queue<int> q;
    fail[root]=root;
    for(int i=0;i<N;i++)
    {
        if(ch[root][i]==-1) ch[root][i]=root;
        else
        {
            fail[ch[root][i]]=root;
            q.push(ch[root][i]);
        }
    }
    while(!q.empty())
    {
       int  now=q.front(); q.pop();
       end[now]+=end[fail[now]];
       for(int i=0;i<N;i++)
       {
           if(ch[now][i]==-1) ch[now][i]=ch[fail[now]][i];
           else
           {
               fail[ch[now][i]]=ch[fail[now]][i];
               q.push(ch[now][i]);
           }
       }
    }
}

struct BigInt
{
    const static int mod=10000;
    const static int dlen=4;
    int a[110],len;

    BigInt()
    {
        memset(a,0,sizeof(a));
        len=1;
    }

    BigInt(int v)
    {
        memset(a,0,sizeof(a));
        len=0;
        do
        {
            a[len++]=v%mod;
            v/=mod;
        }while(v);
    }

    BigInt operator + (const BigInt &b) const
    {
        BigInt res;
        res.len=max(len,b.len);
        for(int i=0;i<=res.len;i++)
        {
            res.a[i]=0;
        }
        for(int i=0;i<res.len;i++)
        {
            res.a[i]+=((i<len)?a[i]:0)+((i<b.len)?b.a[i]:0);
            res.a[i+1]+=res.a[i]/mod;
            res.a[i]%=mod;
        }
        if(res.a[res.len]>0) res.len++;
        return res;
    }

    void output()
    {
        printf("%d",a[len-1]);
        for(int i=len-2;i>=0;i--)
            printf("%04d",a[i]);
        printf("\n");
    }
}dp[110][110];

void ac_solve()
{
    for(int i=0;i<=M+1;i++)
    {
        for(int j=0;j<=sz;j++)
        {
            dp[i][j]=BigInt(0);
        }
    }
    dp[0][0]=BigInt(1);
    for(int i=0;i<M;i++)
    {
        for(int j=0;j<sz;j++)
        {
            for(int k=0;k<N;k++)
            {
                int p=ch[j][k];
                if(end[p]) continue;
                dp[i+1][p]=dp[i+1][p]+dp[i][j];
            }
        }
    }
    BigInt ans=BigInt(0);
    for(int i=0;i<sz;i++) ans=ans+dp[M][i];
    ans.output();
}

int main()
{
    while(scanf("%d%d%d",&N,&M,&P)!=EOF)
    {
        getchar();
        gets(str);
        ac_init(); mp.clear();
        for(int i=0;i<N;i++)
            mp[str[i]]=i;
        for(int i=0;i<P;i++)
        {
            gets(str);
            ac_insert(str);
        }
        ac_build();
        ac_solve();
    }
    return 0;
}



你可能感兴趣的:(POJ 1625 Censored!)