【动态规划】P3609 [USACO17JAN] Hoof, Paper, Scissor G

本文涉及知识点

C++动态规划

P3609 USACO17JAN Hoof, Paper, Scissor G

题目背景

本题与 银组同名题目 在题意上一致,唯一的差别在于对变手势次数的限制。

题目描述

你可能玩过"石头,剪刀,布",这个游戏在奶牛中同样流行,不过它的名字变成了"蹄子,剪刀,布"。

"蹄子,剪刀,布"和"石头,剪刀,布"的规则十分类似,两只奶牛数到三,然后出一个代表蹄子,剪刀或布的手势。蹄子胜过剪刀,剪刀胜过布,布胜过蹄子。特别地,如果两只奶牛的手势相同,则视为平局。

现在 FJ 和 Bassie 要进行 N N N 轮对抗。Bassie 已经预测了 FJ 每一轮要出的手势。然而 Bassie 很懒,她最多只想变换 K K K 次手势。

现在请你帮 Bassie 求出她最多能赢多少轮。

输入格式

第一行输入两个整数 N , K N,K N,K( 1 ≤ N ≤ 10 5 1 \leq N \leq 10^5 1≤N≤105, 0 ≤ K ≤ 20 0 \leq K \leq 20 0≤K≤20)。

接下来 N N N 行,每行一个字母,代表 FJ 这一轮出的手势。H 代表蹄子(Hoof),S 代表剪刀(Scissors),P 代表布(Paper)。

输出格式

输出一个整数,代表 Bassie 在最多变换 K K K 次手势的前提下最多赢多少轮。

输入输出样例 #1

输入 #1

复制代码
5 1
P
P
H
P
S

输出 #1

复制代码
4

P3609 动态规划 状态机动态规划

动态规划的状态表示

dpijk记录如下状态最多可以赢多少轮。i ∈ \in ∈0,N,j ∈ \in ∈0,2 , 分别代表三种状态。 k ,分别代表三种状态。 k ,分别代表三种状态。k\in$0,K,表示改变过多少次手势。

可以用滚动向量优化空间。pre=dpi,cur=dpi+1空间复杂度:O(NK)

动态规划的填表顺序

枚举前置状态。i = 0 to N-1,j =0 to 2, k = 0 to K 。

动态规划的转移方程

每种状态都有三种新状态,就是当前回合的3种手势。k1 = k +(j != j1),如果k1 > K ,忽略。

单个状态转移时间复杂度:O(1),总时间复杂度:O(NK)

动态规划的初始值

dp00,1,20=0 ,其它全部是-N。

动态规划的返回值

dpN的最大值。

vWini 记录第i轮赢的状态。

代码

核心代码

cpp 复制代码
#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>

#include <bitset>
using namespace std;

template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {
	in >> pr.first >> pr.second;
	return in;
}

template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {
	in >> get<0>(t) >> get<1>(t) >> get<2>(t);
	return in;
}

template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {
	in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);
	return in;
}

template<class T = int>
vector<T> Read() {
	int n;
	scanf("%d", &n);
	vector<T> ret(n);
	for (int i = 0; i < n; i++) {
		cin >> ret[i];
	}
	return ret;
}

template<class T = int>
vector<T> Read(int n) {
	vector<T> ret(n);
	for (int i = 0; i < n; i++) {
		cin >> ret[i];
	}
	return ret;
}

template<int N = 1'000'000>
class COutBuff
{
public:
	COutBuff() {
		m_p = puffer;
	}
	template<class T>
	void write(T x) {
		int num[28], sp = 0;
		if (x < 0)
			*m_p++ = '-', x = -x;

		if (!x)
			*m_p++ = 48;

		while (x)
			num[++sp] = x % 10, x /= 10;

		while (sp)
			*m_p++ = num[sp--] + 48;
		AuotToFile();
	}
	void writestr(const char* sz) {
		strcpy(m_p, sz);
		m_p += strlen(sz);
		AuotToFile();
	}
	inline void write(char ch)
	{
		*m_p++ = ch;
		AuotToFile();
	}
	inline void ToFile() {
		fwrite(puffer, 1, m_p - puffer, stdout);
		m_p = puffer;
	}
	~COutBuff() {
		ToFile();
	}
private:
	inline void AuotToFile() {
		if (m_p - puffer > N - 100) {
			ToFile();
		}
	}
	char  puffer[N], * m_p;
};

template<int N = 1'000'000>
class CInBuff
{
public:
	inline CInBuff() {}
	inline CInBuff<N>& operator>>(char& ch) {
		FileToBuf();
		ch = *S++;
		return *this;
	}
	inline CInBuff<N>& operator>>(int& val) {
		FileToBuf();
		int x(0), f(0);
		while (!isdigit(*S))
			f |= (*S++ == '-');
		while (isdigit(*S))
			x = (x << 1) + (x << 3) + (*S++ ^ 48);
		val = f ? -x : x; S++;//忽略空格换行		
		return *this;
	}
	inline CInBuff& operator>>(long long& val) {
		FileToBuf();
		long long x(0); int f(0);
		while (!isdigit(*S))
			f |= (*S++ == '-');
		while (isdigit(*S))
			x = (x << 1) + (x << 3) + (*S++ ^ 48);
		val = f ? -x : x; S++;//忽略空格换行
		return *this;
	}
	template<class T1,class T2>
	inline CInBuff& operator>>(pair<T1,T2>& val) {
		*this >> val.first >> val.second;
		return *this;
	}
	template<class T1, class T2,class T3>
	inline CInBuff& operator>>(tuple<T1, T2,T3>& val) {
		*this >> get<0>(val) >> get<1>(val) >> get<2>(val);
		return *this;
	}
	template<class T1, class T2, class T3,class T4>
	inline CInBuff& operator>>(tuple<T1, T2, T3,T4>& val) {
		*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);
		return *this;
	}
	template<class T = int>
	inline CInBuff& operator>>(vector<T>& val) {
		int n;
		*this >> n;
		val.resize(n);
		for (int i = 0; i < n; i++) {
			*this >> val[i];
		}
		return *this;
	}
	template<class T = int>
	vector<T> Read(int n) {
		vector<T> ret(n);
		for (int i = 0; i < n; i++) {
			*this >> ret[i];
		}
		return ret;
	}
private:
	inline void FileToBuf() {
		const int canRead = m_iWritePos - (S - buffer);
		if (canRead >= 100) { return; }
		if (m_bFinish) { return; }
		for(int i = 0 ;i < canRead;i++)
		{
			buffer[i] = S[i];//memcpy出错			
		}
		m_iWritePos = canRead;
		buffer[m_iWritePos] = 0;
		S = buffer;
		int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);
		if (readCnt <= 0) { m_bFinish = true; return; }
		m_iWritePos += readCnt;
		buffer[m_iWritePos] = 0;
		S = buffer;
	}
	int m_iWritePos = 0; bool m_bFinish = false;
	char buffer[N + 10], * S = buffer;
};

class Solution {
public:
	int Ans(const vector<char>& a, int K) {
		const int N = a.size();
		vector<int> b;
		for (const auto& ch : a) {
			if ('H' == ch) { b.emplace_back(0); }
			if ('S' == ch) { b.emplace_back(1); }
			if ('P' == ch) { b.emplace_back(2); }
		}
		vector<vector<int>> pre(3, vector<int>(K + 1, -N));
		pre[0][0] = pre[1][0] = pre[2][0] = 0;
		for (int i = 0; i < N; i++) {
			vector<vector<int>> cur(3, vector<int>(K + 1, -N));
			for (int j = 0; j < 3; j++) {
				for (int k = 0; k <= K; k++) {
					for (int j2 = 0; j2 < 3; j2++) {
						const int k2 = k + (j != j2);
						if (k2 > K) { continue; }
						cur[j2][k2] = max(cur[j2][k2], pre[j][k] + (j2 == b[i]));
					}
				}
			}
			pre.swap(cur);
		}
		int ans = 0;
		for (const auto& v : pre) {
			for (const auto& i : v) {
				ans = max(ans, i);
			}
		}
		return ans;
	}
};

int main() {	
#ifdef _DEBUG
	freopen("a.in", "r", stdin);
#endif // DEBUG	
	CInBuff<> ib;
	int n, k;
	ib >> n >> k;
	vector<char> a(n);
	char tmp;
	for (int i = 0; i < n; i++) {
		ib >> a[i] >> tmp;
	}
#ifdef _DEBUG		
	printf("k=%d,", k);
	Out(a, ",a=");
	/*Out(edge, "edge=");
	Out(que, "que=");*/
#endif // DEBUG	
	auto res = Solution().Ans(a,k);
	cout << res;
	return 0;
}

单元测试

cpp 复制代码
	TEST_METHOD(TestMethod1)
		{
			k = 1, a = { 'P','P','H','P','S' };
			auto res = Solution().Ans(a, k);
			AssertEx(4, res);
		}

扩展阅读

算法为骨,CAD为魂
亲士工具箱:支持中望CAD2024、AutoCad2013及以上,多年承接CAD项目的精华
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作
活到老,学到老。明朝中后期,大约50%的进士能当上堂官(副部及更高);能当上堂官的举人只有十余人。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。

视频课程

先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。

https://edu.csdn.net/course/detail/38771

如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程

https://edu.csdn.net/lecturer/6176

测试环境

操作系统:win7 开发环境: VS2019 C++17

或者 操作系统:win10 开发环境: VS2022 C++17

如无特殊说明,本算法用**C++**实现。

相关推荐
汉克老师2 小时前
CSP-J 初赛(以满分为目标):第二十六课 《图的遍历——DFS深度优先搜索——从“树的先序遍历”走进真正的图世界》
c++·csp-j·小学生·学c++编程
程序猿编码2 小时前
基于GGML的C++17轻量化语音推理引擎:说话人识别与语音分析技术全解析
开发语言·c++·pytorch·深度学习·神经网络·大模型
leihefeng2 小时前
手写数字识别:KNN vs 逻辑回归实战
python·算法·机器学习·逻辑回归·scikit-learn
影视飓风TIM2 小时前
C++哈希表深度剖析:冲突处理、rehash、迭代器与unordered容器封装
c++·哈希算法·散列表
全栈技术负责人2 小时前
DeepSeek Harness 业务工具权限插件 dsh-tool-permission设计思路
网络·算法·ai·ai编程
mmmmath_32 小时前
面试题 02.07. 链表相交
算法·链表
C++ 老炮儿的技术栈3 小时前
Qt5 使用 QPainter 绘制阿基米德螺线
开发语言·c++·windows·qt·代码化
residual_fan3 小时前
特征级SMOTE(Feature-level SMOTE)论文分享
人工智能·算法·数据挖掘·数据分析
xxwxx__3 小时前
深入理解 C++ STL:stack、queue 与 deque 从使用到底层实现全解析
开发语言·c++·算法