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:
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 = s1
d = s2

S = list(input())

T = list(input())


for i in range(n):
    if S[i] == "R":
        r += 1
    if r > w:
        print("NO")
        exit()
    if T[i] ==  "L":
        if r != 1:
            r -= 1

r = s1

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


for i in range(n):
    if S[i] == "U":
        d += 1
    if d > h:
        print("NO")
        exit()
    if T[i] == "D":
        if d != 1:
            d -= 1

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

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 = s1
d = s2

S = list(input())

T = list(input())


for i in range(n):
    if S[i] == "R":
        r += 1
    if r > w:
        print("NO")
        exit()
    if T[i] ==  "L":
        if r != 1:
            r -= 1

r = s1

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


for i in range(n):
    if S[i] == "U":
        d += 1
    if d > h:
        print("NO")
        exit()
    if T[i] == "D":
        if d != 1:
            d -= 1

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

print("YES")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
H, W, N = map(int, input().split())
sr, sc = map(lambda x: int(x) - 1, input().split())
S = input()
T = input()
l = 0
r = W - 1
u = 0
d = H - 1
for i in range(N)[::-1]:
    if T[i] == 'L':
        r = min(W - 1, r + 1)
    if T[i] == 'R':
        l = max(0, l - 1)
    if T[i] == 'U':
        d = min(H - 1, d + 1)
    if T[i] == 'D':
        u = max(0, l - 1)
    if S[i] == 'L':
        l = min(W, l + 1)
    if S[i] == 'R':
        r = max(-1, r - 1)
    if S[i] == 'U':
        u = min(H, u + 1)
    if S[i] == 'D':
        d = max(-1, d - 1)
    if l > r or u > d:
        break

print(["NO", "YES"][l <= sc <= r and u <= sr <= d])

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:
#!/usr/bin/env python3
H, W, N = map(int, input().split())
sr, sc = map(lambda x: int(x) - 1, input().split())
S = input()
T = input()
l = 0
r = W - 1
u = 0
d = H - 1
for i in range(N)[::-1]:
    if T[i] == 'L':
        r = min(W - 1, r + 1)
    if T[i] == 'R':
        l = max(0, l - 1)
    if T[i] == 'U':
        d = min(H - 1, d + 1)
    if T[i] == 'D':
        u = max(0, l - 1)
    if S[i] == 'L':
        l = min(W - 1, l + 1)
    if S[i] == 'R':
        r = max(0, r - 1)
    if S[i] == 'U':
        u = min(H - 1, u + 1)
    if S[i] == 'D':
        d = max(0, d - 1)
    if l >= r or u >= d:
        break

print(["NO", "YES"][l <= sc <= r and u <= sr <= d])

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
l = 0
r = W+1
u = 0
d = H+1
for s,t in zip(S[::-1],T[::-1]):
    if s == 'L':
        l += 1
    if s == 'R':
        r -= 1
    if s == 'U':
        u += 1
    if s == 'D':
        d -= 1
    if t == 'L':
        r = min(0, r-1)
    if t == 'R':
        l = max(W+1, l+1)
    if t == 'U':
        d = min(0, d-1)
    if t == 'D':
        u = max(H+1, u+1)
if l < sc < r and u < sr < d:
    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()
l = 0
r = W+1
u = 0
d = H+1
for s,t in zip(S[::-1],T[::-1]):
    if s == 'L':
        l += 1
    if s == 'R':
        r -= 1
    if s == 'U':
        u += 1
    if s == 'D':
        d -= 1
    if t == 'L':
        r -= 1
    if t == 'R':
        l += 1
    if t == 'U':
        d -= 1
    if t == 'D':
        u += 1
if l < sc < r and u < sr < d:
    print('YES')
else:
    print('NO')

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 -1
u = h - s2
d = s2 -1

S = list(input())

T = list(input())


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

r = w-s1
l = s1-1

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


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

u = h - s2
d = s2 - 1
for i in range(n):
    if S[i] == "D":
        u += 1
        d -= 1
    if d < 0:
        print("NO")
        exit()
    if T[i] == "U":
        if u != 0:
            d += 1
            u -= 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 = 0
r = W+1
u = 0
d = H+1
for s,t in zip(S[::-1],T[::-1]):
    if s == 'L' and t != 'R':
        l += 1
    if s == 'R' and t != 'L':
        r -= 1
    if s == 'U' and t != 'D':
        u += 1
    if s == 'D' and t != 'U':
        d -= 1
if l < sc < r and u < sr < d:
    print('YES')
else:
    print('NO')

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 -1
u = h - s2
d = s2 -1

S = list(input())

T = list(input())


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

r = w-s1
l = s1-1

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


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

u = h - s2
d = s2
for i in range(n):
    if S[i] == "D":
        u += 1
        d -= 1
    if d < 0:
        print("NO")
        exit()
    if T[i] == "U":
        if u != 0:
            d += 1
            u -= 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 = 0
r = W+1
u = 0
d = H+1
for s,t in zip(S[::-1],T[::-1]):
    if s == 'L' and t != 'R':
        l += 1
    if s == 'R' and t != 'L':
        r -= 1
    if s == 'U' and t != 'D':
        u += 1
    if s == 'D' and t != 'U':
        d -= 1
    if l == r-1 or u == d-1:
        break
if l < sc < r and u < sr < d:
    print('YES')
else:
    print('NO')

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 -1
u = h - s2
d = s2 -1

S = list(input())

T = list(input())


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

r = w-s1
l = s1-1

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


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

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

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 -1
u = h - s2
d = s2 -1

S = list(input())

T = list(input())


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

r = w-s1
l = s1-1

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


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

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

print("YES")

Submission status: Wrong Answer

Solution code:
 
def solve():
    H,W,N= [int(x) for x in input().split()]
     
    Sr,Sc = [int(x) for x in input().split()]
     
    S = input()
    T = input()
     
    minu = Sr
    maxd = Sr
    maxr = Sc
    minl = Sc
    for i in range(N):
        if S[i] == 'U':
            minu -= 1
            if minu == 0:
                return "NO"
        if S[i] == 'D':
            maxd += 1
            if maxd == H+1:
                return "NO"
        if S[i] == 'R':
            maxr += 1
            if maxr == W+1:
                return "NO"
        if S[i] == 'L':
            minl -= 1
            if minl == 0:
                return "NO"
 
        if T[i] == 'D' and minu < H+1:
            minu += 1
        if T[i] == 'U' and maxd > 1:
            maxd -= 1
        if T[i] == 'L' and maxr > 1:
            maxr -= 1
        if T[i] == 'R' and minl < W+1:
            minl += 1
 
    return "YES"
 
print(solve())

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 -1
u = h - s2
d = s2 -1

S = list(input())

T = list(input())


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

r = w-s1
l = s1-1

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


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

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

print("YES")

Submission status: Wrong Answer

Solution code:
 
def solve():
    H,W,N= [int(x) for x in input().split()]
     
    Sr,Sc = [int(x) for x in input().split()]
     
    S = input()
    T = input()
     
    minu = Sr
    maxd = Sr
    maxr = Sc
    minl = Sc
    for i in range(N):
        if S[i] == 'U':
            minu -= 1
            if minu == 0:
                return "NO"
        if S[i] == 'D':
            maxd += 1
            if maxd == H+1:
                return "NO"
        if S[i] == 'R':
            maxr += 1
            if maxr == W+1:
                return "NO"
        if S[i] == 'L':
            minl -= 1
            if minl == 0:
                return "NO"
 
        if T[i] == 'D' and minu < W+1:
            minu += 1
        if T[i] == 'U' and maxd > 1:
            maxd -= 1
        if T[i] == 'L' and maxr > 1:
            maxr -= 1
        if T[i] == 'R' and minl < H+1:
            minl += 1
 
    return "YES"
 
print(solve())

Submission status: Wrong Answer

Solution code:
 
def solve():
    H,W,N= [int(x) for x in input().split()]
     
    Sr,Sc = [int(x) for x in input().split()]
     
    S = input()
    T = input()
     
    minu = Sr
    maxd = Sr
    maxr = Sc
    minl = Sc
    for i in range(N):
        if S[i] == 'U':
            minu -= 1
            if minu == 0:
                return "NO"
        if S[i] == 'D':
            maxd += 1
            if maxd == H+1:
                return "NO"
        if S[i] == 'R':
            maxr += 1
            if maxr == W+1:
                return "NO"
        if S[i] == 'L':
            minl -= 1
            if minl == 0:
                return "NO"
 
        if T[i] == 'D' and minu < W:
            minu += 1
        if T[i] == 'U' and maxd > 1:
            maxd -= 1
        if T[i] == 'L' and maxr > 1:
            maxr -= 1
        if T[i] == 'R' and minl < H:
            minl += 1
 
    return "YES"
 
print(solve())

Submission status: Wrong Answer

Solution code:
H,W,N= [int(x) for x in input().split()]

Sr,Sc = [int(x) for x in input().split()]

S = input()
T = input()


def solve():
    minu = Sr
    maxd = Sr
    maxr = Sc
    minl = Sc
    for i in range(N):
        if S[i] == 'U':
            minu -= 1
            if minu == 0:
                return "NO"
        if S[i] == 'D':
            maxd += 1
            if maxd == W+1:
                return "NO"
        if S[i] == 'R':
            maxr += 1
            if maxr == H+1:
                return "NO"
        if S[i] == 'L':
            minl -= 1
            if minl == 0:
                return "NO"

        if T[i] == 'U' and minu > 1:
            minu -= 1
        if T[i] == 'D' and maxd < W:
            maxd += 1
        if T[i] == 'R' and maxr < H:
            maxr += 1
        if T[i] == 'L' and minl > 1:
            minl -= 1

    return "YES"

print(solve())

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())


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

r = w-s1
l = s1

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


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

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

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":
        r -= 1
        l += 1
    if r < 0:
        print("NO")
        exit()
    if T[i] ==  "L":
        if l != 0 or r != 0:
            r += 1
            l -= 1

r = w-s1
l = s1

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


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

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

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
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 != 0:
            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 != 0:
            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 != 0:
            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 != 0:
            d += 1
            u -= 1
        k2[3] -= 1

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
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 - 1:
            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 - 1:
            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 - 1:
            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 - 1:
            d += 1
            u -= 1
        k2[3] -= 1

print("YES")

Submission status: Wrong Answer

Solution code:
H,W,N= [int(x) for x in input().split()]
 
Sr,Sc = [int(x) for x in input().split()]
 
S = input()
T = input()
 
 
def solve():
    minu = Sr
    maxd = Sr
    maxr = Sc
    minl = Sc
    for i in range(N):
        if S[i] == 'U':
            minu -= 1
            if minu == 0:
                return "NO"
        if S[i] == 'D':
            maxd += 1
            if maxd == W+1:
                return "NO"
        if S[i] == 'R':
            maxr += 1
            if maxr == H+1:
                return "NO"
        if S[i] == 'L':
            minl -= 1
            if minl == 0:
                return "NO"
 
        if T[i] == 'U' and minu > 1:
            minu -= 1
        if T[i] == 'D' and maxd < W:
            maxd += 1
        if T[i] == 'R' and maxr < H:
            maxr += 1
        if T[i] == 'L' and minl > 1:
            minl -= 1
 
    return "YES"
 
print(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="YES"
sU=sD=sL=sR=tU=tD=tL=tR=0
for i in range(n):
        sU += s[i]=="U"
        sD += s[i]=="D"
        sL += s[i]=="L"
        sR += s[i]=="R"
        tU += t[i]=="U"
        tD += t[i]=="D"
        tL += t[i]=="L"
        tR += t[i]=="R"
        if (U-sU+tD)==0 or (D-sD+tU)==0 or (L-sL+tR)==0 or (R-sR+tL)==0:
                win="NO"
                break
print(win)

Submission status: Wrong Answer

Solution code:
H,W,N= [int(x) for x in input().split()]

Sr,Sc = [int(x) for x in input().split()]

S = input()
T = input()


def solve():
    minu = Sr
    maxd = Sr
    maxr = Sc
    minl = Sc
    for i in range(N):
        if S[i] == 'U' and minu > 1:
            minu -= 1
        if S[i] == 'D' and maxd < W:
            maxd += 1
        if S[i] == 'R' and maxr < H:
            maxr += 1
        if S[i] == 'L' and minl > 1:
            minl -= 1
        if T[i] == 'U':
            minu -= 1
            if minu == 0:
                return "NO"
        if T[i] == 'D':
            maxd += 1
            if maxd == W+1:
                return "NO"
        if T[i] == 'R':
            maxr += 1
            if maxr == H+1:
                return "NO"
        if T[i] == 'L':
            minl -= 1
            if minl == 0:
                return "NO"


    return "YES"

print(solve())

Submission status: Wrong Answer

Solution code:
H,W,N= [int(x) for x in input().split()]

Sr,Sc = [int(x) for x in input().split()]

S = input()
T = input()


def solve():
    minu = Sr
    maxd = Sr
    maxr = Sc
    minl = Sc
    for i in range(N):
        if T[i] == 'U':
            minu -= 1
            if minu == 0:
                return "NO"
        if T[i] == 'D':
            maxd += 1
            if maxd == W+1:
                return "NO"
        if T[i] == 'R':
            maxr += 1
            if maxr == H+1:
                return "NO"
        if T[i] == 'L':
            minl -= 1
            if minl == 0:
                return "NO"

        if S[i] == 'U' and minu > 1:
            minu -= 1
        if S[i] == 'D' and maxd < W:
            maxd += 1
        if S[i] == 'R' and maxr < H:
            maxr += 1
        if S[i] == 'L' and minl > 1:
            minl -= 1

    return "YES"

print(solve())