1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| #include <cstdio> #include <queue> #include <cstring> using namespace std; const int N = 6e3 + 10, M = 110, mod = 1e4 + 7; char s[N]; int n, m, f[N][M]; int idx, cnt[N], nxt[N], tr[N][26]; int binpow (int b, int k) { int res = 1; while (k) { if (k & 1) res = res * b % mod; b = b * b % mod; k >>= 1; } return res; } void insert (int len, char *str) { int p = 0; for (int i = 1; i <= len; i++) { int &t = tr[p][str[i] - 'A']; !t && (t = ++idx); p = t; } cnt[p]++; } void build () { queue <int> q; for (int i = 0; i < 26; i++) tr[0][i] && (q.push(tr[0][i]), 0); while (!q.empty()) { int t = q.front(); q.pop(); for (int i = 0; i < 26; i++) { int &p = tr[t][i]; if (!p) p = tr[nxt[t]][i]; else { nxt[p] = tr[nxt[t]][i]; q.push(p); } } cnt[t] += cnt[nxt[t]]; } } int main () { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%s", s + 1); insert(strlen(s + 1), s); } build(); f[0][0] = 1; for (int k = 0; k < m; k++) for (int i = 0; i <= idx; i++) for (int j = 0; j < 26; j++) if (!cnt[tr[i][j]]) (f[tr[i][j]][k + 1] += f[i][k]) %= mod; int res = binpow(26, m); for (int i = 0; i <= idx; i++) (res -= f[i][m]) %= mod; printf("%d", (res + mod) % mod); return 0; }
|