#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
const int N = 210, K = 45, M = 10;
const unsigned p = 998250007;
int n, k, s, tot, len[M], cnt[N][N], dp[N][K];
ull power[N], dictionary[M][N], Hash[N];
signed main() {
//freopen("pos.in", "r", stdin);
//freopen("pos.out", "w", stdout);
power[0] = 1;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
string str;
cin >> str;
for (const char& ch : str)
++tot,
power[tot] = power[tot - 1] * p,
Hash[tot] = Hash[tot - 1] * p + ch;
}
cin >> s;
for (int i = 1; i <= s; i++) {
string str;
cin >> str;
len[i] = (int)str.size();
for (int j = 1; j <= str.size(); j++)
dictionary[i][j] = dictionary[i][j - 1] * p + str[j - 1];
}
for (int i = 1; i <= s; i++)
for (int j = i + 1; j <= s; j++) {
int a = (len[i] < len[j] ? i : j), b = (len[i] < len[j] ? j : i);
if (dictionary[a][len[a]] == dictionary[b][len[a]])
memset(dictionary[b], 0, sizeof dictionary[b]);
}
for (int i = 1; i <= tot; i++)
for (int j = i; j <= tot; j++) {
cnt[i][j] = cnt[i][j - 1];
for (int now = 1; now <= s; now++)
if (j - i + 1 >= len[now])
cnt[i][j] += Hash[j] - Hash[j - len[now]] * power[len[now]] == dictionary[now][len[now]];
}
for (int i = 1; i <= tot; i++)
for (int j = 1; j <= k; j++)
for (int f = j - 1; f < i; f++)
dp[i][j] = max(dp[i][j], dp[f][j - 1] + cnt[f + 1][i]);
cout << dp[tot][k] << "\n";
return 0;
}
蒟蒻的哈希感觉好复杂,求大佬喷指导