题目背景
欢迎提供翻译,请直接在讨论区发帖,感谢你的贡献。
题目描述
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.
输入输出样例
说明
感谢 @lzyzz250 的翻译
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/* 20分暴力 卡时感觉能多A几个点。 */ #include<ctime> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int n,k,ans; char s[100010]; char d[4]={'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=0;i<3;i++){ if(judge(d[i],s[nowtime])) dfs(nowtime+1,sum+1,tot+1,d[i]); else dfs(nowtime+1,sum,tot+1,d[i]); } } if(judge(pos,s[nowtime])) dfs(nowtime+1,sum+1,tot,pos); else dfs(nowtime+1,sum,tot,pos); } else{ if(judge(pos,s[nowtime])) dfs(nowtime+1,sum+1,tot,pos); else dfs(nowtime+1,sum,tot,pos); } } int main(){ scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) cin>>s[i]; dfs(1,0,0,'P'); dfs(1,0,0,'H'); dfs(1,0,0,'S'); cout<<ans; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 100010 using namespace std; int n,k; int fj[MAXN]; int dp[MAXN][21][4]; bool judge(int a,int b){ if(a==1&&b==3||a==2&&b==1||a==3&&b==2) return true; else return false; } int dfs(int now,int tot,int pos){ if(now>n) return 0; if(dp[now][tot][pos]) return dp[now][tot][pos]; if(tot<k){ if(judge(pos,fj[now])){ if(pos==1) return dp[now][tot][pos]=max(dfs(now+1,tot,pos),max(dfs(now+1,tot+1,2),dfs(now+1,tot+1,3)))+1; else if(pos==2) return dp[now][tot][pos]=max(dfs(now+1,tot,pos),max(dfs(now+1,tot+1,1),dfs(now+1,tot+1,3)))+1; else if(pos==3) return dp[now][tot][pos]=max(dfs(now+1,tot,pos),max(dfs(now+1,tot+1,1),dfs(now+1,tot+1,2)))+1; } else{ if(pos==1) return dp[now][tot][pos]=max(dfs(now+1,tot,pos),max(dfs(now+1,tot+1,2),dfs(now+1,tot+1,3))); else if(pos==2) return dp[now][tot][pos]=max(dfs(now+1,tot,pos),max(dfs(now+1,tot+1,1),dfs(now+1,tot+1,3))); else if(pos==3) return dp[now][tot][pos]=max(dfs(now+1,tot,pos),max(dfs(now+1,tot+1,1),dfs(now+1,tot+1,2))); } } else{ if(judge(pos,fj[now])) return dp[now][tot][pos]=dfs(now+1,tot,pos)+1; else return dp[now][tot][pos]=dfs(now+1,tot,pos); } } int main(){ scanf("%d%d",&n,&k); for(int i=1;i<=n;i++){ char x;cin>>x; if(x=='P') fj[i]=1; if(x=='S') fj[i]=2; if(x=='H') fj[i]=3; } cout<<max(max(dfs(1,0,1),dfs(1,0,2)),dfs(1,0,3))<<endl; }