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:
def examA():
    N = I()
    ans = 0
    print(ans)
    return

def examB():
    H, W, N = LI()
    start = LI()
    S = SI(); T = SI()
    for t,a in [["U","D"],["R","L"]]:
        if t=="U":
            n = H
        else:
            n = W
        # 落ちないエリア
        l = 1; r = n
        if S[-1]==t:
            r -=1
        elif S[-1]==a:
            l +=1
        for i in range(N-2,-1,-1):
            if T[i]==t and l>1:
                l -= 1
            elif T[i]==a and r<n:
                r += 1
            if S[i] == t:
                r -= 1
            elif S[i] == a:
                l += 1
#            print(l,r)
            if t=="U" and (r==0 or l==n):
                print("NO")
                return
            if t=="R" and (r==0 or l==n):
                print("NO")
                return

        if t=="U" and (not l<=start[1]<=r):
            print("NO")
            return
        elif t=="R" and (not l<=start[0]<=r):
            print("NO")
            return
    print("YES")
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examB()

"""

"""

Submission status: Wrong Answer

Solution code:
def examA():
    N = I()
    ans = 0
    print(ans)
    return

def examB():
    H, W, N = LI()
    start = LI()
    S = SI(); T = SI()
    for t,a in [["U","D"],["R","L"]]:
        if t=="U":
            n = H
        else:
            n = W
        # 落ちないエリア
        l = 1; r = n
        if S[-1]==t:
            r -=1
        elif S[-1]==a:
            l +=1
        for i in range(N-2,-1,-1):
            if T[i]==t and l>1:
                l -= 1
            elif T[i]==a and r<n:
                r += 1
            if S[i] == t:
                r -= 1
            elif S[i] == a:
                l += 1
#            print(l,r)
            if t=="U" and (r==0 or l==n):
                print("NO")
                return
            if t=="R" and (r==0 or l==n):
                print("NO")
                return

        if t=="U" and (not l<=start[0]<=r):
            print("NO")
            return
        elif t=="R" and (not l<=start[1]<=r):
            print("NO")
            return
    print("YES")
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examB()

"""

"""

Submission status: Wrong Answer

Solution code:
def examA():
    N = I()
    ans = 0
    print(ans)
    return

def examB():
    H, W, N = LI()
    start = LI()
    S = SI(); T = SI()
    for t,a in [["U","D"],["R","L"]]:
        if t=="U":
            n = H
        else:
            n = W
        l = 0; r = n+1
        if S[-1]==t:
            r -=1
        elif S[-1]==a:
            l +=1
        if r - l <= 1:
            print("NO")
            return
        for i in range(N-2,-1,-1):
            if T[i]==t and l>0:
                l -=1
            elif T[i]==a and r<n+1:
                r +=1
            if S[i] == t:
                r -= 1
            elif S[i] == a:
                l += 1
#            print(l,r)
            if t=="U" and (r<=1 or n<=l):
                print("NO")
                return
            if t=="R" and (r<=1 or n<=l):
                print("NO")
                return
        if t=="U" and (not l<start[0]<r):
            print("NO")
            return
        elif t=="R" and (not l<start[1]<r):
            print("NO")
            return
    print("YES")
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examB()

"""

"""

Submission status: Wrong Answer

Solution code:
def examA():
    N = I()
    ans = 0
    print(ans)
    return

def examB():
    H, W, N = LI()
    start = LI()
    S = SI(); T = SI()
    for t,a in [["U","D"],["R","L"]]:
        if t=="U":
            n = H
        else:
            n = W
        l = 0; r = n+1
        if S[-1]==t:
            r -=1
        elif S[-1]==a:
            l +=1
        if r - l <= 1:
            print("NO")
            return
        for i in range(N-2,-1,-1):
            if T[i]==t:
                l -=1
                l = max(l,0)
            elif T[i]==a:
                r +=1
                r = min(r,n+1)
            if S[i] == t:
                r -= 1
            elif S[i] == a:
                l += 1
#            print(l,r)
            if r-l<=1:
                print("NO")
                return
        if t=="U" and (not l<start[0]<r):
            print("NO")
            return
        elif t=="R" and (not l<start[1]<r):
            print("NO")
            return
    print("YES")
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examB()

"""

"""

Submission status: Wrong Answer

Solution code:
def examA():
    N = I()
    ans = 0
    print(ans)
    return

def examB():
    H, W, N = LI()
    start = LI()
    S = SI(); T = SI()
    for t,a in [["U","D"],["R","L"]]:
        if t=="U":
            n = H
        else:
            n = W
        l = 0; r = n+1
        if S[-1]==t:
            r -=1
        elif S[-1]==a:
            l +=1

        for i in range(N-2,-1,-1):
            if T[i]==t:
                l -=1
                l = max(l,0)
            elif T[i]==a:
                r +=1
                r = min(r,n+1)
            if S[i] == t:
                r -= 1
            elif S[i] == a:
                l += 1
#            print(l,r)
            if r-l<=1:
                print("NO")
                return
        if t=="U" and (not l<start[0]<r):
            print("NO")
            return
        elif t=="R" and (not l<start[1]<r):
            print("NO")
            return
    print("YES")
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examB()

"""

"""

Submission status: Wrong Answer

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

S = S[::-1]
T = T[::-1]

left = 0
right = W - 1
for i in range(N):
  if T[i] == "L":
    right = min(right + 1, W - 1)
  if T[i] == "R":
    left = max(left - 1, 0)
  if S[i] == "L":
    left = left + 1
  if S[i] == "R":
    right = right - 1
  if left > right:
    print("NO")
    exit()
  print(left, right)

if left <= sc - 1<= right:
  1
else:
  print("NO")
  exit()
print("")
  
up = 0
down = H - 1
for i in range(N):
  if T[i] == "U":
    down = min(down + 1, H - 1)
  if T[i] == "D":
    up = max(up - 1, 0)
  if S[i] == "U":
    up = up + 1
  if S[i] == "D":
    down = down - 1
  if up > down:
    print("NO")
    exit()
  print(up, down)
  
if up <= sr - 1 <= down:
  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()

S = S[::-1]
T = T[::-1]

left = 0
right = W - 1
for i in range(N):
  if T[i] == "L":
    right = min(right + 1, W - 1)
  if T[i] == "R":
    left = max(left - 1, 0)
  if S[i] == "L":
    left = left + 1
  if S[i] == "R":
    right = right - 1
  if left > right:
    print("No")
    exit()
  #print(left, right)

if left <= sc - 1<= right:
  1
else:
  print("No")
  exit()
#print("")
  
up = 0
down = H - 1
for i in range(N):
  if T[i] == "U":
    down = min(down + 1, H - 1)
  if T[i] == "D":
    up = max(up - 1, 0)
  if S[i] == "U":
    up = up + 1
  if S[i] == "D":
    down = down - 1
  if up > down:
    print("No")
    exit()
  #print(up, down)
  
if up <= sr - 1 <= down:
  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()

removed = [0,0,0,0]

x = sc
for i in range(N):
    if S[i] == 'L':
        x -= 1
    if x == 0:
        removed[0] = 1
        break
    if T[i] == 'R':
        x = min(x+1,W)

x = sc
for i in range(N):
    if S[i] == 'R':
        x += 1
    if x == W+1:
        removed[1] = 1
        break
    if T[i] == 'L':
        x = max(x-1,1)

y = sr
for j in range(N):
    if S[j] == 'U':
        y += 1
    if y == H+1:
        removed[2] = 1
        break
    if T[j] == 'D':
        y = max(y-1,1)

y = sr
for j in range(N):
    if S[j] == 'D':
        y -= 1
    if y == 0:
        removed[3] = 1
        break
    if T[j] == 'U':
        y = min(y+1,H)

if any(removed):
    print('NO')
else:
    print('YES')

Submission status: Wrong Answer

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

for i in range(N):
    if S[i] == "L":
        if pos[1] <= W//2:
            pos[1] -= 1
    elif S[i] == "R":
        if pos[1] >= W - W//2:
            pos[1] += 1
    elif S[i] == "U":
        if pos[0] <= (H//2):
            pos[0] -= 1
    elif S[i] == "D":
        if pos[0] >= H - H // 2:
            pos[0] += 1

    if pos[0] == 0 or pos[0] == H+1 or pos[1] == 0 or pos[1] == W+1:
        print("NO")
        exit(0)
    if T[i] == "L":
        if pos[1] >= W - (W//2):
            pos[1] -= 1
    elif T[i] == "R":
        if pos[1] <= W//2:
            pos[1] += 1

    elif T[i] == "U":
        if pos[0] >= H - (H//2):
            pos[0] -= 1
    elif T[i] == "D":
        if pos[0] <= H // 2:
            pos[0] += 1
print("YES")

Submission status: Wrong Answer

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

for i in range(N):
    if S[i] == "L":
        if pos[1] <= W - (W//2):
            pos[1] -= 1
    elif S[i] == "R":
        if pos[1] >= W//2:
            pos[1] += 1
    elif S[i] == "U":
        if pos[0] <= H - (H//2):
            pos[0] -= 1
    elif S[i] == "D":
        if pos[0] >= H // 2:
            pos[0] += 1

    if pos[0] == 0 or pos[0] == H+1 or pos[1] == 0 or pos[1] == W+1:
        print("NO")
        exit(0)
    if T[i] == "L":
        if pos[1] >= W - (W//2):
            pos[1] -= 1
    elif T[i] == "R":
        if pos[1] <= W//2:
            pos[1] += 1

    elif T[i] == "U":
        if pos[0] >= H - (H//2):
            pos[0] -= 1
    elif T[i] == "D":
        if pos[0] <= H // 2:
            pos[0] += 1
print("YES")

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sy, sx = map(int, input().split())
l, r = 1, w
d, u = 1, h
for a, b in zip(input()[::-1], input()[::-1]):
	r = min(r + (b == "L"), w) - (a == "R")
	l = max(l - (b == "R"), 1) + (a == "L")
	u = min(u + (b == "D"), h) - (a == "U")
	d = max(d - (b == "U"), 1) + (a == "D")
print("YES" if l <= sx <= r and d <= h - sy + 1 <= u else "NO")

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sy, sx = map(int, input().split())
l, r = 1, w
d, u = 1, h
for a, b in zip(input()[::-1], input()[::-1]):
	r = min(r + (b == "L"), w) - (a == "R")
	l = max(l - (b == "R"), 1) + (a == "L")
	u = min(u + (b == "D"), h) - (a == "U")
	d = max(d - (b == "U"), 1) + (a == "D")
print("YES" if l <= sx <= r and d <= h - sy - 1 <= u else "NO")

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
x, y = map(int, input().split())
x_med = (w + 1) / 2
y_med = (h + 1) / 2
for a, b in zip(input(), input()):
	if a == "R":
		if (x + 1) > x_med:
			x += 1
	elif a == "L":
		if (x - 1) < x_med:
			x -= 1
	elif a == "U":
		if (y + 1) > y_med:
			y += 1
	else:
		if (y - 1) < y_med:
			y -= 1
	if not (0 < x < w + 1 and 0 < y < h + 1):
		print("NO")
		exit()
	if b == "R":
		if x < x_med:
			x += 1
	elif b == "L":
		if x > x_med:
			x -= 1
	elif b == "U":
		if y < y_med:
			y += 1
	else:
		if y > y_med:
			y -= 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 solve() :
    l, r = 1, W
    u, d = 1, H
    
    for i in range(N - 1, -1, -1) :
        if T[i] == 'L' :
            r = min(r + 1, W)
        elif T[i] == 'R' :
            l = max(l - 1, 1)
        elif T[i] == 'U' :
            d = min(d + 1, H)
        elif T[i] == 'd' :
            u = max(u - 1, 1)
            
        if S[i] == 'L' :
            l += 1
        elif S[i] == 'R' :
            r -= 1
        elif S[i] == 'U' :
            u += 1
        elif S[i] == 'D' :
            d -= 1
            
        if r < l or d < u :
            return 'NO'

    if l <= sc <= r and u <= sr <= d :
        return 'YES'
    else :
        return 'NO'
    
print(solve())

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

def solve() :
    l, r = 1, W
    d, u = 1, H
    
    for i in range(N - 1, -1, -1) :
        if T[i] == 'L' :
            r = min(r + 1, W)
        elif T[i] == 'R' :
            l = max(l - 1, 1)
        elif T[i] == 'D' :
            u = min(u + 1, H)
        elif T[i] == 'U' :
            d = max(d - 1, 1)
            
        if S[i] == 'L' :
            l += 1
        elif S[i] == 'R' :
            r -= 1
        elif S[i] == 'D' :
            d += 1
        elif S[i] == 'U' :
            u -= 1
            
        if r < l or u < d :
            return 'NO'

    if l <= sc <= r and d <= sr <= u :
        return 'YES'
    else :
        return 'NO'
    
print(solve())

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

def solve() :
    l, r = 1, W
    d, u = 1, H
    
    for i in range(N - 1, -1, -1) :
        if T[i] == 'L' :
            r = min(r + 1, W)
        elif T[i] == 'R' :
            l = max(l - 1, 1)
        elif T[i] == 'D' :
            u = min(u + 1, H)
        elif T[i] == 'U' :
            d = max(d - 1, 1)
            
        if S[i] == 'L' :
            l += 1
        elif S[i] == 'R' :
            r -= 1
        elif S[i] == 'D' :
            d += 1
        elif S[i] == 'U' :
            u -= 1
            
        if r < l or u < d or W < l or r < 1 :
            return 'NO'

    if l <= sc <= r and d <= sr <= u :
        return 'YES'
    else :
        return 'NO'
    
print(solve())

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

def solve() :
    l, r = 1, W
    d, u = 1, H
    
    for i in range(N - 1, -1, -1) :
        if S[i] == 'L' :
            l = l + 1
        elif S[i] == 'R' :
            r = r - 1
        elif S[i] == 'D' :
            d = d + 1
        elif S[i] == 'U' :
            u = u - 1
            
        if r < l or u < d :
            return 'NO'
        
        if i == 0 :
            break
        
        if T[i-1] == 'L' :
            r = min(r + 1, W)
        elif T[i-1] == 'R' :
            l = max(l - 1, 1)
        elif T[i-1] == 'D' :
            u = min(u + 1, H)
        elif T[i-1] == 'U' :
            d = max(d - 1, 1)

    if l <= sc <= r and d <= sr <= u :
        return 'YES'
    else :
        return 'NO'
    
print(solve())

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

def solve() :
    l, r = 1, W
    d, u = 1, H
    
    for i in range(N - 1, 0, -1) :
        if S[i] == 'L' :
            l = l + 1
        elif S[i] == 'R' :
            r = r - 1
        elif S[i] == 'D' :
            d = d + 1
        elif S[i] == 'U' :
            u = u - 1
            
        if r < l or u < d :
            return 'NO'
            
        if T[i-1] == 'L' :
            r = min(r + 1, W)
        elif T[i-1] == 'R' :
            l = max(l - 1, 1)
        elif T[i-1] == 'D' :
            u = min(u + 1, H)
        elif T[i-1] == 'U' :
            d = max(d - 1, 1)

    if l <= sc <= r and d <= sr <= u :
        return 'YES'
    else :
        return 'NO'
    
print(solve())

Submission status: Wrong Answer

Solution code:
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 solve() :
    sdp = [[0] * (N + 1) for _ in range(4)]
    tdp = [[0] * (N + 1) for _ in range(4)]

    d = {'L': 0, 'R': 1, 'U': 2, 'D': 3}
    for i in range(1, N + 1) :
        for j in range(4) :
            sdp[j][i] += sdp[j][i - 1]
            tdp[j][i] += tdp[j][i - 1]
            
        sdp[d[S[i-1]]][i] += 1
        tdp[d[T[i-1]]][i] += 1
    
    for i in range(N) :
        l = sdp[0][-1] - sdp[0][i]
        r = sdp[1][-1] - sdp[1][i]
        u = sdp[2][-1] - sdp[2][i]
        d = sdp[3][-1] - sdp[3][i]
        
        tla, tlb = tdp[0][i], tdp[0][-1] - tdp[0][i]
        tra, trb = tdp[1][i], tdp[1][-1] - tdp[1][i]
        tua, tub = tdp[2][i], tdp[2][-1] - tdp[2][i]
        tda, tdb = tdp[3][i], tdp[3][-1] - tdp[3][i]
        
        if sc + r - (tla + tlb) > W or sc - (sc + r - tlb - W) - l + trb < 1 or sc - (sc + r - tlb - W) < 1 :
            return 'NO'
        if sc - l + (tra + trb) < 1 or sc + (-sc + l - trb + 1) + r - tlb > W or sc + (-sc + l - trb + 1) > W:
            return 'NO'
        if sr + u - (tda + tdb) > H or sr - (sr + u - tdb - H) - d + tub < 1 or sr - (sr + u - tdb - H) < 1:
            return 'NO'
        if sr - d + (tua + tub) < 1 or sr + (-sr + d - tub + 1) + u - tdb > H or sr + (-sr + d - tub + 1) > H :
            return 'NO'
            
    return 'YES'
    
print(solve())

Submission status: Wrong Answer

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

def solve() :
    sdp = [[0] * (N + 1) for _ in range(4)]
    tdp = [[0] * (N + 1) for _ in range(4)]

    d = {'L': 0, 'R': 1, 'U': 2, 'D': 3}
    for i in range(1, N + 1) :
        for j in range(4) :
            sdp[j][i] += sdp[j][i - 1]
            tdp[j][i] += tdp[j][i - 1]
            
        sdp[d[S[i-1]]][i] += 1
        tdp[d[T[i-1]]][i] += 1
    
    for i in range(N) :
        l = sdp[0][-1] - sdp[0][i]
        r = sdp[1][-1] - sdp[1][i]
        u = sdp[2][-1] - sdp[2][i]
        d = sdp[3][-1] - sdp[3][i]
        
        tla, tlb = tdp[0][i], tdp[0][-1] - tdp[0][i]
        tra, trb = tdp[1][i], tdp[1][-1] - tdp[1][i]
        tua, tub = tdp[2][i], tdp[2][-1] - tdp[2][i]
        tda, tdb = tdp[3][i], tdp[3][-1] - tdp[3][i]
        
        if sc + r - (tla + tlb) > W or sc - (sc + r - tlb - W) - l + trb < 1 :
            return 'NO'
        if sc - l + (tra + trb) < 1 or sc + (-sc + l - trb + 1) + r - tlb > W :
            return 'NO'
        if sr + u - (tda + tdb) > H or sr - (sr + u - tdb - H) - d + tub < 1 :
            return 'NO'
        if sr - d + (tua + tub) < 1 or sr + (-sr + d - tub + 1) + u - tdb > H :
            return 'NO'
            
    return 'YES'
    
print(solve())

Submission status: Wrong Answer

Solution code:
print("NO")

Submission status: Wrong Answer

Solution code:
H, W, N = map(int,input().split())
pos = list(map(int,input().split()))
T, A = input(), input()
for t,a in zip(T,A):
  if t=='L':
    if pos[0] <= W//2: pos[0] -= 1
  elif t == 'R':
    if pos[0] > W-W//2: pos[0] += 1
  elif t == 'U':
    if pos[1] > H-H//2: pos[1] += 1
  elif t == 'D':
    if pos[1] <= H//2: pos[1] -= 1
  
  if pos[0]<=0 or W+1<=pos[0] or pos[1]<=0 or H+1<=pos[1]:
    print('NO')
    exit()

  if a=='L':
    if pos[0] > W-W//2: pos[0] -= 1
  elif a == 'R':
    if pos[0] <= W//2: pos[0] += 1
  elif a == 'U':
    if pos[1] <= H//2: pos[1] += 1
  elif a == 'D':
    if pos[1] > H-H//2: pos[1] -= 1

print('YES')

Submission status: Wrong Answer

Solution code:
H, W, N = map(int,input().split())
pos = list(map(int,input().split()))
T, A = input(), input()
for t,a in zip(T,A):
  if t=='L':
    if pos[0] <= W//2: pos[0] -= 1
  elif t == 'R':
    if pos[0] >= W-W//2: pos[0] += 1
  elif t == 'U':
    if pos[1] >= H-H//2: pos[1] += 1
  elif t == 'D':
    if pos[1] <= H//2: pos[1] -= 1
  
  if pos[0]<=0 or W+1<=pos[0] or pos[1]<=0 or H+1<=pos[1]:
    print('NO')
    exit()

  if a=='L':
    if pos[0] >= W-W//2: pos[0] -= 1
  elif a == 'R':
    if pos[0] <= W//2: pos[0] += 1
  elif a == 'U':
    if pos[1] <= H//2: pos[1] += 1
  elif a == 'D':
    if pos[1] >= H-H//2: pos[1] -= 1

print('YES')

Submission status: Wrong Answer

Solution code:
H, W, N = map(int,input().split())
pos = list(map(int,input().split()))
T, A = input(), input()
for t,a in zip(T,A):
  if t=='L':
    if pos[0] <= W//2: pos[0] -= 1
  elif t == 'R':
    if pos[0] >= W//2: pos[0] += 1
  elif t == 'U':
    if pos[1] >= H//2: pos[1] += 1
  elif t == 'D':
    if pos[1] <= H//2: pos[1] -= 1
  
  if pos[0]<=0 or W+1<=pos[0] or pos[1]<=0 or H+1<=pos[1]:
    print('NO')
    exit()

  if a=='L':
    if pos[0] > W//2: pos[0] -= 1
  elif a == 'R':
    if pos[0] < W//2: pos[0] += 1
  elif a == 'U':
    if pos[1] < H//2: pos[1] += 1
  elif a == 'D':
    if pos[1] > H//2: pos[1] -= 1

print('YES')

Submission status: Wrong Answer

Solution code:
H, W, N = map(int,input().split())
pos = list(map(int,input().split()))
T, A = input(), input()
for t,a in zip(T,A):
  if t=='L':
    if pos[0] <= W//2: pos[0] -= 1
  elif t == 'R':
    if pos[0] >= W//2: pos[0] += 1
  elif t == 'U':
    if pos[1] >= H//2: pos[1] += 1
  elif t == 'D':
    if pos[1] <= H//2: pos[1] -= 1
  
  if pos[0]<=0 or W+1<=pos[0] or pos[1]<=0 or H+1<=pos[1]:
    print('NO')
    exit()

  if a=='L':
    if pos[0] >= W//2: pos[0] -= 1
  elif a == 'R':
    if pos[0] <= W//2: pos[0] += 1
  elif a == 'U':
    if pos[1] <= H//2: pos[1] += 1
  elif a == 'D':
    if pos[1] >= H//2: pos[1] -= 1

print('YES')

Submission status: Wrong Answer

Solution code:
import time
st_time=time.time()

from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
ts=time.time()
sys.setrecursionlimit(10**6)
#input=sys.stdin.readline
show_flg=False
#show_flg=True

di='URLD'
mp=dict(zip(list(di),range(4)))
rv=dict(zip(list(di),list(di[::-1])))

h,w,n=LI()
b=[h,w,w,h]
r,c=LI()
st=[h-r,c,w-c,r]
s=input()
t=[rv[i] for i in input()]
ans=True
for j,i in mp.items():
    c=st[i]
    for k in range(n):
        c+=1*(s[k]==j)
        if c>b[i]:
            ans=False
        if c>1:
            c-=1*(t[k]==j)
            
show(s)
show(t)
show(mp,rv,di)
if ans:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
import time
st_time=time.time()

from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
ts=time.time()
sys.setrecursionlimit(10**6)
#input=sys.stdin.readline
show_flg=False
#show_flg=True

di='URLD'
mp=dict(zip(list(di),range(4)))
rv=dict(zip(list(di),list(di[::-1])))

h,w,n=LI()
b=[h,w,w,h]
r,c=LI()
st=[h-r,c,w-c,r]
s=input()
t=[rv[i] for i in input()]
ans=True
for j,i in mp.items():
    c=st[i]
    for k in range(n):
        c+=1*(s[k]==j)
        if c>1:
            c-=1*(t[k]==j)
        if c>b[i]:
            ans=False
            
show(s)
show(t)
show(mp,rv,di)
if ans:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
import time
st_time=time.time()

from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
ts=time.time()
sys.setrecursionlimit(10**6)
#input=sys.stdin.readline
show_flg=False
#show_flg=True

h,w,n=LI()
b=[h,w,w,h]
r,c=LI()
st=[h-r,c,w-c,r]
s=input()
t=input()
cs=[[0]for i in range(4)]
ct=[[0]for i in range(4)]
di='URLD'
di=dict(zip(list(di),range(4)))
show(s)
show(t)
show(di)
for j,i in di.items():
    for k in range(n):
        cs[i].append(cs[i][-1]+(1 if s[k]==j else 0))
        ct[i].append(ct[i][-1]+(1 if t[k]==j else 0))
ans=True
show(cs,ct)
for i in range(4):
    if max([(j-k)+st[i] for j,k in zip(cs[i],[0]+ct[3-i])])>b[i]:
        ans=False
if ans:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
import time
st_time=time.time()

from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
ts=time.time()
sys.setrecursionlimit(10**6)
#input=sys.stdin.readline
show_flg=False
#show_flg=True

h,w,n=LI()
b=[h,w,w,h]
r,c=LI()
st=[h-r,c,w-c,r]
s=input()
t=input()
cs=[[0]for i in range(4)]
ct=[[0]for i in range(4)]
di='URLD'
di=dict(zip(list(di),range(4)))
show(s)
show(t)
show(di)
for j,i in di.items():
    for k in range(n):
        cs[i].append(cs[i][-1]+(1 if s[k]==j else 0))
        ct[i].append(ct[i][-1]+(1 if t[k]==j else 0))
ans=False
show(cs,ct)
for i in range(4):
    if max([(j-k)+st[i] for j,k in zip(cs[i],[0]+ct[3-i])])>b[i]:
        ans=True
if ans:
    print('YES')
else:
    print('NO')