列表

详情


NC24052. [USACO 2017 Jan G]Hoof, Paper, Scissors

描述

You have probably heard of the game "Rock, Paper, Scissors". The cows like to play a similar game they call "Hoof, Paper, Scissors".

The rules of "Hoof, Paper, Scissors" are simple. Two cows play against each-other. They both count to three and then each simultaneously makes a gesture that represents either a hoof, a piece of paper, or a pair of scissors. Hoof beats scissors (since a hoof can smash a pair of scissors), scissors beats paper (since scissors can cut paper), and paper beats hoof (since the hoof can get a papercut). For example, if the first cow makes a "hoof" gesture and the second a "paper" gesture, then the second cow wins. Of course, it is also possible to tie, if both cows make the same gesture.

Farmer John wants to play against his prize cow, Bessie, at N games of "Hoof, Paper, Scissors" (). Bessie, being an expert at the game, can predict each of FJ's gestures before he makes it. Unfortunately, Bessie, being a cow, is also very lazy. As a result, she tends to play the same gesture multiple times in a row. In fact, she is only willing to switch gestures at most K times over the entire set of games (). For example, if K=2, she might play "hoof" for the first few games, then switch to "paper" for a while, then finish the remaining games playing "hoof".

Given the sequence of gestures FJ will be playing, please determine the maximum number of games that Bessie can win.

输入描述

The first line of the input file contains N and K.
The remaining N lines contains FJ's gestures, each either H, P, or S.

输出描述

Print the maximum number of games Bessie can win, given that she can only change gestures at most K times.

示例1

输入:

5 1
P
P
H
P
S

输出:

4

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++11(clang++ 3.9) 解法, 执行用时: 71ms, 内存消耗: 35912K, 提交时间: 2020-04-01 16:10:29

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
int n,k;
int a[202020];
int dp[120000][30][3];
int check(int i,int j)
{
	if(i-j==1||i-j==-2) return 1;
	return 0;
}
int main()
{
	cin>>n>>k;
	char s[20];
	for(int i=1;i<=n;i++)
	{
		cin>>s;
		if(s[0]=='H') a[i]=0;
		if(s[0]=='P') a[i]=1;
		if(s[0]=='S') a[i]=2;
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<=k;j++)
		{
			for(int t=0;t<3;t++)
			{
				if(!j)
				{
					dp[i][j][t]=dp[i-1][j][t]+check(t,a[i]);
					continue;
				}
				int yes=max(dp[i-1][j-1][(t+1)%3],dp[i-1][j-1][(t+2)%3])+check(t,a[i]);
				int no=dp[i-1][j][t]+check(t,a[i]);
				dp[i][j][t]=max(yes,no);
			}
		}
	}
	int ans=0;
	for(int t=0;t<3;t++)
	ans=max(ans,dp[n][k][t]);
	cout<<ans<<endl;
	return 0;
}

上一题