Codeforces 803E - Roma and Poker

E. Roma and Poker 
time limit per test          
2 seconds
memory limit per test    
256 megabytes
input
standard input
output
standard output

Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.

Last evening Roma started to play poker. He decided to spend no more than k virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by k. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by k.

Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won k bourles or he lost.

The sequence written by Roma is a string s consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met:

  • In the end the absolute difference between the number of wins and loses is equal to k;
  • There is no hand such that the absolute difference before this hand was equal to k.

Help Roma to restore any such sequence.

Input

The first line contains two numbers n (the length of Roma's sequence) and k (1 ≤ n, k ≤ 1000).

The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.

Output

If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.

Otherwise print this sequence. If there are multiple answers, print any of them.

Examples
input
3 2
L??
output
LDL
input
3 1
W??
output
NO
input
20 5
?LLLLLWWWWW?????????
output
WLLLLLWWWWWWWWLWLWDW

题意:
有n场比赛,给出一个k值,每场比赛的结果用W表示胜,L表示败,D表示平, ? 表示未知
?处可以自定义 胜负平
问是否有一个序列满足以下2个要求,有则输出序列,无则输出NO
设前i场胜W【i】,负L【i】
1、前n-1场中,不能有|W[i]-L[i]|>=k
2、最后一场,|W[n]-L[n]|=k
f[i][j]表示前i场W[i]-L[i]的差为j
差可能为负,整体后移n位
#include<cstdio>
#include<iostream>
using namespace std;
int n,m;
char ch[1001];
bool f[1001][2005];
int main()
{
    scanf("%d%d",&n,&m);
    scanf("%s",ch+1);
    f[0][n+1]=true;
    for(int i=1;i<=n;i++)
     for(int j=n-i+1;j<=n+i+1;j++)
      if(i!=n&&(j<=n-m+1||j>=n+m+1)) continue;
      else if(ch[i]=='L'&&f[i-1][j+1]) f[i][j]=true;
      else if(ch[i]=='W'&&f[i-1][j-1]) f[i][j]=true;
      else if(ch[i]=='D'&&f[i-1][j]) f[i][j]=true;
      else if(ch[i]=='?'&&(f[i-1][j-1]||f[i-1][j+1]||f[i-1][j])) f[i][j]=true;
    if(!f[n][n-m+1]&&!f[n][n+m+1])  puts("NO");
    else 
    {
        int now;
        if(f[n][n+m+1]) now=n+m+1;
        else now=n-m+1;
        for(int i=n;i>1;i--)
         if(ch[i]=='L') now++;
         else if(ch[i]=='W') now--;
         else if(ch[i]=='?') 
         {
             if(f[i-1][now-1]) ch[i]='W',now--;
             else if(f[i-1][now+1]) ch[i]='L',now++;
             else ch[i]='D';
         } 
         if(now==n+1) ch[1]='D';
         else if(now==n) ch[1]='L';
         else ch[1]='W';
         printf("%s",ch+1);
    }
}

 

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/6834461.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值