蜗蜗再CityWalk
整体思路:dp
死因:统计答案时没有统计修改次数为0的收益,导致sub1 0,sub3 skipped,痛失70分
代码如下
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
#define endl '\n';
void gmin(int &x,int y){x=min(x,y);}
void gmax(int &x,int y){x=max(x,y);}
string pt(int st,bool x){return st==1?x?"Yes":"No":x?"YES":"NO";}
int n;
char s[105];
bool dp[101][101][2][201];
void solve(){
cin>>s+1>>n;
int l=strlen(s+1);
memset(dp,0,sizeof dp);
dp[0][0][0][100]=1;
for(int i=1;i<=l;i++){
for(int j=0;j<=n;j++){
for(int dx=0;dx<=1;dx++){
for(int p=0;p<=200;p++){
if(dp[i-1][j][dx][p]){
int tp=p-100;
int td=dx==0?-1:1;
char ch=s[i];
// bu gai
if(ch=='W'){
// qia jin!
int np=tp+td;
if(np>=-100&&np<=100){
//zai fan wei zhi nei
dp[i][j][td==-1?0:1][np+100]=1;
}
}else{
int nd=-td;
dp[i][j][nd==-1?0:1][tp+100]=1;
}
//xiu gai
if(j+1>n) continue;
char c=ch=='O'?'W':'O';
if(c=='W'){
int np=tp+td;
if(np>=-100&&np<=100){
dp[i][j+1][td==-1?0:1][np+100]=1;
}
}else{
int nd=-td;
dp[i][j+1][nd==-1?0:1][tp+100]=1;
}
}
}
}
}
}
int ans=0;
//这里i=0原本写成了i=1
for(int i=0;i<=n;i++){
if(i%2!=n%2) continue;
for(int d=0;d<=1;d++){
for(int p=0;p<=200;p++){
if(dp[l][i][d][p]){
int tp=p-100;
gmax(ans,abs(tp));
}
}
}
}
cout<<ans<<endl;
}
int main(){
cin.tie(0)->sync_with_stdio(0);
int t=1;
//cin>>t;
while(t--) solve();
}

浙公网安备 33010602011771号