题意:给你两个串,求用m个R,n个D能组成多少个包含这两个串
题解:先构造一个AC自动机记录每个状态包含两个串的状态,
状态很容易定义 dp【i】【j】【k】【status】表示在AC自动机K这个节点
使用了 i 个D,j个R ,状态为status的方案数。
然后直接DP即可。
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map> #define pi acos(-1.0)
#define eps 1e-9
#define fi first
#define se second
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a, b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define sfi(a) scanf("%d", &a)
#define sffi(a, b) scanf("%d %d", &a, &b)
#define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define sfL(a) scanf("%lld", &a)
#define sffL(a, b) scanf("%lld %lld", &a, &b)
#define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
#define sfs(a) scanf("%s", a)
#define sffs(a, b) scanf("%s %s", a, b)
#define sfffs(a, b, c) scanf("%s %s %s", a, b, c)
#define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d)
#define FIN freopen("../in.txt","r",stdin)
#define gcd(a, b) __gcd(a,b)
#define lowbit(x) x&-x
#define IO iOS::sync_with_stdio(false) using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const ULL seed = ;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int maxn = 1e6 + ;
const int maxm = 8e6 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
int T, m, n;
char buf[];
int dp[][][][]; int get_num(char ch) {
if (ch == 'D') return ;
else return ;
} struct Aho_Corasick {
int next[][], fail[], End[];
int root, cnt; int newnode() {
for (int i = ; i < ; i++) next[cnt][i] = -;
End[cnt++] = ;
return cnt - ;
} void init() {
cnt = ;
root = newnode();
} void insert(char buf[], int id) {
int len = strlen(buf);
int now = root;
for (int i = ; i < len; i++) {
if (next[now][get_num(buf[i])] == -) next[now][get_num(buf[i])] = newnode();
now = next[now][get_num(buf[i])];
}
End[now] |= ( << id);
} void build() {
queue<int> Q;
fail[root] = root;
for (int i = ; i < ; i++)
if (next[root][i] == -) next[root][i] = root;
else {
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
while (!Q.empty()) {
int now = Q.front();
Q.pop();
End[now] |= End[fail[now]];
for (int i = ; i < ; i++)
if (next[now][i] == -) next[now][i] = next[fail[now]][i];
else {
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
} LL solve() {
mem(dp, );
dp[][][][] = ;
for (int i = ; i <= n; ++i) {
for (int j = ; j <= m; ++j) {
for (int k = ; k < cnt; ++k) {
for (int status = ; status < ; ++status) {
if (dp[i][j][k][status] == ) continue;
for (int l = ; l < ; ++l) {
int idx = next[k][l];
if (l == ) {
dp[i + ][j][idx][status | End[idx]] =
(1LL * dp[i + ][j][idx][status | (End[idx])] + 1LL * dp[i][j][k][status]) %
mod;
// printf("i = %d j = %d idx = %d status = %d dp = %lld\n", i + 1, j, idx,
// status | (End[idx]),
// dp[i + 1][j][idx][status | End[idx]]);
} else {
dp[i][j + ][idx][status | End[idx]] =
(1LL * dp[i][j + ][idx][status | End[idx]] + 1LL * dp[i][j][k][status]) % mod;
// printf("i = %d j = %d idx = %d status = %d dp = %lld\n", i, j + 1, idx,
// status | (End[idx]),
// dp[i][j + 1][idx][status | End[idx]]);
}
}
}
}
}
}
LL ans = ;
for (int i = ; i < cnt; ++i) ans = (ans + dp[n][m][i][]) % mod;
return ans;
} void debug() {
for (int i = ; i < cnt; i++) {
printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], End[i]);
for (int j = ; j < ; j++) printf("%2d", next[i][j]);
printf("]\n");
}
}
} ac; int main() {
// FIN;
sfi(T);
while (T--) {
sffi(m, n);
ac.init();
for (int i = ; i < ; ++i) {
sfs(buf);
ac.insert(buf, i);
}
ac.build();
printf("%lld\n", ac.solve());
}
return ;
}