POJ 1625 Censored! (AC自动机+DP+高精度,5级)

G - Censored!
Crawling in process... Crawling failed Time Limit:5000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Appoint description:

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,当然需要高精度。

失误:巨坑,char -127-128 用数组HASH RE啊,用MAP吧

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<map>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int msize=109;
const int sig=251;
map<char,int>has;
//int has[257];
class Matrix
{ public:
  int f[msize][msize],n;
  Matrix(){}
  Matrix(int x)
  { n=x;
    FOR(i,0,n-1)FOR(j,0,n-1)
    f[i][j]=0;
  }
  void out()
  {
    FOR(i,0,n-1)FOR(j,0,n-1)
    printf("%d%c",f[i][j],j==n-1?'\n':' ');
  }
};
class AC_Machine
{
  public:int f[msize],ch[msize][sig],sz,val[msize],pos;
  void clear()
  { has.clear();
    pos=0;
    clr(ch[0],0);sz=1;val[0]=0;
  }
  int idx(char z)
  {
   // if(has[z]!=-1)return has[z];
   // has[z]=pos++;
    return has[z];
  }
  void insert(char*s,int v)
  {
    int u=0,c;
    for(int i=0;s[i];++i)
    {
      c=idx(s[i]);
      if(!ch[u][c])
      {
        clr(ch[sz],0);val[sz]=0;
        ch[u][c]=sz++;
      }
      u=ch[u][c];
    }
    val[u]=v;
  }
  void getFail()
  {
    int u,v,r;
    queue<int>Q;
    FOR(i,0,pos-1)
    { u=ch[0][i];
      if(u)
      {
        Q.push(u);f[u]=0;
      }
    }
    while(!Q.empty())
    {
      r=Q.front();Q.pop();
      val[r]|=val[ f[r] ];
      FOR(c,0,pos-1)
      {
        u=ch[r][c];
        if(!u)
        {
          ch[r][c]=ch[ f[r] ][c];continue;
        }
        Q.push(u);
        v=f[r];
        while(v&&!ch[v][c])v=f[v];
        f[u]=ch[v][c];
      }
    }
  }
  Matrix getMatrix()
  {
    Matrix ret=Matrix(sz);
    FOR(i,0,sz-1)
    FOR(j,0,pos-1)
    if(!val[ ch[i][j] ])//合法
    ret.f[i][ ch[i][j] ]++;
    return ret;
  }
};
class BigInt
{
 public:
 const static int mod=10000,Dlen=4;
  int f[601],len;
  BigInt()
  {
    clr(f,0);len=1;
  }
  BigInt(int x)
  {
    clr(f,0);
    len=0;
    do
    {
      f[len++]=x%mod;x/=mod;
    }while(x);
  }
  BigInt operator+(const BigInt&b)const
  {
    BigInt c;
    c.len=max(b.len,len);
    FOR(i,0,c.len)c.f[i]=0;
    FOR(i,0,c.len-1)
    {
      c.f[i]+=(i<len?f[i]:0)+(i<b.len?b.f[i]:0);
      c.f[i+1]+=c.f[i]/mod;
      c.f[i]%=mod;
    }
    if(c.f[c.len]>0)c.len++;
    //c.out();
    return c;
  }
  BigInt operator*(const BigInt&b)const
  {
    BigInt c;
    FOR(i,0,len-1)
    {
      int up=0;//进位
      FOR(j,0,b.len-1)
      {
        int tmp=f[i]*b.f[j]+c.f[i+j]+up;//+up;
        c.f[i+j]=tmp%mod;
        up=tmp/mod;
      }
      if(up)
      {
        c.f[i+b.len]=up;
      }
    }
    c.len=len+b.len;
    while(c.f[ c.len-1 ]==0&&c.len>1)c.len--;
    return c;
  }
  void check()
  {
    if(f[len-1]==0&&len>1)puts("yes");
  }
  void out()
  { printf("%d",f[len-1]);
    for(int i=len-2;i>=0;--i)
      printf("%04d",f[i]);
    printf("\n");
  }
};
void getdata(char*s)
{ int pos=0;
  while(1)
  {
    char z=getchar();
    if(z=='\n'){s[pos]='\0';return;}
    s[pos++]=z;
  }
}
AC_Machine ac;
char s[99];
BigInt dp[2][110];
int main()
{
  int n,m,p;
  while(~scanf("%d%d%d",&n,&m,&p))
  {
    ac.clear();
    getdata(s);
    getdata(s);
    //scanf("%s",s); 有空格啊亲
    for(int i=0;s[i];++i)
      has[s[i]]=i;
      ac.pos=strlen(s);
      //ac.idx(s[i]);
    FOR(i,1,p)
    {
      getdata(s);
      ac.insert(s,1);
    }
    ac.getFail();
    Matrix ret=ac.getMatrix();
   // ret.out();
    int now=0,then;
    dp[now][0]=1;
    FOR(i,1,ret.n-1)
    dp[now][i]=0;
    FOR(i,0,m-1)
    {
      then=now^1;
      FOR(j,0,ret.n-1)
      dp[then][j]=0;
      FOR(j,0,ret.n-1)
      FOR(k,0,ret.n-1)
      if(ret.f[j][k]>0)
        dp[then][k]=dp[then][k]+dp[now][j]*ret.f[j][k];
     now=then;
    }
    BigInt ans=0;
    FOR(i,0,ret.n-1)
    ans=ans+dp[then][i];
    ans.out();
  }
  return 0;
}


posted @ 2013-08-29 21:50  剑不飞  阅读(188)  评论(0编辑  收藏  举报