这题显然的dp
设dp[i][j]表示当前考虑到第i个位置,胜场比负场多j个能否做到,同时记录转移的坐标方便打印
因为j可能是负的,所以要加一个base
如果当前位是W:dp[i][j]=dp[i-1][j-1]
如果当前位是L:dp[i][j]=dp[i][j+1]
如果当前位是D:dp[i][j]=dp[i-1][j]
如果当前位是?:上面三种情况都考虑一遍
注意:转移的时候abs(j)是不能为k的
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <utility>
#include <map>
#include <stack>
#include <set>
#include <vector>
#include <queue>
#include <deque>
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define LL long long
#define Pair pair<bool,pair<int,int> >
#define LOWBIT(x) x & (-x)
using namespace std;
const int zero_stand=1500;
const int MOD=1e9+7;
const int INF=0x7ffffff;
const int magic=348;
Pair dp[1048][3048];
int n,k;
char s[1048];
void print(int x,int y)
{
if (dp[x][y].y.y!=-1) print(x-1,dp[x][y].y.y);
if (dp[x][y].y.x==1) printf("W");
if (dp[x][y].y.x==2) printf("L");
if (dp[x][y].y.x==3) printf("D");
}
int main ()
{
int i,j;
scanf("%d%d%s",&n,&k,s+1);
for (i=1;i<=n;i++)
for (j=zero_stand-k-10;j<=zero_stand+k+10;j++)
dp[i][j]=mp(false,mp(0,0));
if (s[1]=='W') dp[1][zero_stand+1]=mp(true,mp(1,-1));
if (s[1]=='L') dp[1][zero_stand-1]=mp(true,mp(2,-1));
if (s[1]=='D') dp[1][zero_stand]=mp(true,mp(3,-1));
if (s[1]=='?')
{
dp[1][zero_stand+1]=mp(true,mp(1,-1));
dp[1][zero_stand-1]=mp(true,mp(2,-1));
dp[1][zero_stand]=mp(true,mp(3,-1));
}
for (i=2;i<=n;i++)
for (j=zero_stand-k;j<=zero_stand+k;j++)
{
if ((j==zero_stand-k || j==zero_stand+k) && i!=n) continue;
if (s[i]=='W')
if (dp[i-1][j-1].x && j-1!=zero_stand-k)
{
dp[i][j]=mp(true,mp(1,j-1));
continue;
}
if (s[i]=='L')
if (dp[i-1][j+1].x && j+1!=zero_stand+k)
{
dp[i][j]=mp(true,mp(2,j+1));
continue;
}
if (s[i]=='D')
if (dp[i-1][j].x)
{
dp[i][j]=mp(true,mp(3,j));
continue;
}
if (s[i]=='?')
{
if (dp[i-1][j-1].x && j-1!=zero_stand-k)
{
dp[i][j]=mp(true,mp(1,j-1));
continue;
}
if (dp[i-1][j+1].x && j+1!=zero_stand+k)
{
dp[i][j]=mp(true,mp(2,j+1));
continue;
}
if (dp[i-1][j].x)
{
dp[i][j]=mp(true,mp(3,j));
continue;
}
}
}
if (!dp[n][zero_stand-k].x && !dp[n][zero_stand+k].x)
{
printf("NO\n");
return 0;
}
if (dp[n][zero_stand-k].x) print(n,zero_stand-k); else print(n,zero_stand+k);
printf("\n");
return 0;
}