POJ1625 Censored!

我只想说一句WCNMLGB 专业WA自动机!

改了3天。。最后。。我把标程大改。。改到和我的几乎一模一样的时候。。AC了。。但是我的程序依然WA。。RNMB

Censored!
Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 8184   Accepted: 2211

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
 
 1 #include <cstdio>
 2 #include <map>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <queue>
 6 using namespace std;
 7 #define NODE 110
 8 #define For(i,n) for(int i=1;i<=n;i++)
 9 #define For0(i,n) for(int i=0;i<n;i++)
10 int n,L,m;
11 map<char,int> hash;
12 char ch[51];
13 struct bign{
14     int A[300],len;
15     bign(){memset(A,0,sizeof(A));len=1;}
16     void Print(){
17         for(int i=len;i>=1;i--)   printf("%d",A[i]);
18         puts("");
19     }
20 }dp[51][NODE];
21 struct trie{
22     int sz,ch[NODE][50];
23     int next[NODE],Q[NODE*3];
24     bool fn[NODE];
25     void init(){sz=0;}
26     void insert(char st[]){
27         int len=strlen(st),cur=0;
28         For0(i,len){
29             int id=hash[st[i]];
30             if(!ch[cur][id]) {
31                 ch[cur][id]=++sz;
32             }
33             cur=ch[cur][id];
34         }
35         fn[cur]=1;
36     }
37     void BuildNext(){
38         int l=0,r=0;
39         For0(i,n) if(ch[0][i]) Q[++r]=ch[0][i];
40         while(l<r){
41             int cur=Q[++l];
42             For0(i,n)
43               if(ch[cur][i]){
44                   Q[++r]=ch[cur][i];
45                   next[ch[cur][i]]=ch[next[cur]][i];
46                   if(fn[next[ch[cur][i]]]) fn[ch[cur][i]]=1;
47               }
48               else ch[cur][i]=ch[next[cur]][i];
49         }
50     }
51 }ac;    
52 
53 bign operator + (bign A,bign B){
54     bign C;C.len=max(A.len,B.len);
55     for(int i=1;i<=C.len;i++){ 
56         C.A[i]+=A.A[i]+B.A[i];
57         C.A[i+1]=C.A[i]/10;
58         C.A[i]%=10;
59     }
60     if(C.A[C.len+1]) C.len++;
61     return C;
62 }
63 
64 void DP(){
65     dp[0][0].A[1]=1;
66     For(i,L)
67         For0(j,ac.sz+1){
68             if(ac.fn[j])   continue;
69             For0(k,n){
70                 int cur=ac.ch[j][k];
71                 if(ac.fn[cur])   continue;
72                 dp[i][cur]=dp[i-1][j]+dp[i][cur];
73             }
74         }
75     bign ans;
76     For0(i,ac.sz+1) if(!ac.fn[i])  ans=ans+dp[L][i];
77     ans.Print();
78 }
79 
80 int main(){
81     char s[51];
82     hash.clear();
83     scanf("%d%d%d",&n,&L,&m);getchar();gets(ch);
84     For0(i,n) hash[ch[i]]=i;
85     ac.init();
86     For0(i,m){
87         gets(s);
88         ac.insert(s);
89     }
90     ac.BuildNext();
91     DP();
92     return 0;
93 }
AC
  1 #include<set>
  2 #include<map>
  3 #include<cmath>
  4 #include<queue>
  5 #include<vector>
  6 #include<cstdio>
  7 #include<cstdlib>
  8 #include<cstring>
  9 #include<iostream>
 10 #include<algorithm>
 11 using namespace std;
 12 const int N = 60;
 13 #define clr(a,b) memset(a,b,sizeof(a));
 14 #define For(i,n) for(int i=1;i<=n;i++)
 15 #define For0(i,n) for(int i=0;i<n;i++)
 16 #define Rep(i,l,r) for(int i=l;i<=r;i++)
 17 #define Down(i,r,l) for(int i=r;i>=l;i--)
 18 #define debug printf("TTTTTTTTTT_______TTTTTTTTTT
");
 19 map<char,int> hash;
 20 char T[N],st[N];
 21 int n,m,p;
 22 
 23 struct bign{
 24     int A[300],len;
 25     bign(){clr(A,0);len=1;}
 26     void Print(){
 27         Down(i,len,1)   printf("%d",A[i]);
 28         puts("");
 29     }
 30 }dp[N][N],ans;
 31 
 32 struct trie{
 33     int sz,ch[N*10][N],next[N*10],Q[N*10],fn[N*10];
 34     void init(){sz=0;}
 35     void insert(char st[]){
 36         int len=strlen(st),cur=0;
 37         For0(i,len){
 38             int id=hash[st[i]];
 39             if(!ch[cur][id]) {
 40                 ch[cur][id]=++sz;
 41             }
 42             cur=ch[cur][id];
 43         }
 44         fn[cur]=1;
 45     }
 46     void BuildNext(){
 47         int l=0,r=0;
 48         For0(i,n) if(ch[0][i]) Q[++r]=ch[0][i];
 49         while(l<r){
 50             int cur=Q[++l];
 51             For0(i,n)
 52               if(ch[cur][i]){
 53                   Q[++r]=ch[cur][i];
 54                   next[ch[cur][i]]=ch[next[cur]][i];
 55                   if(fn[next[ch[cur][i]]]) fn[ch[cur][i]]=1;
 56               }
 57               else ch[cur][i]=ch[next[cur]][i];
 58         }
 59    }
 60 }ac;
 61 
 62 bign operator + (bign A,bign B){
 63     bign C;C.len=max(A.len,B.len);
 64     for(int i=1;i<=C.len;i++){ 
 65         C.A[i]+=A.A[i]+B.A[i];
 66         C.A[i+1]=C.A[i]/10;
 67         C.A[i]%=10;
 68     }
 69     if(C.A[C.len+1]) C.len++;
 70     return C;
 71 }
 72 
 73 void DP(){
 74     dp[0][0].A[1]=1;
 75     For(i,m)
 76         For0(j,ac.sz+1){
 77             if(ac.fn[j])   continue;
 78             For0(k,n){
 79                 int cur=ac.ch[j][k];
 80                 if(ac.fn[cur])   continue;
 81                 dp[i][cur]=dp[i-1][j]+dp[i][cur];
 82             }
 83         }
 84     For0(i,ac.sz+1) if(!ac.fn[i])  ans=ans+dp[m][i];
 85     ans.Print();
 86 }
 87 
 88 int main(){
 89     char s[51];
 90     hash.clear();
 91     scanf("%d%d%d",&n,&m,&p);getchar();gets(T);
 92     For0(i,n) hash[T[i]]=i;
 93     ac.init();
 94     For0(i,p){
 95         gets(s);
 96         ac.insert(s);
 97     }
 98     ac.BuildNext();
 99     DP();
100     return 0;
101 }
WA
原文地址:https://www.cnblogs.com/zjdx1998/p/3982602.html