Problem:


Score : 600 points

Problem Statement

We have a rectangular grid of squares with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.
On this grid, there is a piece, which is initially placed at square (s_r,s_c).

Takahashi and Aoki will play a game, where each player has a string of length N.
Takahashi's string is S, and Aoki's string is T. S and T both consist of four kinds of letters: L, R, U and D.

The game consists of N steps. The i-th step proceeds as follows:

First, Takahashi performs a move. He either moves the piece in the direction of S_i, or does not move the piece.

Second, Aoki performs a move. He either moves the piece in the direction of T_i, or does not move the piece.

Here, to move the piece in the direction of L, R, U and D, is to move the piece from square (r,c) to square (r,c-1), (r,c+1), (r-1,c) and (r+1,c), respectively. If the destination square does not exist, the piece is removed from the grid, and the game ends, even if less than N steps are done.

Takahashi wants to remove the piece from the grid in one of the N steps.
Aoki, on the other hand, wants to finish the N steps with the piece remaining on the grid.
Determine if the piece will remain on the grid at the end of the game when both players play optimally.

Constraints

2 \leq H,W \leq 2 \times 10^5

2 \leq N \leq 2 \times 10^5

1 \leq s_r \leq H

1 \leq s_c \leq W

|S|=|T|=N

S and T consists of the four kinds of letters L, R, U and D.

Input

Input is given from Standard Input in the following format:

H W N
s_r s_c
S
T

Output

If the piece will remain on the grid at the end of the game, print YES; otherwise, print NO.

Sample Input 1

2 3 3
2 2
RRL
LUD

Sample Output 1

YES

Here is one possible progress of the game:

Takahashi moves the piece right. The piece is now at (2,3).

Aoki moves the piece left. The piece is now at (2,2).

Takahashi does not move the piece. The piece remains at (2,2).

Aoki moves the piece up. The piece is now at (1,2).

Takahashi moves the piece left. The piece is now at (1,1).

Aoki does not move the piece. The piece remains at (1,1).

Sample Input 2

4 3 5
2 2
UDRRR
LLDUD

Sample Output 2

NO

Sample Input 3

5 6 11
2 1
RLDRRUDDLRL
URRDRLLDLRD

Sample Output 3

NO



Sample input:
2 3 3
2 2
RRL
LUD


Sample output:
YES

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()

Slr, Sud = 0, 0
Tlr, Tud = 0, 0
for n in range(N):
    if S[n] == "L":
        Slr += -1
    elif S[n] == "R":
        Slr += 1
    elif S[n] == "U":
        Sud += -1
    elif S[n] == "D":
        Sud += 1

    if T[n] == "L" and Tlr >= 1-sc:
        Tlr += -1
    elif T[n] == "R" and Tlr <= W-sc:
        Tlr += 1
    elif T[n] == "U" and Tud >= 1-sr:
        Tud += -1
    elif T[n] == "D" and Tud <= H-sr:
        Tud += 1

    if Slr + Tlr < 1-sc or Slr + Tlr > W-sc:
        print("NO")
        exit()
    if Sud + Tud < 1-sr or Sud + Tud > H-sr:
        print("NO")
        exit()

print("YES")

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = [i for i in input()]
T = [i for i in input()]

lef1 = 0
rig1 = 0
up1 = 0
do1 = 0

def mind(ssr,ssc):
    min()
a=0
for i in range(N):
    if(S[i] =="L"):
        lef1 +=1
    if(T[i] =="R"):
        if(lef1>0):
            lef1 -=1
    if(S[i] =="R"):
        rig1 +=1
    if(T[i] =="L"):
        if(rig1>0):
            rig1 -=1
    if(S[i] =="U"):
        up1 +=1
    if(T[i] =="D"):
        if(up1>0):
            up1 -=1
    if(S[i] =="D"):
        do1 +=1
    if(T[i] =="U"):
        if(do1>0):        
            do1 -=1
    if((lef1>=sr)or(up1>=sc)or(rig1>=W-sr+1)or(do1>=H-sc+1)):
        print("NO")
        a=1
        break
if(a==0):
    print("YES")

Submission status: Wrong Answer

Solution code:
def cheak(s, t):
    if 'L' in s:
        r, c = 0, -1
    elif 'R' in s:
        r, c = 0, 1
    elif 'U' in s:
        r, c = -1, 0
    elif 'D' in s:
        r, c = 1, 0
        
    if 'L' in t:
        c += -1
    elif 'R' in t:
        c += 1
    elif 'U' in t:
        r += -1
    elif 'D' in t:
        r += 1
    return r, c
    
def answer(h, w, r, c):
    if (r>h) or (r<0):
        return 1
    elif (c>w) or (c<0):
        return 1
    else:
        return 0
    
h, w, n = map(int, input().split())
r, c = map(int, input().split())
s = input()
t = input()
ans = 0

L_s, L_t = s.count('L'), t.count('L')
R_s, R_t = s.count('R'), t.count('R')
U_s, U_t = s.count('U'), t.count('U')
D_s, D_t = s.count('D'), t.count('D')

if R_s-L_t+c > w:
    ans += 1
if R_t-L_s+c < 0:
    ans += 1
if D_t-U_s+r < 0:
    ans += 1
if D_s-U_t+r > h:
    ans += 1
if ans >= 1:
    print('Yes')

u, v = cheak(s[0], str(0))
r += u
c += v
if answer(h, w, r, c) == 1:
    print('No')
else:
    for i in range(1, n):
        u, v = cheak(s[i], t[i-1])
        r += u
        c += v
        if answer(h, w, r, c) == 1:
            print('No')
            break
if answer(h, w, r, c) != 1:
    print('Yes')

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = input()
t = input()

margins = [sc - 1, w - sc, sr - 1, h - sr]
# print(margins)
s_cum = [0] * 4
t_cum = [0] * 4
for si, ti in zip(s, t):

    s_cum["LRUD".find(si)] += 1

    if s_cum[0] > margins[0]:
        if t_cum[1]:
            t_cum[1] -= 1
            s_cum[0] -= 1
        else:
            print("NO")
            break

    if s_cum[1] > margins[1]:
        if t_cum[0]:
            t_cum[0] -= 1
            s_cum[1] -= 1
        else:
            print("NO")
            break

    if s_cum[2] > margins[2]:
        if t_cum[3]:
            t_cum[3] -= 1
            s_cum[2] -= 1
        else:
            print("NO")
            break

    if s_cum[3] > margins[3]:
        if t_cum[2]:
            t_cum[2] -= 1
            s_cum[3] -= 1
        else:
            print("NO")
            break

    t_cum["LRUD".find(ti)] += 1

    for e, (i, j, k) in enumerate(zip(t_cum, margins, s_cum)):
        t_cum[e] = max(0, i - max(0, i - j))

    # print(si, ti, s_cum, t_cum)
else:
    print("YES")

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
Max = sys.maxsize
def l():
    return list(map(int,input().split()))
def m():
    return map(int,input().split())
def s(x):
    a = []
    aa = x[i]
    su = 1
    for i in range(len(x)-1):
        if aa == x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x):
    return " ".join(map(str,x))

h,w,n = m()

s1,s2 = m()


r = w-s1
l = s1
u = h - s2
d = s2

S = list(input())

T = list(input())

k1 = [0,0,0,0]

k2 = [0,0,0,0]

for i in range(n):
    if S[i] == "R":
        k1[0] += 1
    elif S[i] == "L":
        k1[1] += 1
    elif S[i] == "U":
        k1[2] += 1
    else:
        k1[3] += 1
    if T[i] == "R":
        k2[0] += 1
    elif T[i] == "L":
        k2[1] += 1
    elif T[i] == "U":
        k2[2] += 1
    else:
        k2[3] += 1
if k1[1] - k2[0] > l or k1[0] - k2[1] > r or k1[2] - k2[3] > u or k1[3] - k2[2] > d:
    print("NO")
    exit()
for i in range(n):
    if S[i] == "R":
        r -= 1
        l += 1
        k1[0] -= 1
    if r < 0:
        print("NO")
        exit()
    if T[i] ==  "L":
        if l != 0 or r != w:
            r += 1
            l -= 1
        k2[1] -= 1

r = w-s1
l = s1

for i in range(n):
    if S[i] == "L":
        l -= 1
        r +=1
        k1[1] -= 1
    if l < 0:
        print("NO")
        exit()
    if T[i] ==  "R":
        if r != 0 or l != w:
            r -= 1
            l += 1
        k2[0] -= 1


for i in range(n):
    if S[i] == "U":
        d += 1
        u -= 1
        k1[3] -= 1
    if u < 0:
        print("NO")
        exit()
    if T[i] == "D":
        if d != 0 or u != h:
            u += 1
            d -= 1
        k2[2] -= 1

u = h - s2
d = s2
for i in range(n):
    if S[i] == "D":
        u += 1
        d -= 1
        k1[2] -= 1
    if d < 0:
        print("NO")
        exit()
    if T[i] == "U":
        if u != 0 or d != h:
            d += 1
            u -= 1
        k2[3] -= 1

print("YES")

Submission status: Wrong Answer

Solution code:

import sys

H, W, N = map(int, input().rstrip().split(' '))
h, w = map(int, input().rstrip().split(' '))
h -= 1
w -= 1
S = [x for x in input()]
T = [x for x in input()]

d = {'R': [0, 1], 'L': [0, -1], 'U': [-1, 0], 'D': [1, 0]}

for s, t in zip(S, T):
    h += d[s][0]
    w += d[s][1]
    
    if h < 0 or h >= H or w < 0 or w >= W:
        print('No')
        sys.exit()
        
    h += d[t][0]
    w += d[t][1]
    
    if h < 0 or h >= H or w < 0 or w >= W:
        print('No')
        sys.exit()
        
print('Yes')

Submission status: Wrong Answer

Solution code:
H,W,N = map(int,input().split())
R,C = map(int,input().split())
S = input()
T = input()
for n in range(N):
        s,t = S[n],T[n]
        if s == 'U':
                R -= 1
                if R < 0:
                        print('N0')
                        exit()
        if t == 'D' and R < H:
                R += 1
        if s == 'L':
                C -= 1
                if C < 0:
                        print('N0')
                        exit()
        if t == 'R' and C < W:
                C += 1
        if s == 'R':
                C += 1
                if C > W:
                        print('N0')
                        exit()
        if t == 'L' and C > 0:
                C -= 1
        if s == 'D':
                R += 1
                if R > H:
                        print('N0')
                        exit()
        if t == 'U' and R > 0:
                R -= 1
print('YES')

Submission status: Wrong Answer

Solution code:
H,W,N = map(int,input().split())
sr, sc = map(int,input().split())
S = input()
T = input()
L = sc-1
R = W-sc
U = sr-1
D = H-sr
for s, t in zip(S,T):
    if s == 'L':
        L -= 1
    elif s == 'R':
        R -= 1
    elif s == 'U':
        U -= 1
    elif s == 'D':
        D -= 1
    if min(L,R,U,D) < 0:
        print('NO')
        quit()
    if t == 'L' and R < W-1:
        R += 1
    elif t == 'R' and L < W-1:
        L += 1
    elif t == 'U' and D < H-1:
        D += 1
    elif t == 'D' and D < H-1:
        U += 1
print('YES')

Submission status: Wrong Answer

Solution code:
def solve():
    H, W, N = map(int, input().split())
    sr, sc = map(int, input().split())
    S = list(input())
    T = list(input())

    # to left
    vec = 'L'
    anv = 'R'
    border = 1
    winner = 'YES'
    result = sc
    addition = -1
    out_mass_func = lambda x: x < border
    for i in range(len(S)):
        if S[i] == vec:
            result += addition
        if out_mass_func(result):
            winner = 'NO'
            break
        if T[i] == anv and result < border:
            result -= addition
    # print('left', result)
    # to right
    if winner == 'YES':
        vec = 'R'
        anv = 'L'
        border = W
        winner = 'YES'
        result = sc
        addition = 1
        out_mass_func = lambda x: x > border
        for i in range(len(S)):
            if S[i] == vec:
                result += addition
            if out_mass_func(result):
                winner = 'NO'
                break
            if T[i] == anv and (1 < result):
                result -= addition
        # print('right', result)
    # to top
    if winner == 'YES':
        vec = 'U'
        anv = 'D'
        border = 1
        winner = 'YES'
        result = sr
        addition = -1
        out_mass_func = lambda x: x < border
        for i in range(len(S)):
            if S[i] == vec:
                result += addition
            if out_mass_func(result):
                winner = 'NO'
                break

            if T[i] == anv and (result < border):
                result -= addition
        # print('top', result)
    # to bottom
    if winner == 'YES':
        vec = 'D'
        anv = 'U'
        border = H
        winner = 'YES'
        result = sr
        addition = 1
        out_mass_func = lambda x: x > border
        for i in range(len(S)):
            if S[i] == vec:
                result += addition
            if out_mass_func(result):
                winner = 'NO'
                break
            if T[i] == anv and 1 < result:
                result -= addition
        # print('bottom', result)
    print(winner)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/agc033
# -*- coding: utf-8 -*-

h,w,n = map(int,input().split())
current = list(map(int,input().split()))
s = list(input())
t = list(input())

U = current[0]
D = h-current[0]+1
L = current[1]
R = w-current[1]+1

win="NO"
for i in range(n):
        sU = s[0:i].count("U")
        sD = s[0:i].count("D")
        sL = s[0:i].count("L")
        sR = s[0:i].count("R")

        tU = t[0:i].count("U")
        tD = t[0:i].count("D")
        tL = t[0:i].count("L")
        tR = t[0:i].count("R")

        # print(U,D,L,R)
        # print(sU,sD,sL,sR)
        # print(tU,tD,tL,tR)

        if (U-sU+tD)==0 or (D-sD+tU)==0 or (L-sL+tR)==0 or (R-sR+tL)==0 :
                win="YES"
                break

print(win)

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()


def T_move_l(y, x):
    WW = W+1 if W % 2 else W
    if x <= WW/2:
        return y, x-1
    else:
        return y, x


def T_move_r(y, x):
    WW = W+1 if W % 2 else W+2
    if x >= WW/2:
        return y, x+1
    else:
        return y, x


def T_move_u(y, x):
    HH = H+1 if H % 2 else H
    if y <= HH/2:
        return y-1, x
    else:
        return y, x


def T_move_d(y, x):
    HH = H+1 if H % 2 else H+2
    if y >= HH/2:
        return y+1, x
    else:
        return y, x


def A_move_l(y, x):
    WW = W+1 if W % 2 else W
    if x > WW/2:
        return y, x-1
    else:
        return y, x


def A_move_r(y, x):
    WW = W+1 if W % 2 else W+2
    if x < WW/2:
        return y, x+1
    else:
        return y, x


def A_move_u(y, x):
    HH = H+1 if H % 2 else H
    if y > HH/2:
        return y-1, x
    else:
        return y, x


def A_move_d(y, x):
    HH = H+1 if H % 2 else H+2
    if y < HH/2:
        return y+1, x
    else:
        return y, x


flg = 0
y, x = sr, sc
for i in range(N):
    if S[i] == 'L':
        y, x = T_move_l(y, x)
    elif S[i] == 'R':
        y, x = T_move_r(y, x)
    elif S[i] == 'U':
        y, x = T_move_u(y, x)
    elif S[i] == 'D':
        y, x = T_move_d(y, x)
    if y < 1 or y > H or x < 1 or x > W:
        flg = 1
        break
    if T[i] == 'L':
        y, x = A_move_l(y, x)
    elif T[i] == 'R':
        y, x = A_move_r(y, x)
    elif T[i] == 'U':
        y, x = A_move_u(y, x)
    elif T[i] == 'D':
        y, x = A_move_d(y, x)

print('NO' if flg else 'YES')

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = input()
t = input()

margins = [sc - 1, w - sc, sr - 1, h - sr]
# print(margins)
s_cum = [0] * 4
t_cum = [0] * 4
for si, ti in zip(s, t):

    s_cum["LRUD".find(si)] += 1

    if s_cum[0] > margins[0]:
        if t_cum[1]:
            t_cum[1] -= 1
            s_cum[0] -= 1
        else:
            print("NO")
            break

    if s_cum[1] > margins[1]:
        if t_cum[0]:
            t_cum[0] -= 1
            s_cum[1] -= 1
        else:
            print("NO")
            break

    if s_cum[2] > margins[2]:
        if t_cum[3]:
            t_cum[3] -= 1
            s_cum[2] -= 1
        else:
            print("NO")
            break

    if s_cum[3] > margins[3]:
        if t_cum[2]:
            t_cum[2] -= 1
            s_cum[3] -= 1
        else:
            print("NO")
            break

    t_cum["LRUD".find(ti)] += 1

    for e, (i, j, k) in enumerate(zip(t_cum, margins, s_cum)):
        t_cum[e] -= max(0, i - j)
        t_cum[e] = max(0, t_cum[e])

    # print(si, ti, s_cum, t_cum)
else:
    print("YES")

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
dps = [[0,0,0,0] for _ in range(N)]
dpt = [[0,0,0,0] for _ in range(N)]
for i,ss in enumerate(S):
    dps[i] = dps[i-1].copy()
    if ss == 'L':
        dps[i][0] += 1
    if ss == 'R':
        dps[i][1] += 1
    if ss == 'U':
        dps[i][2] += 1
    if ss == 'D':
        dps[i][3] += 1
for i,st in enumerate(T):
    dpt[i] = dpt[i-1].copy()
    if (st == 'L') and (dps[i][1]-dpt[i-1][0] + sc != 1):
        dpt[i][0] += 1
    if (st == 'R') and (-dps[i][0]+dpt[i-1][1] + sc != W):
        dpt[i][1] += 1
    if (st == 'U') and (dps[i][3]-dpt[i-1][2] + sr != 1):
        dpt[i][2] += 1
    if (st == 'D') and (-dps[i][2]+dpt[i-1][3] + sr != H):
        dpt[i][3] += 1
ans = 'YES'
#はじめに落とせる場合
if dps[0][0]==1 & sc==1:
    ans ='NO'
elif dps[0][1]==1 & sc==W:
    ans ='NO'
elif dps[0][2]==1 & sr==1:
    ans ='NO'
elif dps[0][3]==1 & sr==H:
    ans ='NO'
else:
    for i in range(1,N):
        if -dps[i][0]+dpt[i-1][1]+sc == 0:
            ans = 'NO'
            break
        if dps[i][1]-dpt[i-1][0]+sc == W+1:
            ans = 'NO'
            break
        if -dps[i][2]+dpt[i-1][3]+sr==0:
            ans = 'NO'
            break
        if dps[i][3]-dpt[i-1][2]+sr == H+1:
            ans = 'NO'
            break
print(ans)

Submission status: Wrong Answer

Solution code:
H,W,N = map(int,input().split())
R,C = map(int,input().split())
S = input()
T = input()
for n in range(N):
        s,t = S[n],T[n]
        if s == 'U':
                R -= 1
                if R < 0:
                        print('N0')
                        exit()
        if t == 'D' and R < H:
                R += 1
        if s == 'L':
                C -= 1
                if C < 0:
                        print('N0')
                        exit()
        if t == 'R' and C < W:
                C += 1
        if s == 'R':
                C += 1
                if C > W:
                        print('N0')
                        exit()
        if t == 'L' and C > 0:
                C -= 1
        if s == 'D':
                R += 1
                if R > H:
                        print('N0')
                        exit()
        if t == 'U' and R < H:
                R += 1
print('YES')

Submission status: Wrong Answer

Solution code:
h,w,n = map(int,input().split())
sr,sc = map(int,input().split())
s = input()
t = input()
def taka(k,pos):
    if k=='U' and pos[0]-1<h-pos[0]+1:
        pos[0] -= 1
    if k=='D' and h-pos[0]<pos[0]:
        pos[0] += 1
    if k=='R' and w-pos[1]<pos[1]:
        pos[1] += 1
    if k=='L' and pos[1]-1<w-pos[1]+1:
        pos[1] -= 1
    return pos
def ao(k,pos):
    if k=='U' and pos[0]-1>=h-pos[0]+1:
        pos[0] -= 1
    if k=='D' and h-pos[0]>=pos[0]:
        pos[0] += 1
    if k=='R' and w-pos[1]>=pos[1]:
        pos[1] += 1
    if k=='L' and pos[1]-1>=w-pos[1]+1:
        pos[1] -= 1
    return pos
pos = [sr,sc]
for i in range(n):
    pos = taka(s[i],pos)
    if pos[0]<1 or pos[1]<1 or pos[0]>h or pos[1]>w:
        print('NO')
        exit()
    pos = ao(t[i],pos)
print('YES')

Submission status: Wrong Answer

Solution code:
H,W,N=map(int,input().split())
Sr,Sc=map(int,input().split())
S=input()
T=input()

wr=Sc
wl=Sc
hu=Sr
hd=Sr


ans='YES'
for i in range(N):
    if S[i]=='R':
        wr+=1
    elif S[i]=='L':
        wl-=1
    elif S[i]=='U':
        hu-=1
    elif S[i]=='D':
        hd+=1
    if wr>W or wl<1 or hu<1 or hd>H:
        ans='NO'
        break

    if T[i]=='R':
        wl = min(W, wl+1)
    elif T[i]=='L':
        wr = max(1, wr-1)
    elif T[i]=='U':
        hd= min(H,hd-1)
    elif T[i]=='D':
        hu=max(1,hu+1)

print(ans)

Submission status: Wrong Answer

Solution code:
H,W,N = map(int, input().split())
now = [int(i) for i in input().split()]
S = input()
T = input()
flag = True
 
for n in range(N):
    if S[n] == 'L':
        if W-now[1] >= now[1]-1:
            now[1] -= 1
    if S[n] == 'R':
        if W-now[1] <= now[1]-1:
            now[1] += 1 
    if S[n] == 'U':
        if H-now[0] >= now[0]-1:
            now[0] -= 1 
    if S[n] == 'D':
        if H-now[0] <= now[0]-1:
            now[0] += 1
    #print(now)
 
    if now[0]*now[1] == 0 or now[0] == H+1 or now[1] == W+1 :
        flag = False
        break
    
    if T[n] == 'L':
        if W-now[1] < now[1]-1:
            now[1] -= 1
    if T[n] == 'R':
        if W-now[1] > now[1]-1:
            now[1] += 1 
    if T[n] == 'U':
        if H-now[0] < now[0]-1:
            now[0] -= 1 
    if T[n] == 'D':
        if H-now[0] > now[0]-1:
            now[0] += 1
    #print(now)
 
    if now[0]*now[1] == 0 or now[0] == H+1 or now[1] == W+1 :
        flag = False
        break

if flag:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
h, w, n = [int(_) for _ in input().split()]
sr, sc = [int(_) for _ in input().split()]
s = input()
t = input()
if n >= 10**5:
  print("YES")
else:
  print("NO")

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
dps = [[0,0,0,0] for _ in range(N)]
dpt = [[0,0,0,0] for _ in range(N)]
for i,ss in enumerate(S):
    dps[i] = dps[i-1].copy()
    if ss == 'L':
        dps[i][0] += 1
    if ss == 'R':
        dps[i][1] += 1
    if ss == 'U':
        dps[i][2] += 1
    if ss == 'D':
        dps[i][3] += 1
for i,st in enumerate(T):
    dpt[i] = dpt[i-1].copy()
    if (st == 'L') and (dps[i][1]-dpt[i-1][0] + sc != 1):
        dpt[i][0] += 1
    if (st == 'R') and (-dps[i][0]+dpt[i-1][1] + W - sc != W):
        dpt[i][1] += 1
    if (st == 'U') and (dps[i][3]-dpt[i-1][2] + sr != 1):
        dpt[i][2] += 1
    if (st == 'D') and (-dps[i][2]+dpt[i-1][3] + H - sr != H):
        dpt[i][3] += 1
ans = 'YES'
#はじめに落とせる場合
if dps[0][0]==1 & sc==1:
    ans ='NO'
elif dps[0][1]==1 & sc==W:
    ans ='NO'
elif dps[0][2]==1 & sr==1:
    ans ='NO'
elif dps[0][3]==1 & sr==H:
    ans ='NO'
else:
    for i in range(1,N):
        if -dps[i][0]+dpt[i-1][1]+sc == 0:
            ans = 'NO'
            break
        if dps[i][1]-dpt[i-1][0]+sc == W+1:
            ans = 'NO'
            break
        if -dps[i][2]+dpt[i-1][3]+sr==0:
            ans = 'NO'
            break
        if dps[i][3]-dpt[i-1][2]+sr == H+1:
            ans = 'NO'
            break
print(ans)

Submission status: Wrong Answer

Solution code:
H,W,N = map(int, input().split())
now = [int(i) for i in input().split()]
S = input()
T = input()
flag = True

for n in range(N):
    if S[n] == 'L':
        if W-now[1] > now[1]-1:
            now[1] -= 1
    if S[n] == 'R':
        if W-now[1] < now[1]-1:
            now[1] += 1 
    if S[n] == 'U':
        if H-now[0] > now[0]-1:
            now[0] -= 1 
    if S[n] == 'D':
        if H-now[0] < now[0]-1:
            now[0] += 1
    #print(now)

    if now[0]*now[1] == 0 or now[0] == H+1 or now[1] == W+1 :
        flag = False
        break
    
    if T[n] == 'L':
        if W-now[1] <= now[1]-1:
            now[1] -= 1
    if T[n] == 'R':
        if W-now[1] >= now[1]-1:
            now[1] += 1 
    if T[n] == 'U':
        if H-now[0] <= now[0]-1:
            now[0] -= 1 
    if T[n] == 'D':
        if H-now[0] >= now[0]-1:
            now[0] += 1
    #print(now)

    if now[0]*now[1] == 0 or now[0] == H+1 or now[1] == W+1 :
        flag = False
        break

if flag:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
s_dict = defaultdict(lambda: 0)
t_dict = defaultdict(lambda: 0)

h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = list(str(input()))
t = list(str(input()))

for i in range(n):
    s_dict[s[i]] += 1
    t_dict[t[i]] += 1

# 落ちるパターン
# 駒が端にあり、高橋くんが場外に移動させることができる場合、落ちる
ans = "NO"
first = False
if sr == 1 and s_dict["L"] > 0 or sr == w and s_dict["R"] > 0 or sc == 1 and s_dict["U"] > 0 or sc == h and s_dict["D"] > 0:
    first = True

s_sum = defaultdict(lambda: 0)
t_sum = defaultdict(lambda: 0)
if not first:
    # それぞれの累積和でmaxを考える
    for i in range(n):
        s_sum[s[i]] += 1
        # 高橋くんは落とさないようにする制約がある
        if t[i] == "L" and t_sum["L"] - s_sum["R"] >= sr - 1:
            pass
        elif t[i] == "R" and t_sum["R"] - s_sum["L"] >= w - sr:
            pass
        elif t[i] == "U" and t_sum["U"] - s_sum["D"] >= sc - 1:
            pass
        elif t[i] == "D" and t_sum["D"] - s_sum["U"] >= h - sc:
            pass
        else:
            t_sum[t[i]] += 1

        if sr <= s_sum["L"] - t_sum["R"]:
            break
        elif w - sr < s_sum["R"] - t_sum["L"]:
            break
        elif sc <= s_sum["U"] - t_sum["D"]:
            break
        elif h - sc < s_sum["D"] - t_sum["U"]:
            break
        if i == n-1:
            ans = "YES"
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate

def main():
    input = sys.stdin.readline
    H, W, N = map(int, input().split())
    r, c = map(int, input().split())
    S = str(input().strip())
    T = str(input().strip())

    R = c
    L = c
    U = r
    D = r

    i = 0
    while i < N:
        s, t = S[i], T[i]

        if s == 'R':
            R += 1
        elif s == 'L':
            L -= 1
        elif s == 'U':
            U += 1
        else:
            D -= 1

        if R > W or L <= 0 or U > H or D <= 0:
            return 'NO'

        if t == 'R':
            L += 1
            L = min(L, W)
        elif t == 'L':
            R -= 1
            R = max(R, 1)
        elif t == 'U':
            D += 1
            D = min(D, H)
        else:
            U -= 1
            U = max(U, 1)

        i += 1

    return 'YES' 


if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = input()
t = input()

margins = [sc - 1, w - sc, sr - 1, h - sr]
# print(margins)
s_cum = [0] * 4
t_cum = [0] * 4
for si, ti in zip(s, t):

    s_cum["LRUD".find(si)] += 1

    if s_cum[0] > margins[0]:
        if t_cum[1]:
            t_cum[1] -= 1
            s_cum[0] -= 1
        else:
            print("NO")
            break

    if s_cum[1] > margins[1]:
        if t_cum[0]:
            t_cum[0] -= 1
            s_cum[1] -= 1
        else:
            print("NO")
            break

    if s_cum[2] > margins[2]:
        if t_cum[3]:
            t_cum[3] -= 1
            s_cum[2] -= 1
        else:
            print("NO")
            break

    if s_cum[3] > margins[3]:
        if t_cum[2]:
            t_cum[2] -= 1
            s_cum[3] -= 1
        else:
            print("NO")
            break

    t_cum["LRUD".find(ti)] += 1

    for e, (i, j, k) in enumerate(zip(t_cum, margins, s_cum)):
        t_cum[e] -= max(0, i - j)
        s_cum[e] -= max(0, i - j)
        s_cum[e] = max(0, s_cum[e])

    # print(si, ti, s_cum, t_cum)
else:
    print("YES")

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()


def T_move_l(y, x):
    WW = W+1 if W % 2 else W
    if x <= WW/2:
        return y, x-1
    else:
        return y, x


def T_move_r(y, x):
    WW = W+1 if W % 2 else W+2
    if x >= WW/2:
        return y, x+1
    else:
        return y, x


def T_move_u(y, x):
    HH = H+1 if H % 2 else H
    if y <= HH/2:
        return y-1, x
    else:
        return y, x


def T_move_d(y, x):
    HH = H+1 if H % 2 else H+2
    if y >= HH/2:
        return y+1, x
    else:
        return y, x


def A_move_l(y, x):
    WW = W+1 if W % 2 else W+2
    if x > WW/2:
        return y, x-1
    else:
        return y, x


def A_move_r(y, x):
    WW = W+1 if W % 2 else W
    if x < WW/2:
        return y, x+1
    else:
        return y, x


def A_move_u(y, x):
    HH = H+1 if H % 2 else H+2
    if y > HH/2:
        return y-1, x
    else:
        return y, x


def A_move_d(y, x):
    HH = H+1 if H % 2 else H
    if y < HH/2:
        return y+1, x
    else:
        return y, x


flg = 0
y, x = sr, sc
for i in range(N):
    if S[i] == 'L':
        y, x = T_move_l(y, x)
    elif S[i] == 'R':
        y, x = T_move_r(y, x)
    elif S[i] == 'U':
        y, x = T_move_u(y, x)
    elif S[i] == 'D':
        y, x = T_move_d(y, x)
    if y < 1 or y > H or x < 1 or x > W:
        flg = 1
        break
    if T[i] == 'L':
        y, x = A_move_l(y, x)
    elif T[i] == 'R':
        y, x = A_move_r(y, x)
    elif T[i] == 'U':
        y, x = A_move_u(y, x)
    elif T[i] == 'D':
        y, x = A_move_d(y, x)

print('NO' if flg else 'YES')

Submission status: Wrong Answer

Solution code:
from collections import*
h,w,n=map(int,input().split())
x,y=map(int,input().split())
s=Counter(input())
t=Counter(input())
a1=s["R"]-t["L"]
a2=s["L"]-t["R"]
a3=s["U"]-t["D"]
a4=s["D"]-t["U"]
if all([0<=(a1+x)<=w,0<=(a2+x)<=w,0<=(a3+y)<=h,0<=(a4+y)<=h]):
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
dps = [[0,0,0,0] for _ in range(N)]
dpt = [[0,0,0,0] for _ in range(N)]
for i,ss in enumerate(S):
    dps[i] = dps[i-1].copy()
    if ss == 'L':
        dps[i][0] += 1
    if ss == 'R':
        dps[i][1] += 1
    if ss == 'U':
        dps[i][2] += 1
    if ss == 'D':
        dps[i][3] += 1
for i,st in enumerate(T):
    dpt[i] = dpt[i-1].copy()
    if (st == 'L') and (dps[i][1]-dpt[i-1][0] + sc != 1):
        dpt[i][0] += 1
    if (st == 'R') and (-dps[i][0]+dpt[i-1][1] + W - sc != W):
        dpt[i][1] += 1
    if (st == 'U') and (dps[i][2]-dpt[i-1][3] + sr != 1):
        dpt[i][2] += 1
    if (st == 'D') and (-dps[i][3]+dpt[i-1][2] + H - sr != H):
        dpt[i][3] += 1
ans = 'YES'
#はじめに落とせる場合
if dps[0][0]==1 & sc==1:
    ans ='NO'
elif dps[0][1]==1 & sc==W:
    ans ='NO'
elif dps[0][2]==1 & sr==1:
    ans ='NO'
elif dps[0][3]==1 & sr==H:
    ans ='NO'
else:
    for i in range(1,N):
        if -dps[i][0]+dpt[i-1][1]+sc == 0:
            ans = 'NO'
            break
        if dps[i][1]-dpt[i-1][0]+sc == W+1:
            ans = 'NO'
            break
        if -dps[i][2]+dpt[i-1][3]+sr==0:
            ans = 'NO'
            break
        if dps[i][3]-dpt[i-1][2]+sr == H+1:
            ans = 'NO'
            break
print(ans)

Submission status: Wrong Answer

Solution code:
h,w,n = map(int,input().split())
sr,sc = map(int,input().split())
s = input()
t = input()
def taka(k,pos):
    if k=='U' and pos[0]-1<=h-pos[0]+1:
        pos[0] -= 1
    if k=='D' and h-pos[0]<=pos[0]:
        pos[0] += 1
    if k=='R' and w-pos[1]<=pos[1]:
        pos[1] += 1
    if k=='L' and pos[1]-1<=w-pos[1]+1:
        pos[1] -= 1
    return pos
def ao(k,pos):
    if k=='U' and pos[0]-1>h-pos[0]+1:
        pos[0] -= 1
    if k=='D' and h-pos[0]>pos[0]:
        pos[0] += 1
    if k=='R' and w-pos[1]>pos[1]:
        pos[1] += 1
    if k=='L' and pos[1]-1>w-pos[1]+1:
        pos[1] -= 1
    return pos
pos = [sr,sc]
for i in range(n):
    pos = taka(s[i],pos)
    if pos[0]<1 or pos[1]<1 or pos[0]>h or pos[1]>w:
        print('NO')
        exit()
    pos = ao(t[i],pos)
print('YES')