洛谷P3906 Hoof Paper, Scissor (记忆化搜索)
阅读原文时间:2023年07月12日阅读:1

这道题问的是石头剪刀布的的出题问题

首先不难看出这是个dp题

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

然后dp一下就行了,很简单的一道题

#include
#include
using namespace std;
#define limit (100000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-6
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a,b) pair
#define rep(i, a, b) for(ll i = a; i <= b ; ++i) #define per(i, a, b) for(ll i = b ; i >= a ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\dabiao.txt", "wt", stdout)
#define debug(x) cout< '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();} while(s >= '0' && s <= '9'){x = (x << 3) + (x << 1) + s - '0';s = getchar();}
return x * sign;
}//快读
void write(ll x){
if(x < 0) putchar('-'),x = -x;
if(x / 10) write(x / 10);
putchar(x % 10 + '0');
}
int kase;
int n,k;
int dp[limit][3][22];

int c[limit];
int dfs(int cur, int gest, int tot){
if(dp[cur][gest][tot])return dp[cur][gest][tot];
if(cur > n)return 0;
int ans = dfs(cur + 1, gest, tot);
if(c[cur] == gest)++ans;
if(tot > 0){
int t,p;
if(gest == 0)t = 1, p = 2;
else if(gest == 2)t = 0,p = 1;
else t = 0, p = 2;
ans = max({ans, dfs(cur + 1, t, tot - 1), dfs(cur + 1, p, tot - 1)});
}
return dp[cur][gest][tot] = ans;
}
int main() {
#ifdef LOCAL
FOPEN;
#endif
FASTIO
cin>>n>>k;
rep(i,1,n){
char cc;
cin>>cc;
if(cc == 'H'){
c[i] = 0;
}else if(cc == 'S'){
c[i] = 1;
}else{
c[i] = 2;
}
}
int ans = 0;

cout<<max({ans,dfs(1,0,k),dfs(1,1,k),dfs(1,2,k)});;  
return 0;  

}