AC自动机+矩阵。模式串数量和长度都很小,最多100个状态,建立矩阵。注意取模操作,写太挫会超时- -!
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 2000005
#define INF 100000
#define base 'a'
typedef long long LL;
int n, z;
struct Trie {
int fail, kind;
int son[4];
int ex;
};
Trie t[N];
int ptr, que[N];
int id(char c){
if(c == 'A')return 0;
if(c == 'G')return 1;
if(c == 'C')return 2;
return 3;
}
int NewNode(){
int p = ptr;
for(int i = 0; i < 4; i++)t[p].son[i] = 0;
t[p].fail = 0;
t[p].ex = 0;
t[p].kind = ptr++;
return p;
}
void Insert(char *c){
int p = 0;
int len = strlen(c);
for(int i = 0; i < len; i++){
if(!t[p].son[id(c[i])])t[p].son[id(c[i])] = NewNode();
p = t[p].son[id(c[i])];
}
t[p].ex = 1;
}
void Build_fail(){
int head = 0, tail = 0;
que[tail++] = 0;
int p;
while(head < tail){
p = que[head++];
for(int i = 0; i < 4; i++){
if(!t[p].son[i]){
if(!p) t[p].son[i] = 0;
else t[p].son[i] = t[t[p].fail].son[i];
}
else {
if(!p)t[t[p].son[i]].fail = 0;
else {
t[t[p].son[i]].fail = t[t[p].fail].son[i];
if(t[t[t[p].fail].son[i]].ex)t[t[p].son[i]].ex = 1;
}
que[tail++] = t[p].son[i];
}
}
}
}
char str[15];
struct Matrix{
LL m[101][101];
}s;
Matrix mut(Matrix a, Matrix b){
Matrix ret;
for(int i = 0; i < ptr; i++){//ptr
for(int j = 0; j < ptr; j++){
ret.m[i][j] = 0;
for(int k = 0; k < ptr; k++){
ret.m[i][j] = ret.m[i][j] + a.m[i][k] * b.m[k][j];//此处若两次取模超时
if(ret.m[i][j] > INF)ret.m[i][j] %= INF;
}
}
}
return ret;
}
Matrix Mpow(Matrix a, int b){
Matrix ret;
for(int i = 0; i < ptr; i++){
for(int j = 0; j < ptr; j++){
ret.m[i][j] = 0;
if(i == j)ret.m[i][j] = 1;
}
}
while(b){
if(b & 1)ret = mut(ret, a);
a = mut(a, a);
b >>= 1;
}
return ret;
}
int main(){
while(scanf("%d%d", &n, &z) != EOF){
ptr = 0;
NewNode();
for(int i = 0; i < n; i++){
scanf("%s", str);
Insert(str);
}
Build_fail();
for(int i = 0; i < ptr; i++){
for(int j = 0; j < 4; j++){
int next = t[i].son[j];
if(!t[next].ex && !t[i].ex){
s.m[i][t[next].kind]++;
}
}
}
s = Mpow(s, z);
LL ans = 0;
for(int i = 0; i < ptr; i++){
ans += s.m[0][i];
if(ans >= INF)ans -= INF;
}
printf("%I64d\n", ans);
}
return 0;
}