思路:把 n 个串丢进AC自动机中,然后dp就好啦。 我的代码居然是在CF上跑最快的。。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, m, len, k, tmp, v[];
vector<int> L, R;
vector<int> s; inline void add(int &a, int b) {
a += b; if(a >= mod) a -= mod;
} struct Ac {
int ch[][], val[], f[], tot, sz;
int dp[][][][], cur, pre;
void init(int _sz) {tot = ; sz = _sz;}
inline int newNode() {
tot++; f[tot] = ; val[tot] = ;
memset(ch[tot], , sizeof(ch[tot]));
return tot;
}
inline int idx(int c) {return c;}
void addStr(vector<int> &s, int cost) {
int u = ;
for(int i = ; i < s.size(); i++) {
int c = idx(s[i]);
if(!ch[u][c]) ch[u][c] = newNode();
u = ch[u][c];
}
val[u] += cost;
}
void build() {
queue<int> que;
for(int c = ; c < sz; c++) {
int v = ch[][c];
if(!v) ch[][c] = ;
else f[v] = , que.push(v);
}
while(!que.empty()) {
int u = que.front(); que.pop();
val[u] += val[f[u]];
for(int c = ; c < sz; c++) {
int v = ch[u][c];
if(!v) ch[u][c] = ch[f[u]][c];
else f[v] = ch[f[u]][c], que.push(v);
}
}
}
int solve(vector<int> &str) {
int n = str.size(), ans = ;
cur = , pre = ;
memset(dp[cur], , sizeof(dp[cur]));
for(int z = ; z <= str[]; z++) {
int v = ch[][z];
if(val[v] <= k) add(dp[cur][v][val[v]][z == str[]], );
}
for(int i = ; i < n; i++) {
swap(cur, pre);
memset(dp[cur], , sizeof(dp[cur]));
for(int z = ; z < m; z++) {
int v = ch[][z];
if(val[v] <= k) add(dp[cur][v][val[v]][], );
}
for(int u = ; u <= tot; u++) {
for(int s = ; s <= k; s++) {
if(dp[pre][u][s][]) {
for(int z = ; z < m; z++) {
int v = ch[u][z];
if(s+val[v] <= k) add(dp[cur][v][val[v]+s][], dp[pre][u][s][]);
}
} if(dp[pre][u][s][]) {
for(int z = ; z <= str[i]; z++) {
int v = ch[u][z];
if(s+val[v] <= k) add(dp[cur][v][val[v]+s][z==str[i]], dp[pre][u][s][]);
}
}
}
}
}
for(int u = ; u <= tot; u++)
for(int j = ; j <= k; j++)
add(ans, dp[cur][u][j][]), add(ans, dp[cur][u][j][]);
return ans;
}
} ac; int main() {
scanf("%d%d%d", &n, &m, &k);
ac.init(m);
scanf("%d", &len); L.resize(len);
for(int i = ; i < len; i++) scanf("%d", &L[i]);
scanf("%d", &len); R.resize(len);
for(int i = ; i < len; i++) scanf("%d", &R[i]);
for(int i = ; i < n; i++) {
scanf("%d", &len); s.resize(len);
for(int j = ; j < len; j++) scanf("%d", &s[j]);
int cost; scanf("%d", &cost);
ac.addStr(s, cost);
for(int p = ; p+s.size() <= L.size(); p++) {
bool flag = true;
for(int q = ; q < s.size(); q++) {
if(s[q] != L[q+p]) {
flag = false;
break;
}
}
if(flag) tmp += cost;
}
}
ac.build();
int ans = (ac.solve(R) - ac.solve(L) + (tmp <= k) + mod) % mod;
printf("%d\n", ans);
return ;
}
/*
*/