Roma and Poker
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.
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.
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.
def ma():
    s = input()
    v = s.split(' ')
    n = int(v[0])
    k = int(v[1])
    s = input()
    dp = [[0, 0] for _ in range(n + 1)]
    flag = True
    mp = {}
    mp['W'] = 1
    mp['D'] = 0
    mp['L'] = -1
    for i in range(1, n):
        c = s[i - 1]
        if c == '?':
            dp[i][0] = min(dp[i - 1][0] + 1, k - 1)
            dp[i][1] = max(dp[i - 1][1] - 1, -k + 1)
        else:
            dp[i][0] = min(dp[i - 1][0] + mp[c], k - 1)
            dp[i][1] = max(dp[i - 1][1] + mp[c], -k + 1)
            if dp[i][1] == k or dp[i][0] == -k:
                flag = False
                break
    if not flag:
        print('NO')
        return
    i = n
    if s[i - 1] == '?':
        dp[i][0] = dp[i - 1][0] + 1
        dp[i][1] = dp[i - 1][1] - 1
    else:
        dp[i][0] = dp[i - 1][0] + mp[s[i - 1]]
        dp[i][1] = dp[i - 1][1] + mp[s[i - 1]]
    res = ['?'] * n
    if dp[i][0] == k or dp[i][1] == -k:
        if dp[i][0] == k:
            cur = k
        else:
            cur = -k
        for i in range(n - 1, -1, -1):
            c = s[i]
            if c == '?':
                if cur > dp[i][0]:
                    res[i] = 'W'
                elif dp[i][1] <= cur <= dp[i][0]:
                    res[i] = 'D'
                elif cur < dp[i][1]:
                    res[i] = 'L'
            else:
                res[i] = c
            cur = cur - mp[res[i]]
        for i in range(n):
            print(res[i], end='')
    else:
        print('NO')
ma()
def ma():
    s = input()
    v = s.split(' ')
    n = int(v[0])
    k = int(v[1])
    s = input()
    dp = [[0, 0] for _ in range(n + 1)]
    flag = True
    mp = {}
    mp['W'] = 1
    mp['D'] = 0
    mp['L'] = -1
    for i in range(1, n):
        c = s[i - 1]
        if c == '?':
            dp[i][0] = min(dp[i - 1][0] + 1, k - 1)
            dp[i][1] = max(dp[i - 1][1] - 1, -k + 1)
        else:
            dp[i][0] = min(dp[i - 1][0] + mp[c], k - 1)
            dp[i][1] = max(dp[i - 1][1] + mp[c], -k + 1)
            if dp[i][1] == k or dp[i][0] == -k:
                flag = False
            ''
    if not flag:
        print('NO')
        return
    i = n
    if s[i - 1] == '?':
        dp[i][0] = dp[i - 1][0] + 1
        dp[i][1] = dp[i - 1][1] - 1
    else:
        dp[i][0] = dp[i - 1][0] + mp[s[i - 1]]
        dp[i][1] = dp[i - 1][1] + mp[s[i - 1]]
        ''
    res = ['?'] * n
    if dp[i][0] == k or dp[i][1] == -k:
        if dp[i][0] == k:
            cur = k
        else:
            cur = -k
        for i in range(n - 1, -1, -1):
            c = s[i]
            if c == '?':
                if cur > dp[i][0]:
                    res[i] = 'W'
                elif dp[i][1] <= cur <= dp[i][0]:
                    res[i] = 'D'
                elif cur < dp[i][1]:
                    res[i] = 'L'
            else:
                res[i] = c
            cur = cur - mp[res[i]]
            ''
        for i in range(n):
            print(res[i], end='')
    else:
        print('NO')
ma()
def ma():
    s = input()
    v = s.split(' ')
    n = int(v[0])
    k = int(v[1])
    s = input()
    dp = [[0, 0] for _ in range(n + 1)]
    flag = True
    for i in range(1, n):
        c = s[i - 1]
        if c == '?':
            dp[i][0] = min(dp[i - 1][0] + 1, k - 1)
            dp[i][1] = max(dp[i - 1][1] - 1, -k + 1)
        elif c == 'D':
            dp[i][0] = dp[i - 1][0]
            dp[i][1] = dp[i - 1][1]
        elif c == 'W':
            dp[i][0] = min(dp[i - 1][0] + 1, k - 1)
            dp[i][1] = dp[i - 1][1] + 1
            if dp[i][1] == k:
                flag = False
        elif c == 'L':
            dp[i][0] = dp[i - 1][0] - 1
            dp[i][1] = max(dp[i - 1][1] - 1, -k + 1)
            if dp[i][0] == -k:
                flag = False
    if not flag:
        print('NO')
        return
    i = n
    if s[i - 1] == '?':
        dp[i][0] = dp[i - 1][0] + 1
        dp[i][1] = dp[i - 1][1] - 1
    elif s[i - 1] == 'D':
        dp[i][0] = dp[i - 1][0]
        dp[i][1] = dp[i - 1][1]
    elif s[i - 1] == 'W':
        dp[i][0] = dp[i - 1][0] + 1
        dp[i][1] = dp[i - 1][1] + 1
    elif s[i - 1] == 'L':
        dp[i][0] = dp[i - 1][0] - 1
        dp[i][1] = dp[i - 1][1] - 1
    res = ['?'] * n
    if dp[i][0] == k or dp[i][1] == -k:
        if dp[i][0] == k:
            cur = k
        else:
            cur = -k
        for i in range(n - 1, -1, -1):
            c = s[i]
            if c == '?':
                if cur > dp[i][0]:
                    cur = cur - 1
                    res[i] = 'W'
                elif dp[i][1] <= cur <= dp[i][0]:
                    cur = cur
                    res[i] = 'D'
                elif cur < dp[i][1]:
                    cur = cur + 1
                    res[i] = 'L'
            elif c == 'D':
                cur = cur
                res[i] = c
            elif c == 'W':
                cur = cur - 1
                res[i] = c
            elif c == 'L':
                cur = cur + 1
                res[i] = c
        for i in range(n):
            print(res[i], end='')
    else:
        print('NO')
ma()