动态规划+字符串哈希判断有无冲突

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值