这题,真心木啥意思,就是数据里貌似字符有负数,注意gets读入。。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 #include <algorithm> 6 #include <cstdlib> 7 using namespace std; 8 int trie[201][50]; 9 int dp[51][201][201]; 10 int o[301]; 11 int que[201]; 12 int fail[201]; 13 int id[1001]; 14 int t,N; 15 void CL() 16 { 17 t = 1; 18 memset(trie,-1,sizeof(trie)); 19 memset(dp,0,sizeof(dp)); 20 memset(o,0,sizeof(o)); 21 memset(id,0,sizeof(id)); 22 } 23 void insert(char *str) 24 { 25 int len,i,root; 26 len = strlen(str); 27 root = 0; 28 for(i = 0;i < len;i ++) 29 { 30 if(trie[root][id[str[i]+200]] == -1) 31 trie[root][id[str[i]+200]] = t ++; 32 root = trie[root][id[str[i]+200]]; 33 } 34 o[root] = 1; 35 } 36 void build_ac() 37 { 38 int front,tail,head,i; 39 head = tail = 0; 40 for(i = 0;i < N;i ++) 41 { 42 if(trie[0][i] != -1) 43 { 44 fail[trie[0][i]] = 0; 45 que[tail++] = trie[0][i]; 46 } 47 else 48 { 49 trie[0][i] = 0; 50 } 51 } 52 while(head != tail) 53 { 54 front = que[head++]; 55 if(o[fail[front]]) 56 o[front] = 1; 57 for(i = 0;i < N;i ++) 58 { 59 if(trie[front][i] != -1) 60 { 61 que[tail++] = trie[front][i]; 62 fail[trie[front][i]] = trie[fail[front]][i]; 63 } 64 else 65 { 66 trie[front][i] = trie[fail[front]][i]; 67 } 68 } 69 } 70 } 71 int main() 72 { 73 int M,P,i,j,k,u; 74 int ans[201]; 75 char ch[51]; 76 while(scanf("%d%d%d%*c",&N,&M,&P)!=EOF) 77 { 78 CL(); 79 gets(ch); 80 for(i = 0;i < N;i ++) 81 { 82 id[ch[i]+200] = i; 83 } 84 for(i = 0;i < P;i ++) 85 { 86 gets(ch); 87 insert(ch); 88 } 89 build_ac(); 90 dp[0][0][0] = 1; 91 for(i = 0;i < M;i ++) 92 { 93 for(j = 0;j < t;j ++) 94 { 95 if(o[j]) continue; 96 for(k = 0;k < N;k ++) 97 { 98 if(o[trie[j][k]]) continue; 99 for(u = 0;u < 200;u ++) 100 { 101 dp[i+1][trie[j][k]][u] += dp[i][j][u]; 102 } 103 for(u = 0;u < 200;u ++) 104 { 105 if(dp[i+1][trie[j][k]][u] > 9) 106 { 107 dp[i+1][trie[j][k]][u+1] += dp[i+1][trie[j][k]][u]/10; 108 dp[i+1][trie[j][k]][u] = dp[i+1][trie[j][k]][u]%10; 109 } 110 } 111 } 112 } 113 } 114 memset(ans,0,sizeof(ans)); 115 for(i = 0;i < t;i ++) 116 { 117 for(u = 0;u < 200;u ++) 118 { 119 ans[u] += dp[M][i][u]; 120 } 121 for(u = 0;u < 200;u ++) 122 { 123 if(ans[u] > 9) 124 { 125 ans[u+1] += ans[u]/10; 126 ans[u] = ans[u]%10; 127 } 128 } 129 } 130 int flag = 0; 131 for(i = 200;i >= 0;i --) 132 { 133 if(ans[i] > 0) 134 flag = 1; 135 if(flag) 136 printf("%d",ans[i]); 137 } 138 if(flag == 0) 139 printf("0 "); 140 else 141 printf(" "); 142 } 143 return 0; 144 }