https://codeforces.com/group/vFwRVj9WjO/contest/345181/problem/H
第一次交的时候p用的是1e7,有冲突,用13331可以。
题解:判断前1-5个是否出现过,如果出现了,计算它的贡献值。
#include <iostream>
#include <cstring>
using namespace std;
#include <map>
const int N = 1e5 + 10;
const int mod = 128;
const int P = 13331;
map<string, int> mp;
string str;
int dp[N], Hash[N];
int main()
{
int T;
cin >> T;
while(T--)
{
mp.clear();
memset(dp, 0, sizeof dp);
memset(Hash, 0, sizeof Hash);
int n , m;
cin >> n >> m;
char ch; string x;
for(int i = 1; i <= m; i++)
{
cin >> ch >> x;
mp[x] ++;
}
cin >> str;
str = " " + str;
dp[0] = Hash[0] = 1;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= 5 and j <= i; j++)
{
string t = str.substr(i-j+1, j);
if(mp[t])
{
dp[i] = (dp[i] + dp[i-j] * mp[t] % mod) % mod;
Hash[i] = (Hash[i] + Hash[i-j * mp[t] % P) % P;
}
}
}
if(dp[n] == 0 and Hash[n] == 0) cout << "nonono\n";
else if(dp[n]==1 and Hash[n]==1) cout << "happymorsecode\n";
else cout << "puppymousecat " << dp[n] << endl;
}
return 0;
}