标签:
Description
Input
Output
Sample Input
4 3 AT AC AG AA
Sample Output
36
代码:
1 #include <cstdio> 2 #include <cstring> 3 struct Matrix { 4 int a[110][110]; 5 } mat, ti; 6 int n, m, len, last, tn, ans, f[110], t[110][4], v[110]; 7 char str[11]; 8 void BFS () 9 { 10 int q[110], hd, tl; 11 for (q[hd = tl = 0] = 0; hd <= tl; hd++) 12 for (int i = 0; i < 4; i++) 13 t[q[hd]][i] ? (q[hd] ? f[t[q[hd]][i]] = t[f[q[hd]]][i] : 0) ,v[t[q[hd]][i]] |= v[f[t[q[hd]][i]]], q[++tl] = t[q[hd]][i] : t[q[hd]][i] = t[f[q[hd]]][i]; 14 } 15 inline Matrix times (Matrix m1, Matrix m2) 16 { 17 Matrix ret; 18 memset (ret.a, 0, sizeof (ret.a)); 19 for (int i = 0; i <= tn; i++) 20 for (int j = 0; j <= tn; j++) 21 for (int k = 0; k <= tn; ret.a[i][j] %= 100000, k++) 22 ret.a[i][j] += (long long) m1.a[i][k] * m2.a[k][j] % 100000; 23 return ret; 24 } 25 inline int gc (char ch) 26 { 27 return ch == ‘A‘ ? 0 : ch == ‘C‘ ? 1 : ch == ‘T‘ ? 2 : 3; 28 } 29 int main () 30 { 31 scanf ("%d %d", &n, &m); 32 for (int i = 0; i < n; i++) 33 { 34 scanf ("%s", (char*) &str); 35 len = strlen (str); 36 last = 0; 37 for (int j = 0, ch = gc (str[j]); j < len; ch = gc (str[++j])) 38 last = t[last][ch] ? t[last][ch] : t[last][ch] = ++tn; 39 v[last] = 1; 40 } 41 BFS (); 42 for (int i = 0; i <= tn; i++) 43 for (int j = 0; j < 4; j++) 44 (!v[t[i][j]]) && !v[i] ? mat.a[i][t[i][j]]++ : 0; 45 for (int i = 0; i <= tn; i++) ti.a[i][i] = 1; 46 for (; m; m >>= 1) 47 (m & 1 ? ti = times (ti, mat) : ti), mat = times (mat, mat); 48 for (int i = 0; i <= tn; i++) ans += ti.a[0][i]; 49 printf ("%d", ans % 100000); 50 }
标签:
原文地址:http://www.cnblogs.com/lightning34/p/4309612.html