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

Solution (Python):

import sys
def input():
	return sys.stdin.readline().strip()

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

left = 0
right = W
up = 0
down = H

for times in range(N - 1, -1, -1):
	
	direct = T[times]
	if direct == 'L':
		right = min(W, right + 1)
	elif direct == 'R':
		left = max(0, left - 1)
	elif direct == 'U':
		down = min(H, down + 1)
	elif direct == 'D':
		up = max(0, up - 1)
	direct = S[times]
	if direct == 'L':
		left = min(W, left + 1)
	elif direct == 'R':
		right = max(0, right - 1)
	elif direct == 'U':
		up = min(H, up + 1)
	elif direct == 'D':
		down = max(0, down - 1)
	if up >= down or left >= right:
		print("NO")
		exit()


if sc - 1 < right and left <= sc - 1 and sr - 1 >= up and down > sr - 1:
	print("YES")
else:
	print("NO")

Solution (Python):
H,W,N=map(int,input().split())
sr,sc=map(int,input().split())
T=input()
A=input()
t,b,l,r=0,H,0,W
for i in range(N-1,-1,-1):
    if A[i]=='U':
        b=min(b+1,H)
    elif A[i]=='D':
        t=max(t-1,0)
    elif A[i]=='L':
        r=min(r+1,W)
    else:
        l=max(l-1,0)
    if T[i]=='U':
        t+=1
    elif T[i]=='D':
        b-=1
    elif T[i]=='L':
        l+=1
    else:
        r-=1
    if t==b or l==r:
        break
print(['NO','YES'][t<sr<=b and l<sc<=r])

Solution (Python):
h, w, n = map(int, input().split())
y, x = map(int, input().split())
s = input().rstrip()
t = input().rstrip()


def check(pos_dir, neg_dir, minlim, maxlim, start):
    lim = maxlim
    count = 0
    for sc, tc in zip(s[::-1], t[::-1]):
        if tc == pos_dir:
            minlim = max(minlim - 1, 0)
        elif tc == neg_dir:
            maxlim = min(maxlim + 1, lim)
        if sc == pos_dir:
            maxlim -= 1
        elif sc == neg_dir:
            minlim += 1
        count += 1
        if minlim > maxlim:
            return False
    return minlim <= start <= maxlim


for elem in [
    ('R', 'L', 0, w - 1, x - 1),
    ('D', 'U', 0, h - 1, y - 1),
]:
    if not check(*elem):
        print('NO')
        break
else:
    print('YES')

Solution (Python):
h, w, n = map(int, input().split())
y, x = map(int, input().split())
s = input().rstrip()
t = input().rstrip()


def check(direction, opposite_direction, pos, target):
    for i in range(n):
        if s[i] == direction:
            pos += 1
        if pos == target:
            return False
        if t[i] == opposite_direction and pos > 1:
            pos -= 1
    return True


for d, od, pos, target in [
    ('R', 'L', x, w + 1),
    ('L', 'R', w + 1 - x, w + 1),
    ('D', 'U', y, h + 1),
    ('U', 'D', h + 1 - y, h + 1),
]:
    ok = check(d, od, pos, target)
    if not ok:
        print('NO')
        break
else:
    print('YES')

Solution (Python):
H, W, N = map(int, input().split())
sh, sw = map(int, input().split())
TS = input()
AS = input()

TS, AS = TS[::-1], AS[::-1]


def calc(length, start, target_chars):
    alive_section = [1, length]

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

    
    for i in range(N):
        if AS[i] in target_chars:
            j, d = AX[AS[i]]
            alive_section[j] += d

            
            alive_section[0] = max(1, alive_section[0])
            alive_section[1] = min(length, alive_section[1])

        if TS[i] in target_chars:
            j, d = TX[TS[i]]
            alive_section[j] += d

        cross_check(alive_section)

    if not (alive_section[0] <= start <= alive_section[1]):
        print('NO')
        exit()


def cross_check(alive_section):
    if alive_section[0] > alive_section[1]:
        print('NO')
        exit()


calc(H, sh, ['U', 'D'])
calc(W, sw, ['L', 'R'])

print('YES')

Solution (Python):
import collections

h, w, n = map(int, input().split())
y, x = map(int, input().split())
s = input().rstrip()
t = input().rstrip()
counter = collections.Counter()
for c in s:
    counter[c] += 1


def check(counts, direction, opposite_direction, pos, target):
    for i in range(n):
        counts[direction] -= 1
        if s[i] == direction:
            pos += 1
        if pos == target:
            return False
        if t[i] == opposite_direction and pos > 1 and counts[
                opposite_direction] < pos:
            pos -= 1

    return True


for d, od, pos, target in [
    ('R', 'L', x, w + 1),
    ('L', 'R', w + 1 - x, w + 1),
    ('D', 'U', y, h + 1),
    ('U', 'D', h + 1 - y, h + 1),
]:
    ok = check(collections.Counter(counter.items()), d, od, pos, target)
    if not ok:
        print('NO')
        break
else:
    print('YES')

Solution (Python):



import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w,n = LI()
sr,sc = LI()
sr -= 1
sc -= 1
h -= 1
w -= 1
s = input()
t = input()
l,r,d,u = 0,w,0,h
for i in range(n)[::-1]:
    if t[i] == 'R':
        if l > 0:
            l -= 1
    elif t[i] == 'L':
        if r < w:
            r += 1
    elif t[i] == 'D':
        if d > 0:
            d -= 1
    elif t[i] == 'U':
        if u < h:
            u += 1
    
    if s[i] == 'R':
        r -= 1
        if r < 0:
            print('NO')
            quit()
    elif s[i] == 'L':
        l += 1
        if l > w:
            print('NO')
            quit()
    elif s[i] == 'D':
        u -= 1
        if u < 0:
            print('NO')
            quit()
    elif s[i] == 'U':
        d += 1
        if d > h:
            print('NO')
            quit()
    
if l <= sc <= r and d <= sr <= u:
    print('YES')
else:
    print('NO')

Solution (Python):



import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w,n = LI()
sr,sc = LI()
sr -= 1
sc -= 1
h -= 1
w -= 1
s = input()
t = input()
l,r,d,u = 0,w,h,0
for i in range(n)[::-1]:
    if t[i] == 'R':
        if l > 0:
            l -= 1
    elif t[i] == 'L':
        if r < w:
            r += 1
    elif t[i] == 'U':
        if d < h:
            d += 1
    elif t[i] == 'D':
        if u > 0:
            u -= 1
    if s[i] == 'R':
        r -= 1
        if r < 0:
            print('NO')
            quit()
    elif s[i] == 'L':
        l += 1
        if l > w:
            print('NO')
            quit()
    elif s[i] == 'U':
        u += 1
        if u > h:
            print('NO')
            quit()
    elif s[i] == 'D':
        d -= 1
        if d < 0:
            print('NO')
            quit()
    
if l <= sc <= r and u <= sr <= d:
    print('YES')
else:
    print('NO')

Solution (Python):
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
B = [1, W, 1, H]
for i in range(N - 1, -1, -1):
  if T[i] == 'L': B[1] = min(B[1] + 1, W)
  if T[i] == 'R': B[0] = max(B[0] - 1, 1)
  if T[i] == 'U': B[3] = min(B[3] + 1, H)
  if T[i] == 'D': B[2] = max(B[2] - 1, 1)
  if S[i] == 'L': B[0] += 1
  if S[i] == 'R': B[1] -= 1
  if S[i] == 'U': B[2] += 1
  if S[i] == 'D': B[3] -= 1
  if B[1] < B[0] or B[3] < B[2]:
    print('NO')
    break
else:
  print('YES' if B[0] <= sc <= B[1] and B[2] <= sr <= B[3] else 'NO')

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

left = 1
right = W
bottom = 1
top = H

if S[-1] == 'L':
    left += 1
elif S[-1] == 'R':
    right -= 1
elif S[-1] == 'U':
    bottom += 1
elif S[-1] == 'D':
    top -= 1

for i in range(N - 1)[:: -1]:
    if T[i] == 'L':
        right = min(W, right + 1)
    elif T[i] == 'R':
        left = max(1, left - 1)
    elif T[i] == 'U':
        top = min(H, top + 1)
    elif T[i] == 'D':
        bottom = max(1, bottom - 1)

    if S[i] == 'L':
        left += 1
    elif S[i] == 'R':
        right -= 1
    elif S[i] == 'U':
        bottom += 1
    elif S[i] == 'D':
        top -= 1

    if left > right or bottom > top:
        print('NO')
        break
else:
    if left <= sc <= right and bottom <= sr <= top:
        print('YES')
    else:
        print('NO')

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

left = 1
right = w
bot = 1
top = h

if s[-1] == "L":
    left += 1
elif s[-1] == "R":
    right -= 1
elif s[-1] == "U":
    bot += 1
elif s[-1] == "D":
    top -= 1

for i in range(n-2, -1, -1):
    if t[i] == "L":
        right = min(w, right+1)
    elif t[i] == "R":
        left = max(1, left-1)
    elif t[i] == "U":
        top = min(h, top+1)
    elif t[i] == "D":
        bot = max(1, bot-1)
    if s[i] == "L":
        left += 1
    elif s[i] == "R":
        right -= 1
    elif s[i] == "U":
        bot += 1
    elif s[i] == "D":
        top -= 1
    if left > right or bot > top:
        print("NO")
        exit()
if left <= sc <= right and bot <= sr <=top:
    print("YES")
else:
    print("NO")

Solution (Python):
h,w,n = map(int,input().split())
sr,sc = map(int,input().split())
t = input()
s = input()
hei = [1,h]
wid = [1,w]
for i in range(n-1,-1,-1):
  if s[i] == "L":
    wid[1] = min(wid[1]+1,w)
  elif s[i] == "R":
    wid[0] = max(wid[0]-1,1)
  elif s[i] == "U":
    hei[1] = min(hei[1]+1,h)
  elif s[i] == "D":
    hei[0] = max(hei[0]-1,1)
  if t[i] == "L":
    wid[0] += 1
  elif t[i] == "R":
    wid[1] -= 1
  elif t[i] == "U":
    hei[0] += 1
  elif t[i] == "D":
    hei[1] -= 1
  if wid[0]>wid[1] or hei[0]>hei[1]:
    print("NO")
    exit(0)
if hei[0] <= sr <= hei[1] and wid[0] <= sc <= wid[1]:
  print("YES")
else:
  print("NO")

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

sr = H+1-sr


L,R = 1,W
D,U = 1,H
for s,t in zip(S[::-1],T[::-1]):
    if t == 'L': R += 1
    elif t == 'D': U += 1
    elif t == 'R': L -= 1
    elif t == 'U': D -= 1
    
    if L == 0: L = 1
    if D == 0: D = 1
    if R > W: R = W
    if U > H: U = H

    if s == 'L': L += 1
    elif s == 'R': R -= 1
    elif s == 'D': D += 1
    elif s == 'U': U -= 1
    
    if L>R or D>U:
        
        break

bl = (L <= sc <= R) and (D <= sr <= U)
answer = 'YES' if bl else 'NO'
print(answer)

Solution (Python):
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = input()
t = input()
l, u = 1, 1
r = w
d = h
for i in range(n-1, -1, -1):
    if t[i] == 'L':
        r = min(w, r+1)
    if t[i] == 'R':
        l = max(1, l-1)
    if t[i] == 'U':
        d = min(h, d+1)
    if t[i] == 'D':
        u = max(1, u-1)
    if s[i] == 'L':
        l += 1
    if s[i] == 'R':
        r -= 1
    if s[i] == 'U':
        u += 1
    if s[i] == 'D':
        d -= 1
    if l > r or u > d:
        print("NO")
        quit()
print("YES" if l<=sc<=r and u<=sr<=d else "NO")

Solution (Python):

H, W, N = map(int, input().split())
Sr, Sc = map(int, input().split())
Taka = input()
Aoki = input()

mincol = 1
maxcol = W
ans = True
for i in range(N-1, -1, -1):
    if Aoki[i] == "R" and mincol > 1:
        mincol -= 1
    if Aoki[i] == "L" and maxcol < W:
        maxcol += 1

    

    if Taka[i] == "L":
        mincol += 1
    if Taka[i] == "R":
        maxcol -= 1
    
    if mincol > maxcol:
        ans = False
        break
if Sc < mincol or Sc > maxcol:
    ans = False



minrow = 1
maxrow = H
for i in range(N-1, -1, -1):
    if Aoki[i] == "D" and minrow > 1:
        minrow -= 1
    if Aoki[i] == "U" and maxrow < H:
        maxrow += 1
    

    if Taka[i] == "U":
        minrow += 1
    if Taka[i] == "D":
        maxrow -= 1
    
    if minrow > maxrow:
        ans = False
        break

if Sr < minrow or Sr > maxrow:
    ans = False

if ans:
    print("YES")
else:
    print("NO")

Solution (Python):
h,w,n=map(int,input().split())
sy,sx=map(int,input().split())
s=input()
t=input()
for i in range(n):
  tmp1=s[i]
  tmp2=t[i]
flag=True
for i in range(4):
  if i==0 or i==1:
    pos=sx
  elif i==2 or i==3:
    pos=sy
  exist=False
  for j in range(1,n+1):
    tmp1=s[j-1]
    tmp2=t[j-1]
    if i==0:
      if tmp1=='L':
        pos-=1
        if pos==0:
          flag=False
      if tmp2=='R':
        pos+=1
        if pos>w:
          pos=w
      if tmp1=='R':
        if pos==w and exist==True:
          pos=w-1
      if tmp2=='L':
        exist=True
    elif i==1:
      if tmp1=='R':
        pos+=1
        if pos>w:
          flag=False
      if tmp2=='L':
        pos-=1
        if pos==0:
          pos=1
      if tmp1=='L':
        if pos==1 and exist==True:
          pos=2
      if tmp2=='R':
        exist=True
    elif i==2:
      if tmp1=='U':
        pos-=1
        if pos==0:
          flag=False
      if tmp2=='D':
        pos+=1
        if pos>h:
          pos=h
      if tmp1=='D':
        if pos==h and exist==True:
          pos=h-1
      if tmp2=='U':
        exist=True
    elif i==3:
      if tmp1=='D':
        pos+=1
        if pos>h:
          flag=False
      if tmp2=='U':
        pos-=1
        if pos==0:
          pos=1
      if tmp1=='U':
        if pos==1 and exist==True:
          pos=2
      if tmp2=='D':
        exist=True
if flag==True:
  print('YES')
else:
  print('NO')

Solution (Python):
h,w,n=map(int,input().split())
sy,sx=map(int,input().split())
s=input()
t=input()
for i in range(n):
  tmp1=s[i]
  tmp2=t[i]
flag=True
for i in range(4):
  if i==0 or i==1:
    pos=sx
  elif i==2 or i==3:
    pos=sy
  for j in range(1,n+1):
    tmp1=s[j-1]
    tmp2=t[j-1]
    if i==0:
      if tmp1=='L':
        pos-=1
        if pos==0:
          flag=False
      if tmp2=='R':
        pos+=1
        if pos>w:
          pos=w
    elif i==1:
      if tmp1=='R':
        pos+=1
        if pos>w:
          flag=False
      if tmp2=='L':
        pos-=1
        if pos==0:
          pos=1
    elif i==2:
      if tmp1=='U':
        pos-=1
        if pos==0:
          flag=False
      if tmp2=='D':
        pos+=1
        if pos>h:
          pos=h
    elif i==3:
      if tmp1=='D':
        pos+=1
        if pos>h:
          flag=False
      if tmp2=='U':
        pos-=1
        if pos==0:
          pos=1
if flag==True:
  print('YES')
else:
  print('NO')

Solution (Python):
def main():
    H, W, N = map(int, input().split())
    sr, sc = map(int, input().split())
    S = input()
    T = input()
    safe = {"L": 1, "R": W, "U": 1, "D": H}
    inv = {
        "L": "R",
        "R": "L",
        "U": "D",
        "D": "U"
    }
    for i in range(N-1, -1, -1):
        t = T[i]
        if t == "L":
            safe["R"] = min(safe["R"] + 1, W)
        elif t == "U":
            safe["D"] = min(safe["D"] + 1, H)
        elif t == "R":
            safe["L"] = max(safe["L"] - 1, 1)
        elif t == "D":
            safe["U"] = max(safe["U"] - 1, 1)
        if S[i] in ("R", "D"):
            safe[S[i]] -= 1
        else:
            safe[S[i]] += 1

        if safe["D"] < safe["U"] or safe["R"] < safe["L"]:
            print("NO")
            return
    if safe["U"] <= sr <= safe["D"] and safe["L"] <= sc <= safe["R"]:
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    main()

Solution (Python):


import sys


s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    H, W, N = i2nn()
    sr, sc = i2nn()
    S = i2s()
    T = i2s()
    h0 = 1
    h1 = H
    w0 = 1
    w1 = W
    for i, s, t in zip(range(N), S[::-1], T[::-1]):
        if t == 'L':
            w1 += 1 if w1 < W else 0
        elif t == 'R':
            w0 -= 1 if w0 > 1 else 0
        elif t == 'U':
            h1 += 1 if h1 < H else 0
        elif t == 'D':
            h0 -= 1 if h0 > 1 else 0

        if s == 'L':
            w0 += 1
        elif s == 'R':
            w1 -= 1
        elif s == 'U':
            h0 += 1
        elif s == 'D':
            h1 -= 1

        if w0 > w1 or h0 > h1:
            print('NO')
            return
    if w0 <= sc and sc <= w1 and h0 <= sr and sr <= h1:
        print('YES')
    else:
        print('NO')

main()

"""
..
...
"""

Solution (Python):
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
S = input()[::-1]
T = input()[::-1]

r_bound = w
l_bound = 1
d_bound = h
u_bound = 1

remain = True

for s, t in zip(S, T):
    if t =='R' and l_bound > 1:
        l_bound -= 1
    elif t =='L' and r_bound < w:
        r_bound += 1
    elif t =='D' and u_bound > 1:
        u_bound -= 1
    elif t =='U' and d_bound < h:
        d_bound += 1
    if s =='R':
        r_bound -= 1
    elif s =='L':
        l_bound += 1
    elif s =='D':
        d_bound -= 1
    elif s =='U':
        u_bound += 1
    if (r_bound < l_bound) or (d_bound < u_bound):
        remain = False
        break
if (sc < l_bound or sc > r_bound) or (sr < u_bound or sr > d_bound):
    remain = False

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

Solution (Python):

import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


def aok_turn(aoki: str, tolow: str, toup: str, lower: int, upper: int, amin: int, amax: int):
    if aoki == tolow:
        upper = min(amax, upper + 1)
    elif aoki == toup:
        lower = max(amin, lower - 1)

    return lower, upper


def tak_turn(taki: str, tolow: str, toup: str, lower: int, upper: int, amin:int, amax: int):
    if taki == tolow:
        lower = min(amax, lower + 1)
    elif taki == toup:
        upper = max(amin, upper - 1)

    return lower, upper


def on_board(lower: int, upper: int, amin: int, amax: int):
    if lower < upper and upper > amin and lower < amax:
        return True
    else:
        return False


def judge(tak: str, aok: str, tolow: str, toup: str, start: int, amin: int, amax: int) -> bool:
    lower = amin
    upper = amax
    for aoki, taki in zip(aok, tak):
        lower, upper = aok_turn(aoki, tolow, toup, lower, upper, amin, amax)
        lower, upper = tak_turn(taki, tolow, toup, lower ,upper, amin, amax)
        if not on_board(lower, upper, amin, amax):
            return False

    return True if lower <= start < upper else False


h,w,n = li()
sr, sc = li_()
tak = ns()[::-1]
aok = ns()[::-1]

L = 'L'
R = 'R'
U = 'U'
D = 'D'

lr = judge(tak, aok, L, R, sc, 0, w)
ud = judge(tak, aok, U, D, sr, 0, h)

print("YES") if lr and ud else print("NO")

Solution (Python):
H, W, N = map(int, input().split())
s_r, s_c = map(int, input().split())
S = input()
T = input()


up, down = 0, H-1
for i in range(N-1, -1, -1):
    if T[i] == 'U':
        down = min(H-1, down + 1)
    elif T[i] == 'D':
        up = max(0, up - 1)
    
    if S[i] == 'U':
        up += 1
    elif S[i] == 'D':
        down -= 1
    
    if up > down:
        print('NO')
        exit()
        

left, right = 0, W-1
for i in range(N-1, -1, -1):    
    if T[i] == 'L':
        right = min(W-1, right + 1)
    elif T[i] == 'R':
        left = max(0, left - 1)
    
    if S[i] == 'L':
        left += 1
    elif S[i] == 'R':
        right -= 1
    
    if left > right:
        print('NO')
        exit()
if up <= s_r-1 <=down and left <= s_c-1 <= right:
    print('YES')
else:
    print('NO')

Solution (Python):
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 i in range(N-1,-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 L+1==R or U+1==D:
        print("NO")
        break

    if i>0:
        if T[i-1] == "L":
            R = min(W+1,R+1)
        elif T[i-1] == "R":
            L = max(0,L-1)
        elif T[i-1] == "U":
            D = min(H+1,D+1)
        elif T[i-1] == "D":
            U = max(0,U-1)
else:
    if sr<=U or D<=sr or sc<=L or R<=sc:
        print("NO")
    else:
        print("YES")

Solution (Python):
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
flag = True
for s,t in zip(S[::-1],T[::-1]):
    if l+1==r or u+1==d:
        flag=False
        break
    if t == 'L':
        r = min(W+1, r+1)
    if t == 'R':
        l = max(0, l-1)
    if t == 'U':
        d = min(H+1, d+1)
    if t == 'D':
        u = max(0, u-1)
    if s == 'L':
        l += 1
    if s == 'R':
        r -= 1
    if s == 'U':
        u += 1
    if s == 'D':
        d -= 1
if not(l < sc < r and u < sr < d):
    flag = False
if flag:
    print('YES')
else:
    print('NO')

Solution (Python):
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = list(input())
t = list(input())
r, l, u, d = w, 1, 1, h
ns = list(range(n))
for i in ns[::-1]:
    if t[i] == 'L' and r < w:
        r += 1
    elif t[i] == 'R' and l > 1:
        l -= 1
    elif t[i] == 'U' and d < h:
        d += 1
    elif t[i] == 'D' and u > 1:
        u -= 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 (l > r) or (u > d):
        ans = 'NO'
        break
if (l <= sc <= r) and (u <= sr <= d):
    ans = 'YES'
else:
    ans = 'NO'
print(ans)

Solution (Python):
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = list(input())
t = list(input())
sl_move, sr_move, su_move, sd_move = [], [], [], []
tl_move, tr_move, tu_move, td_move = [], [], [], []
for i in s:
    if i == 'L':
        sl_move += [-1]
        sr_move += [0]
        su_move += [0]
        sd_move += [0]
    elif i == 'R':
        sl_move += [0]
        sr_move += [1]
        su_move += [0]
        sd_move += [0]
    elif i == 'U':
        sl_move += [0]
        sr_move += [0]
        su_move += [-1]
        sd_move += [0]
    elif i == 'D':
        sl_move += [0]
        sr_move += [0]
        su_move += [0]
        sd_move += [1]
for i in t:
    if i == 'L':
        tl_move += [-1]
        tr_move += [0]
        tu_move += [0]
        td_move += [0]
    elif i == 'R':
        tl_move += [0]
        tr_move += [1]
        tu_move += [0]
        td_move += [0]
    elif i == 'U':
        tl_move += [0]
        tr_move += [0]
        tu_move += [-1]
        td_move += [0]
    elif i == 'D':
        tl_move += [0]
        tr_move += [0]
        tu_move += [0]
        td_move += [1]
ru, rd, cr, cl = sr, sr, sc, sc
for i in range(n):
    ru += su_move[i]
    if ru < 1:
        ans = 'NO'
        break
    ru += td_move[i]
    if ru > h:
        ru = h
    rd += sd_move[i]
    if rd > h:
        ans = 'NO'
        break
    rd += tu_move[i]
    if rd < 1:
        rd = 1
    cr += sr_move[i]
    if cr > w:
        ans = 'NO'
        break
    cr += tl_move[i]
    if cr < 1:
        cr = 1
    cl += sl_move[i]
    if cl < 1:
        ans = 'NO'
        break
    cl += tr_move[i]
    if cl > w:
        cl = w
    ans = 'YES'
print(ans)

Solution (Python):
def f(h, w, n):
    si, sj = map(int, input().split())
    ss = input()
    tt = input()
    l = u = 1
    r, d = w, h
    for t, s in zip(tt[::-1], ss[::-1]):
        if t == "U" and d < h:
            d += 1
        elif t == "D" and u > 1:
            u -= 1
        elif t == "L" and r < w:
            r += 1
        elif t == "R" and l > 1:
            l -= 1
        if s == "U":
            u += 1
        elif s == "D":
            d -= 1
        elif s == "L":
            l += 1
        else:
            r -= 1
        if u > d or l > r:
            print("NO")
            exit()
    if u <= si <= d and l <= sj <= r:
        print("YES")
    else:
        print("NO")


h, w, n = map(int, input().split())
f(h, w, n)

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

H,W,N=map(int,input().split())

sr,sc=map(int,input().split())
S=input()[:-1]
T=input()[:-1]



left=0
right=W+1
up=0
down=H+1

ans="YES"

for s,t in zip(reversed(S),reversed(T)):
    if t=='R':
        left=max(0,left-1)
    elif t=="L":
        right=min(W+1,right+1)
    elif t=="D":
        up = max(0,up-1)
    else:
        down=min(H+1,down+1)


    if s=="R":
        right-=1
    elif s=="L":
        left+=1
    elif s=="D":
        down-=1
    else:
        up+=1

    
    
    if right-left==1 or down-up==1:
        ans="NO"
        break


if not left<sc<right or not up<sr<down:
    ans="NO"
print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

H,W,N=map(int,input().split())

sr,sc=map(int,input().split())
S=input()[:-1]
T=input()[:-1]



left=0
right=W+1
up=0
down=H+1

ans="YES"
for s,t in zip(reversed(S),reversed(T)):
    if t=='R':
        left=max(0,left-1)
    elif t=="L":
        right=min(W+1,right+1)
    elif t=="D":
        up = max(0,up-1)
    else:
        down=min(H+1,down+1)


    if s=="R":
        right-=1
    elif s=="L":
        left+=1
    elif s=="D":
        down-=1
    else:
        up+=1

    
    if right-left==1 or down-up==1:
        ans="NO"
        break

if not left<sc<right or not up<sr<down:
    ans="NO"
print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

H,W,N=map(int,input().split())

sr,sc=map(int,input().split())
S=input()[:-1]
T=input()[:-1]

left=0
right=W+1
up=0
down=H+1

ans="YES"
for i,(s,t) in enumerate(zip(reversed(S),reversed(T))):
    if i!=0 or True:
        if t=='R':
            left=max(0,left-1)
        elif t=="L":
            right=min(W+1,right+1)
        elif t=="D":
            up = max(0,up-1)
        else:
            down=min(H+1,down+1)


    if s=="R":
        right-=1
    elif s=="L":
        left+=1
    elif s=="D":
        down-=1
    else:
        up+=1

    if right-left==1 or down-up==1:
        ans="NO"
        break

if not left<sc<right or not up<sr<down:
    ans="NO"
print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

H,W,N=map(int,input().split())

sr,sc=map(int,input().split())
S=input()[:-1]
T=input()[:-1]

left=0
right=W+1
up=0
down=H+1

ans="YES"
for i,(s,t) in enumerate(zip(reversed(S),reversed(T))):
    if i!=0:
        if t=='R':
            left=max(0,left-1)
        elif t=="L":
            right=min(W+1,right+1)
        elif t=="D":
            up = max(0,up-1)
        else:
            down=min(H+1,down+1)


    if s=="R":
        right-=1
    elif s=="L":
        left+=1
    elif s=="D":
        down-=1
    else:
        up+=1

    if right-left==1 or down-up==1:
        ans="NO"
        break

if not left<sc<right or not up<sr<down:
    ans="NO"
print(ans)

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


lB, rB, uB, dB = 0, W + 1, 0, H + 1

ans = "YES"
for i, (s, t) in enumerate(zip(reversed(S), reversed(T))):
    if i != 0:
        if t == "R":
            lB = max(0, lB - 1)
        elif t == "L":
            rB = min(W + 1, rB + 1)
        elif t == "D":
            uB = max(0, uB - 1)
        elif t == "U":
            dB = min(H + 1, dB + 1)

    if s == "R":
        rB -= 1
    elif s == "L":
        lB += 1
    elif s == "D":
        dB -= 1
    elif s == "U":
        uB += 1

    if (rB - lB) == 1 or (dB - uB) == 1:
        ans = "NO"
        break

if not lB < sc < rB or not uB < sr < dB:
    ans = "NO"

print(ans)

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

lr, rr = 1, H
lc, rc = 1, W


for i in range(N-1, -1, -1):
    if T[i] == 'L':
        rc = min(rc+1, W)
    elif T[i] == 'R':
        lc = max(lc-1, 1)
    elif T[i] == 'U':
        rr = min(rr+1, H)
    else:
        lr = max(lr-1, 1)

    

    if S[i] == 'L':
        lc += 1
    elif S[i] == 'R':
        rc -= 1
    elif S[i] == 'U':
        lr += 1
    else:
        rr -= 1

    

    if lc > rc or lr > rr:
        print('NO')
        exit()

if not lr <= sr <= rr or not lc <= sc <= rc:
    print('NO')
else:
    print('YES')