题目背景

欢迎提供翻译,请直接在讨论区发帖,感谢你的贡献。

题目描述

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 NN games of "Hoof, Paper, Scissors" (1 \leq N \leq 100,0001≤N≤100,000). 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 KK times over the entire set of games (0 \leq K \leq 200≤K≤20). For example, if K=2K=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.

你可能听说过“石头,剪刀,布”的游戏。FJ的牛喜欢玩一个类似的游戏,它们称之为“蹄子,剪刀,布”(“蹄子”就是“石头”)。

游戏规则很简单:比赛双方同时数到3,然后同时出一个手势,代表“蹄子”“剪刀”或“布”。“蹄子”胜“剪刀”,“剪刀”胜“布”,“布”胜“蹄子”。举个例子,第一头牛出“蹄子”,第二头牛出“布”,则第二头牛胜利。当然,也可以“平局”(如果两头牛手势相同的话)。

FJ想对阵自己获奖的牛,贝西。贝西作为一个专家,能够预测FJ的手势。不幸的是,贝西作为一头牛,也十分的懒惰。事实上,她只愿意变换固定次数的手势来完成游戏。例如,她可能只想变1次,则他可能出“蹄子”几次,剩下的都出“布”。

鉴于贝西预测FJ会出的手势,以及她想变的次数,求出她最多能赢多少场

输入输出格式

输入格式:

The first line of the input file contains NN and KK.

The remaining NN 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 KK times.

输入输出样例

输入样例#1: 复制

5 1
P
P
H
P
S
输出样例#1: 复制

4

说明

感谢 @lzyzz250 的翻译

/*
20分暴力
卡时感觉能多A几个点。
*/
#include<ctime>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,k,ans;
char s[];
char d[]={'P','H','S'};
bool judge(char a,char b){
if(a=='P'&&b=='H'||a=='H'&&b=='S'||a=='S'&&b=='P') return true;
else return false;
}
void dfs(int nowtime,int sum,int tot,char pos){
if(sum+(n-nowtime)<ans) return ;//优化1 没什么卵用
if(nowtime>n){
ans=max(ans,sum);
return ;
}
if(tot<k){
if(!judge(pos,s[nowtime])){//如果本来能获胜,这局完全没有换的必要。
for(int i=;i<;i++){
if(judge(d[i],s[nowtime])) dfs(nowtime+,sum+,tot+,d[i]);
else dfs(nowtime+,sum,tot+,d[i]);
}
}
if(judge(pos,s[nowtime])) dfs(nowtime+,sum+,tot,pos);
else dfs(nowtime+,sum,tot,pos);
}
else{
if(judge(pos,s[nowtime])) dfs(nowtime+,sum+,tot,pos);
else dfs(nowtime+,sum,tot,pos);
}
}
int main(){
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++) cin>>s[i];
dfs(,,,'P');
dfs(,,,'H');
dfs(,,,'S');
cout<<ans;
}

20分暴力

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 100010
using namespace std;
int n,k;
int fj[MAXN];
int dp[MAXN][][];
bool judge(int a,int b){
if(a==&&b==||a==&&b==||a==&&b==) return true;
else return false;
}
int dfs(int now,int tot,int pos){
if(now>n) return ;
if(dp[now][tot][pos]) return dp[now][tot][pos];
if(tot<k){
if(judge(pos,fj[now])){
if(pos==) return dp[now][tot][pos]=max(dfs(now+,tot,pos),max(dfs(now+,tot+,),dfs(now+,tot+,)))+;
else if(pos==) return dp[now][tot][pos]=max(dfs(now+,tot,pos),max(dfs(now+,tot+,),dfs(now+,tot+,)))+;
else if(pos==) return dp[now][tot][pos]=max(dfs(now+,tot,pos),max(dfs(now+,tot+,),dfs(now+,tot+,)))+;
}
else{
if(pos==) return dp[now][tot][pos]=max(dfs(now+,tot,pos),max(dfs(now+,tot+,),dfs(now+,tot+,)));
else if(pos==) return dp[now][tot][pos]=max(dfs(now+,tot,pos),max(dfs(now+,tot+,),dfs(now+,tot+,)));
else if(pos==) return dp[now][tot][pos]=max(dfs(now+,tot,pos),max(dfs(now+,tot+,),dfs(now+,tot+,)));
}
}
else{
if(judge(pos,fj[now])) return dp[now][tot][pos]=dfs(now+,tot,pos)+;
else return dp[now][tot][pos]=dfs(now+,tot,pos);
}
}
int main(){
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++){
char x;cin>>x;
if(x=='P') fj[i]=;
if(x=='S') fj[i]=;
if(x=='H') fj[i]=;
}
cout<<max(max(dfs(,,),dfs(,,)),dfs(,,))<<endl;
}

100 记忆化搜索

洛谷 P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀…的更多相关文章

  1. 【luogu P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀布】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3609 ### 看着标签什么记搜什么暴力点进来,读完题第一直觉DP? 还真是个\(DP\). 题目所描述的状态 ...

  2. 洛谷P3906 Hoof Paper, Scissor (记忆化搜索)

    这道题问的是石头剪刀布的的出题问题 首先不难看出这是个dp题 其次这道题的状态也很好确定,之前输赢与之后无关,确定三个状态:当前位置,当前手势,当前剩余次数,所以对于剪刀,要么出石头+1分用一次机会, ...

  3. 洛谷P3608 [USACO17JAN]Balanced Photo平衡的照片

    P3608 [USACO17JAN]Balanced Photo平衡的照片 题目描述 Farmer John is arranging his NN cows in a line to take a ...

  4. 2018.08.16 洛谷P3607 [USACO17JAN]序列反转(线性dp)

    传送门 一道感觉比较简单的dp. 注意是要求翻转一个子序列而不是一段连续的数(被坑了很多次啊)... 看到数据范围果断开一个四维数组来dp一波. 我们显然可以用f[i][j][k][t]表示下标在[l ...

  5. 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]

    题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...

  6. 洛谷P3611 [USACO17JAN]Cow Dance Show奶牛舞蹈

    题目描述 After several months of rehearsal, the cows are just about ready to put on their annual dance p ...

  7. 洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数

    题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...

  8. [BZOJ4760][Usaco2017 Jan]Hoof, Paper, Scissors dp

    4760: [Usaco2017 Jan]Hoof, Paper, Scissors Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 136  Solv ...

  9. 洛谷【P1885】Moo

    我对分治的理解:https://www.cnblogs.com/AKMer/p/9728574.html 题目传送门:https://www.luogu.org/problemnew/show/P18 ...

随机推荐

  1. spring 中文乱码问题

    spring 开发过程中的中文乱码问题主要分为以下几种: 1.前端传参数到后台前  就已经乱码. 这个很大原因就是前端的问题了! 2.传入后台后,乱码. 可能存在几个原因: 2.1 传入tomcat前 ...

  2. iOS - 事件处理全过程(补充)

    事件处理的完整过程 1> 先将事件对象由上往下传递(由父控件传递给子控件),找到最合适的控件来处理这个事件. 2> 调用最合适控件的touches….方法 3> 如果调用了[supe ...

  3. Farseer.net轻量级ORM开源框架 V1.x 入门篇:表的数据操作

    导航 目   录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:表实体类映射 下一篇:Farseer.net轻量级ORM开源框 ...

  4. jsdk之微信分享流程

    .步骤一:绑定域名 先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”. 备注:登录后可在“开发者中心”查看对应的接口权限. .步骤二:引入JS文件 在需要调用JS接口的页面 ...

  5. 认识MySQL Replication

    MySQL Replication 是 MySQL 非常有特色的一个功能,他能够将一个 MySQL Server 的 Instance 中的数据完整的复制到另外一个 MySQL Server 的 In ...

  6. OpenGL Column-Major Matrix 使用注意事项

    这column major的矩阵是彻底把我搞晕了,以后右乘规则下的矩阵应该这么用 假设我想创建一个2x2的矩阵,数学上我这么写: 1 2 3 4 用代码创建的话这么写 // 按照 row major ...

  7. PHP 之PHP + phantomJS实现网站截屏

    php代码: exec("G:/phpstudy/WWW/destoon/api/a/cache/web/phantomjs.exe ./get.js http://www.baidu.co ...

  8. 08CSS边框边距

    CSS边框边距 边框样式——border-style border-top-style border-bottom-style border-left-style border-right-style ...

  9. Python入门之类(class)

    面向对象三大特性 面向对象的三大特性是指:封装.继承和多态. 一.封装 封装,顾名思义就是将内容封装到某个地方,以后再去调用被封装在某处的内容. 所以,在使用面向对象的封装特性时,需要: 将内容封装到 ...

  10. 20181225模拟赛 T1 color (转化思想,分拆思想)

    题目: 有⼀块有 n 段的栅栏,要求第 i 段栅栏最终被刷成颜色 ci .每⼀次可以选择 l, r 把第l . . . r 都刷成某种颜色,后刷的颜⾊会覆盖之前的.⼀共有 m 种颜色,雇主知道只需要用 ...