1. 程式人生 > >[POJ1625]Censored!(AC自動機+DP+高精度)

[POJ1625]Censored!(AC自動機+DP+高精度)

open safe tput wid word .org cst gpo source

Censored!
Time Limit: 5000MS Memory Limit: 10000K
Total Submissions: 10824 Accepted: 2966

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

同HDU2222,只是需要高精度

感覺可能有個問題,就是這題題目沒有規定屏蔽詞包含了所有字母,但是好像對解題沒有什麽影響。

代碼用時:1.5h 高精度輸出寫炸了

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define rep(i,l,r) for (int i=l; i<=r; i++)
 5 using namespace std;
 6 
 7 const int N=110;
 8 int n,m,p,nd,fail[N],b[N],q[N],c[N][N];
 9 char s[N],S[N];
10 
11 int get(char ch){ rep(i,1,n) if (S[i]==ch) return i; return -1; }
12 
13 void ins(char S[]){
14     int x=0,len=strlen(s+1);
15     rep(i,1,len){
16         int k=get(s[i]);
17         if (!c[x][k]) ++nd,c[x][k]=nd;
18         x=c[x][k];
19     }
20     b[x]=1;
21 }
22 
23 void getfail(){
24     int st=0,ed=0;
25     rep(i,1,n) if (c[0][i]) q[++ed]=c[0][i];
26     while (st!=ed){
27         int x=q[++st];
28         rep(i,1,n)
29             if (!c[x][i]) c[x][i]=c[fail[x]][i];
30                 else q[++ed]=c[x][i],fail[c[x][i]]=c[fail[x]][i];
31         b[x]|=b[fail[x]];
32     }
33 }
34 
35 struct D{
36     int v[N],len;
37     D(int x=0){
38         memset(v,0,sizeof(v));
39         for (len=0; x>0; x/=10) v[len++]=x%10;
40         len--;
41     }
42     D operator +(const D &a){
43         D ans;
44         ans.len=max(len,a.len);
45         for (int i=0; i<=ans.len; i++){
46             ans.v[i]+=v[i]+a.v[i]; ans.v[i+1]+=ans.v[i]/10; ans.v[i]%=10;
47         }
48         while (ans.v[ans.len+1]) ans.len++;
49         return ans;
50     }
51     void print(){
52         if (len==-1) { printf("%d\n",0); return; }
53         for (int i=len; ~i; i--) printf("%d",v[i]);
54         printf("\n");
55     }
56 }dp[52][N];
57 
58 int check(int k,int j){ int ans=0; rep(i,1,n) if (c[k][i]==j) ans++; return ans; }
59 
60 int main(){
61     freopen("poj1625.in","r",stdin);
62     freopen("poj1625.out","w",stdout);
63     scanf("%d%d%d",&n,&m,&p); scanf("%s",S+1);
64     rep(i,1,p) scanf("%s",s+1),ins(s);
65     getfail(); dp[0][0]=D(1);
66     rep(i,1,m) rep(j,0,nd) if (!b[j])
67         rep(k,0,nd) if (!b[k])
68             for (int ti=check(k,j); ti--; ) dp[i][j]=dp[i][j]+dp[i-1][k];
69     D ans; rep(i,0,nd) ans=ans+dp[m][i];
70     ans.print();
71     return 0;
72 }

[POJ1625]Censored!(AC自動機+DP+高精度)