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):
h,w,n=map(int,input().split())
sr,sc=map(int,input().split())
s=input()[::-1]
t=input()[::-1]
l,r,u,d=0,w+1,0,h+1
for i in range(n):
 si,ti=s[i],t[i]
 if l+1>=r or u+1>=d:break
 r=min(w+1,r+1)if ti=="L"else r
 l=max(0,l-1)if ti=="R"else l
 d=min(h+1,d+1)if ti=="U"else d
 u=max(0,u-1)if ti=="D"else u
 r-=1 if si=="R"else 0
 l+=1 if si=="L"else 0
 d-=1 if si=="D"else 0
 u+=1 if si=="U"else 0
print("YES"if l<sc<r and u<sr<d else"NO")

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

def check(m,p,l):
    left = 0
    right = l
    for si,ti in zip(s[::-1],t[::-1]):
        if(ti==m):
            right = min(right+1,l)
        elif(ti==p):
            left = max(left-1,0)

        if(si==m):
            left = left+1
        elif(si==p):
            right = right-1

        if(left==right):
            return (-1,-1)

    return (left+1,right+1)

left,right = check('L','R',w)
if(not left<=c<right):
    print('NO')
    exit()

left,right = check('U','D',h)
if(not left<=r<right):
    print('NO')
    exit()

print('YES')

Solution (Python):
h,w,n = list(map(int,input().split()))
sr,sc = list(map(int,input().split()))
s = input()
t = input()
kijyun = "LRUD"
ruisekiS = [{v:0 for v in kijyun} for i in range(n+1)]
ruisekiT = [{v:0 for v in kijyun} for i in range(n+1)]

for i in range(n):
    for c in kijyun:
        if s[i] == c:
            ruisekiS[i+1][c] = ruisekiS[i][c] + 1
        else:
            ruisekiS[i+1][c] = ruisekiS[i][c]
        if t[i] == c:
            ruisekiT[i+1][c] = ruisekiT[i][c] + 1
        else:
            ruisekiT[i+1][c] = ruisekiT[i][c]
kyori = {"L":sc,"R":w-sc+1,"U":sr,"D":h-sr+1}
gyaku = {"L":"R","R":"L","U":"D","D":"U"}

check = 0
for i in range(n):
    for c in kijyun:
        if s[i] == c:
            ruisekiS[i+1][c] = ruisekiS[i][c] + 1
        else:
            ruisekiS[i+1][c] = ruisekiS[i][c]
        if t[i] == c and (ruisekiT[i][c] + 1 - ruisekiS[i][gyaku[c]]) < kyori[c] :
            ruisekiT[i+1][c] = ruisekiT[i][c] + 1
        else:
            ruisekiT[i+1][c] = ruisekiT[i][c]
    if ruisekiS[i+1]["L"] - ruisekiT[i]["R"] >= kyori["L"]:
        check = 1
    if ruisekiS[i+1]["R"] - ruisekiT[i]["L"] >= kyori["R"]:
        check = 1
    if ruisekiS[i+1]["U"] - ruisekiT[i]["D"] >= kyori["U"]:
        check = 1
    if ruisekiS[i+1]["D"] - ruisekiT[i]["U"] >= kyori["D"]:
        check = 1


if check == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

H,W,N = map(int,input().split())
y,x = map(int,input().split())
S = input()
T = input()

def is_out(now,adv,back,L,R):
    for i in range(N):
        if S[i] == adv:
            now += 1
            if now > R:
                return True
        if T[i] == back:
            now = max(now - 1,L)
    return False

ans = is_out(x,'R','L',1,W) | is_out(-x,'L','R',-W,-1) | is_out(y,'D','U',1,H) | is_out(-y,'U','D',-H,-1)
print('NO' if ans else 'YES')

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, W, N = MAP()
sr, sc = MAP()
S = input()
T = input()

l, r, u, d = 1, W, 1, H

for s, t in zip(S[::-1], T[::-1]):
	if t == "L":
		r = min(r+1, W)
	elif t == "R":
		l = max(l-1, 1)
	elif t == "U":
		d = min(d+1, H)
	elif t == "D":
		u = max(u-1, 1)

	if s == "L":
		l += 1
	elif s == "R":
		r -= 1
	elif s == "U":
		u += 1
	elif s == "D":
		d -= 1

	if l > r or u > d:
		print("NO")
		exit()

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, W, N = MAP()
sr, sc = MAP()
S = input()
T = input()

x = sc
for s, t in zip(S, T):  
	if s == "L":
		x -= 1
		if x == 0:
			print("NO")
			exit()
	if t == "R" and x+1 <= W:
		x += 1

x = sc
for s, t in zip(S, T):  
	if s == "R":
		x += 1
		if x == W+1:
			print("NO")
			exit()
	if t == "L" and x-1 >= 1:
		x -= 1

y = sr
for s, t in zip(S, T):  
	if s == "U":
		y -= 1
		if y == 0:
			print("NO")
			exit()
	if t == "D" and y+1 <= H:
		y += 1

y = sr
for s, t in zip(S, T):  
	if s == "D":
		y += 1
		if y == H+1:
			print("NO")
			exit()
	if t == "U" and y-1 >= 1:
		y -= 1

print("YES")

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

Solution (Python):

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

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

taka = [('L','U'),('L','D'),('R','D'),('R','U')]
aoki = [('R','D'),('R','U'),('L','U'),('L','D')]

def sousa(x, s):
    if s == 'L':
        return [x[0], x[1]-1]
    elif s == 'R':
        return [x[0], x[1]+1]
    elif s == 'U':
        return [x[0]-1, x[1]]
    elif s == 'D':
        return [x[0]+1, x[1]]
    return x

def sousa_inv(x, s):
    if s == 'R':
        return [x[0], x[1]-1]
    elif s == 'L':
        return [x[0], x[1]+1]
    elif s == 'D':
        return [x[0]-1, x[1]]
    elif s == 'U':
        return [x[0]+1, x[1]]
    return x


def judge(x):
    if x[0]<= 0 or x[1]<=0:
        return False
    elif x[0]>H or x[1]>W:
        return False
    else:
        return True


for i in range(4):
    cur = [sr, sc]
    for s,t in zip(S,T):
        if s in taka[i]:
            cur = sousa(cur, s)
        if not judge(cur):
            print('NO')
            exit()
        if t in aoki[i]:
            cur = sousa(cur, t)
        if not judge(cur):
            cur = sousa_inv(cur, t)

print('YES')

Solution (Python):
import sys

import math
from fractions import Fraction
import itertools

input=sys.stdin.readline

h,w,n=map(int,input().split())
sr,sc=map(int ,input().split())
s=input()
p=input()

t={"L":0,"R":0,"U":0,"D":0}
a={"L":0,"R":0,"U":0,"D":0}

for i in range(n):
    if s[i]=="L":
        t["L"]+=1
    elif s[i]=="R":
        t["R"]+=1
    elif s[i]=="U":
        t["U"]+=1
    elif s[i]=="D":
        t["D"]+=1
    if t["R"]-a["L"]==w-sc+1 or t["L"]-a["R"]==sc or t["D"]-a["U"]==h-sr+1 or t["U"]-a["D"]==sr:
        print("NO")
        break
    else:
        if p[i]=="L" and a["L"]-t["R"]<sc-1:
            a["L"]+=1
        elif p[i]=="R" and a["R"]-t["L"]<w-sc:
            a["R"]+=1
        elif p[i]=="U" and a["U"]-t["D"]<sr-1:
            a["U"]+=1
        elif p[i]=="D" and a["D"]-t["U"]<h-sr:
            a["D"]+=1
else:
  print("YES")

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    H,W,N = LI()
    R,C = LI()
    S = SI()
    T = SI()

    x = C
    for i in range(N):
        x -= S[i] == 'L'
        if x <= 0:
            print('NO')
            return
        if x < W:
            x += T[i] == 'R'

    x = C
    for i in range(N):
        x += S[i] == 'R'
        if x > W:
            print('NO')
            return
        if x > 1:
            x -= T[i] == 'L'

    x = R
    for i in range(N):
        x -= S[i] == 'U'
        if x <= 0:
            print('NO')
            return
        if x < H:
            x += T[i] == 'D'

    x = R
    for i in range(N):
        x += S[i] == 'D'
        if x > H:
            print('NO')
            return
        if x > 0:
            x -= T[i] == 'U'

    print('YES')

if __name__ == '__main__':
    main()

Solution (Python):

import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    H,W,N=map(int,input().split())
    Y,X=map(int1,input().split())
    T=input()
    A=input()
    d={s:i for i,s in enumerate(['L','R','U','D'])}
    l=[1,0,3,2]
    def check(y,x):
        return 0<=y<H and 0<=x<W
    for k in range(4):
        y,x=Y,X
        for i in range(N):
            if d[T[i]]==k:
                if k==0:
                    x-=1
                elif k==1:
                    x+=1
                elif k==2:
                    y-=1
                else:
                    y+=1
            if not check(y,x):
                print('NO')
                exit()
            if d[A[i]]==l[k]:
                if k==0:
                    x+=1
                elif k==1:
                    x-=1
                elif k==2:
                    y+=1
                else:
                    y-=1
                y=min(max(y,0),H-1)
                x=min(max(x,0),W-1)
    print('YES')
    

if __name__ == '__main__':
    main()

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


now = [Sr, Sc]
for s, t in zip(S, T):
  
  if s == 'L':
    now[1] -= 1
  if s == 'U':
    now[0] -= 1
  
  if now[0] <= 0 or now[1] <= 0:
    print('NO')
    quit()
  
  
  if t == 'R':
    now[1] = min(now[1] + 1, W)
  if t == 'D':
    now[0] = min(now[0] + 1, H)


now = [Sr, Sc]
for s, t in zip(S, T):
  
  if s == 'R':
    now[1] += 1
  if s == 'D':
    now[0] += 1
  
  if H < now[0] or W < now[1]:
    print('NO')
    quit()
  
  
  if t == 'L':
    now[1] = max(now[1] - 1, 1)
  if t == 'U':
    now[0] = max(now[0] - 1, 1)

print('YES')

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

d = {"L": (0, -1), "R": (0, 1), "U": (-1, 0), "D": (1, 0)}


def f(s_dir, t_dir):
    nh = sh
    nw = sw
    sdh, sdw = d[s_dir]
    tdh, tdw = d[t_dir]
    for es, et in zip(s, t):
        if es == s_dir:
            nh += sdh
            nw += sdw
            if nh < 1 or nh > h or nw < 1 or nw > w:
                return True

        if et == t_dir:
            tmph = nh + tdh
            tmpw = nw + tdw
            if 1 <= tmph <= h and 1 <= tmpw <= w:
                nh = tmph
                nw = tmpw

    return False


if f("L", "R") or f("R", "L") or f("U", "D") or f("D", "U"):
    ans = "NO"
else:
    ans = "YES"

print(ans)

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


l = 1
r = w
u = 1
d = h

if s[0] == "L":
    l += 1
elif s[0] == "R":
    r -= 1
elif s[0] == "U":
    u += 1
else:
    d -= 1
for i in range(1,n):
    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)
    else:
        u = max(u-1,1)
    if s[i] == "L":
        l += 1
    elif s[i] == "R":
        r -= 1
    elif s[i] == "U":
        u += 1
    else:
        d -= 1
    if l > r or u > d:
        print("NO")
        exit()

if l <= sc and sc <= r and u <= sr and 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()
r,l,u,d = sc,sc,sr,sr
ans = "YES"
for i,j in zip(s,t):
  if i == "R":
    r+=1
    if r>w:
      ans = "NO"
      break
  elif i =="L":
    l-=1
    if l<1:
      ans = "NO"
      break
  elif i =="U":
    u-=1
    if u<1:
      ans = "NO"
      break
  else:
    d+=1
    if d>h:
      ans = "NO"
      break
  if j == "L":
    r=max(1,r-1)
  elif j == "R":
    l=min(w,l+1)
  elif j == "D":
    u=min(h,u+1)
  else:
    d=max(1,d-1)
print(ans)

Solution (Python):
h,w,n=map(int,input().split())
y,x=map(int,input().split())
ul=y-1
dl=h-y
ll=x-1
rl=w-x
s=input()
t=input()

for i in range(n):
  if s[i]=="U":ul-=1
  if ul<0:exit(print("NO"))
  if t[i]=="D":ul=min(ul+1,h-1)

for i in range(n):
  if s[i]=="D":dl-=1
  if dl<0:exit(print("NO"))
  if t[i]=="U":dl=min(dl+1,h-1)

for i in range(n):
  if s[i]=="L":ll-=1
  if ll<0:exit(print("NO"))
  if t[i]=="R":ll=min(ll+1,w-1)

for i in range(n):
  if s[i]=="R":rl-=1
  if rl<0:exit(print("NO"))
  if t[i]=="L":rl=min(rl+1,w-1)
print("YES")

Solution (Python):
def main():
    H, W, N = map(int, input().split())
    sr, sc = map(int, input().split())
    S = input()
    T = input()
    f = True
    pL, pR, pU, pD = sc, sc, sr, sr
    for i in range(N):
        if S[i] == "L":
            pL -= 1
        elif S[i] == "R":
            pR += 1
        elif S[i] == "U":
            pU -= 1
        else:
            pD += 1
        if pL <= 0 or W < pR or pU <= 0 or H < pD:
            f = False
        if T[i] == "L":
            if 1 < pR:
                pR -= 1
        elif T[i] == "R":
            if pL < W:
                pL += 1
        elif T[i] == "U":
            if 1 < pD:
                pD -= 1
        else:
            if pU < H:
                pU += 1
    if f:
        print("YES")
    else:
        print("NO")

if __name__ == "__main__":
    main()

Solution (Python):
H,W,N=map(int, input().split())
h,w=map(int, input().split())
S=input()
T=input()
h1,h2,w1,w2=H-h+1,H-h+1,w,w
for i in range(N):
  if S[i]=='U':
    h1+=1
  if h1>H:
    print('NO')
    exit()
  if T[i]=='D':
    h1=max(h1-1,1)
 
  if S[i]=='D':
    h2-=1
  if h2<1:
    print('NO')
    exit()
  if T[i]=='U':
    h2=min(h2+1,H)
  
  if S[i]=='R':
    w1+=1
  if w1>W:
    print('NO')
    exit()
  if T[i]=='L':
    w1=max(w1-1,1)
    
  if S[i]=='L':
    w2-=1
  if w2<1:
    print('NO')
    exit()
  if T[i]=='R':
    w2=min(w2+1,W)

print('YES')

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

pos=sc
for i in range(0,N):
    if S[i]=="L":
        pos-=1
        if pos==0:
            print("NO")
            exit()
    if T[i]=="R" and pos!=W:
        pos+=1


pos=sc
for i in range(0,N):
    if S[i]=="R":
        pos+=1
        if pos==W+1:
            print("NO")
            exit()
    if T[i]=="L" and pos!=1:
        pos-=1


pos=sr
for i in range(0,N):
    if S[i]=="U":
        pos-=1
        if pos==0:
            print("NO")
            exit()
    if T[i]=="D" and pos!=H:
        pos+=1


pos=sr
for i in range(0,N):
    if S[i]=="D":
        pos+=1
        if pos==H+1:
            print("NO")
            exit()
    if T[i]=="U" and pos!=1:
        pos-=1

print("YES")

Solution (Python):

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

sr_deque=[]
sc_deque=[]

for i in range(n):
    p=s[i]
    if p=="U":
        
        sr_deque.append((-1,1))
    elif p=="D":
        sr_deque.append((1,1))
    q=t[i]
    if q=="U":
        
        sr_deque.append((-1,2))
    elif q=="D":
        sr_deque.append((1,2))      

for i in range(n):
    p=s[i]
    if p=="R":
        
        sc_deque.append((1,1))
    elif p=="L":
        sc_deque.append((-1,1))
    q=t[i]
    if q=="R":
        
        sc_deque.append((1,2))
    elif q=="L":
        sc_deque.append((-1,2)) 
        
sr_deque=sr_deque[::-1]
sc_deque=sc_deque[::-1]

def judge(start_node,width,deque):
    safe_left=1
    safe_right=width
    flags=True
    for que in deque:
        if que[1]==1:
            
            if que[0]==1:
                if safe_right>0:
                    safe_right-=1
           
            elif que[0]==-1:
                if safe_left<width+1:
                    safe_left+=1 
            
        if que[1]==2:
            
            if que[0]==1:
                if safe_left>1:
                    safe_left-=1
                
                
            elif que[0]==-1:
                if safe_right<width:
                    safe_right+=1
 
        
        if safe_left>safe_right:
            return False
    return safe_left<=start_node<=safe_right
  
def safe():
    return judge(sr,h,sr_deque) and judge(sc,w,sc_deque)
if safe():
    print("YES")
else:
    print("NO")

Solution (Python):
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = input()
t = input()
p1 = sc - 1
p2 = sc - 1
q1 = sr - 1
q2 = sr - 1
for i in range(n):
    if s[i] == 'R':
        p1 += 1
    elif s[i] == 'L':
        p2 -= 1
    elif s[i] == 'U':
        q1 -= 1
    else:
        q2 += 1
    if p1 >= w or p2 < 0 or q1 < 0 or q2 >= h:
        print('NO')
        exit(0)
    if t[i] == 'R':
        p2 += 1
        if p2 >= w:
            p2 -= 1
    elif t[i] == 'L':
        p1 -= 1
        if p1 < 0:
            p1 += 1
    elif t[i] == 'U':
        q2 -= 1
        if q2 < 0:
            q2 += 1
    else:
        q1 += 1
        if q1 >= h:
            q1 -= 1
print('YES')

Solution (Python):
h,w,n = map(int,input().split())
s = list(map(int,input().split()))
S = input()
T = input()

def YES(b):
    if not b:
        print('YES')
    else:
        print('NO')
    exit(0)
    return

R = [s[0],s[0]]
C = [s[1],s[1]]

for i in range(n):
    if S[i] == 'L':
        C[0] -= 1
        if not (1 <= C[0] <= w):YES(True)
    elif S[i] == 'R':
        C[1] += 1
        if not (1 <= C[1] <= w):YES(True)
    elif S[i] == 'U':
        R[0] -= 1
        if not (1 <= R[0] <= h):YES(True)
    elif S[i] == 'D':
        R[1] += 1
        if not (1 <= R[1] <= h):YES(True)
    if T[i] == 'R' and C[0] != w:
        C[0] += 1
    elif T[i] == 'L' and C[1] != 1:
        C[1] -= 1
    elif T[i] == 'D' and R[0] != h:
        R[0] += 1
    elif T[i] == 'U' and R[1] != 1:
        R[1] -= 1
YES(False)

Solution (Python):
H,W,N=map(int,input().split())
sr,sc=map(int,input().split())
S=input()
T=input()
mtl=S.count("L")
mtr=S.count("R")
mtu=S.count("U")
mtd=S.count("D")
mal=T.count("L")
mar=T.count("R")
mau=T.count("U")
mad=T.count("D")
tl,tr,tu,td=0,0,0,0
al,ar,au,ad=0,0,0,0
a=1
for i in range(N):
    if S[i]=="L":
        tl+=1
        if sc-tl+ar==0:
            a=0
            break
    elif S[i]=="R":
        tr+=1
        if sc+tr-al>W:
            a=0
            break
    elif S[i]=="U":
        tu+=1
        if sr-tu+ad==0:
            a=0
            break
    else:
        td+=1
        if sr+td-au>H:
            a=0
            break
    if T[i]=="L":
        al+=1
        if mtr-tr-mal+al>=W:
            a=0
            break
    elif T[i]=="R":
        ar+=1
        if mtl-tl-mar+ar>=W:
            a=0
            break
    elif T[i]=="U":
        au+=1
        if mtd-td-mau+au>=H:
            a=0
            break
    else:
        ad+=1
        if mtu-tu-mad+ad>=H:
            a=0
            break
print("YES" if a else "NO")

Solution (Python):







H,W,N = map(int,input().split())
r,c = map(int,input().split())
S = input()
T = input()


xl = c
for i in range(N):
  if S[i] == "L":
    xl -= 1
    if xl == 0:
      print("NO")
      exit(0)
  if T[i] == "R":
    if xl + 1 <= W:
      xl += 1
    


xr = c
for i in range(N):
  if S[i] == "R":
    xr += 1
    if xr > W:
      print("NO")
      exit(0)
  if T[i] == "L":
    if xr - 1 > 0:
      xr -= 1


xu = r
for i in range(N):
  if S[i] == "U":
    xu -= 1
    if xu == 0:
      print("NO")
      exit(0)
  if T[i] == "D":
    if xu + 1 <= H:
      xu += 1


xd = r
for i in range(N):
  if S[i] == "D":
    xd += 1
    if xd > H:
      print("NO")
      exit(0)
  if T[i] == "U":
    if xd - 1 > 0:
      xd -= 1

print("YES")

Solution (Python):
h,w,n=map(int,input().split())
si,sj=map(int,input().split())
ss=input()
tt=input()
l=1
u=1
r=w
d=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')

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    H, W, N = map(int, rl().split())
    sr, sc = map(lambda x: int(x) - 1, rl().split())
    S = input()
    T = input()
    
    cy = sr
    for i in range(N):
        if S[i] == 'U':
            cy -= 1
            if cy < 0:
                print('NO')
                exit()
        if T[i] == 'D' and cy < H - 1:
            cy += 1
    
    cy = sr
    for i in range(N):
        if S[i] == 'D':
            cy += 1
            if H <= cy:
                print('NO')
                exit()
        if T[i] == 'U' and 0 < cy:
            cy -= 1
    
    cx = sc
    for i in range(N):
        if S[i] == 'L':
            cx -= 1
            if cx < 0:
                print('NO')
                exit()
        if T[i] == 'R' and cx < W - 1:
            cx += 1
    
    cx = sc
    for i in range(N):
        if S[i] == 'R':
            cx += 1
            if W <= cx:
                print('NO')
                exit()
        if T[i] == 'L' and 0 < cx:
            cx -= 1
    
    print('YES')


if __name__ == '__main__':
    solve()

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

h, w,n = map(int, input().split())
r,c = map(int, input().split())
s = input() 
t = input() 


x=c
for i in range(n):
    if s[i] == 'R' : x+=1
    if x == w+1:
        print('NO')
        exit()
    if t[i] == 'L' and x>1 : x-=1


x=c
for i in range(n):
    if s[i] == 'L' : x-=1
    if x == 0:
        print('NO')
        exit()
    if t[i] == 'R' and x<w: x+=1


y=r
for i in range(n):
    if s[i] == 'U' : y-=1
    if y == 0:
        print('NO')
        exit()
    if t[i] == 'D' and y<h: y+=1


y=r
for i in range(n):
    if s[i] == 'D' : y+=1
    if y == h+1:
        print('NO')
        exit()
    if t[i] == 'U' and y>1: y-=1

print('YES')

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

command = []
for s, t in zip(S, T):
    command.append(s)
    command.append(t)

L, U, R, D = sw, sh, sw, sh
taka = 1
for c in command:
    if c == "R":
        if taka:
            R += 1
        else:
            L += 1
            L = min(L, W)
    elif c == "L":
        if taka:
            L -= 1
        else:
            R -= 1
            R = max(R, 1)
    elif c == "U":
        if taka:
            U -= 1
        else:
            D -= 1
            D = max(1, D)
    elif c == "D":
        if taka:
            D += 1
        else:
            U += 1
            U = min(U, H)
    taka = 1 - taka
    if R == W + 1 or L == 0 or U == 0 or D == H + 1:
        print("NO")
        exit()

print("YES")

Solution (Python):
h, w, n = map(int, input().split())
sy, sx = map(lambda x: int(x)-1, input().split())
s, t =  input(), input()
l, r, u, d = sx, sx, sy, sy

for i in range(n):
    if s[i] == "L": l -= 1
    elif s[i] == "R": r += 1
    elif s[i] == "U": u -= 1
    else: d += 1
    if l < 0 or r >= w or u < 0 or d >= h:
        print("NO")
        break
    if t[i] == "L" and r > 0: r -= 1
    elif t[i] == "R" and l < w-1: l += 1
    elif t[i] == "U" and d > 0: d -= 1
    elif t[i] == "D" and u < h-1: u += 1
else: print("YES")

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

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

for s, t in zip(S[::-1], T[::-1]):
    if left + 1 >= right or up + 1 >= down:
        break

    if t == 'L':
        right = min(W + 1, right + 1)
    if t == 'R':
        left = max(0, left - 1)
    if t == 'U':
        down = min(H + 1, down + 1)
    if t == 'D':
        up = max(0, up - 1)

    if s == 'R':
        right -= 1
    if s == 'L':
        left += 1
    if s == 'D':
        down -= 1
    if s == 'U':
        up += 1

print('YES' if left < sw < right and up < sh < down else 'NO')

Solution (Python):
h, w, n = [int(i) for i in input().split()]
sr, sc = [int(i) for i in input().split()]
s = input()
t = input()

ok = True
x = sr
for i, j in zip(s, t):
    if i == "U":
        x -= 1
    if not 1 <= x <= h:
        ok = False
    if j == "D":
        x += 1
    if not 1 <= x <= h:
        x -= 1

x = sr
for i, j in zip(s, t):
    if i == "D":
        x += 1
    if not 1 <= x <= h:
        ok = False
    if j == "U":
        x -= 1
    if not 1 <= x <= h:
        x += 1
x = sc
for i, j in zip(s, t):
    if i == "L":
        x -= 1
    if not 1 <= x <= w:
        ok = False
    if j == "R":
        x += 1
    if not 1 <= x <= w:
        x -= 1

x = sc
for i, j in zip(s, t):
    if i == "R":
        x += 1
    if not 1 <= x <= w:
        ok = False
    if j == "L":
        x -= 1
    if not 1 <= x <= w:
        x += 1

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

Solution (Python):
h, w, n  = map(int, input().split())
sr, sc = map(int, input().split())
S = list(str(input()))
T = list(str(input()))

y = sr
for s,t in zip(S, T):
    if s == 'U':
        y -= 1
        if y < 1:
            print('NO')
            exit()
    if t == 'D':
        if y < h:
            y += 1

y = sr
for s, t in zip(S, T):
    if s == 'D':
        y += 1
        if y > h:
            print('NO')
            exit()
    if t == 'U':
        if y > 1:
            y -= 1

x = sc
for s, t in zip(S, T):
    if s == 'R':
        x += 1
        if x > w:
            print('NO')
            exit()
    if t == 'L':
        if x > 1:
            x -= 1

x = sc
for s, t in zip(S, T):
    if s == 'L':
        x -= 1
        if x < 1:
            print('NO')
            exit()
    if t == 'R':
        if x < w:
            x += 1

print('YES')

Solution (Python):
h, w, n  = map(int, input().split())
sr, sc = map(int, input().split())
S = list(str(input()))
T = list(str(input()))

y = sr
for s,t in zip(S, T):
    if s == 'U':
        y -= 1
        if y < 1:
            print('NO')
            exit()
    if t == 'D':
        if y < h:
            y += 1

y = sr
for s, t in zip(S, T):
    if s == 'D':
        y += 1
        if y > h:
            print('NO')
            exit()
    if t == 'U':
        if y > 1:
            y -= 1

x = sc
for s, t in zip(S, T):
    if s == 'R':
        x += 1
        if x > w:
            print('NO')
            exit()
    if t == 'L':
        if x > 1:
            x -= 1

x = sc
for s, t in zip(S, T):
    if s == 'L':
        x -= 1
        if x < 1:
            print('NO')
            exit()
    if t == 'R':
        if x < w:
            x += 1

print('YES')

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

S = input()
T = input()

def canTakaWin(toS, isInc, toT, R, c):
    now = c

    for s, t in zip(S, T):
        if s == toS:
            now = (now + 1) if isInc else (now - 1)

        if not 0 < now <= R:
            return True

        if t == toT:
            now = (now + 1) if not isInc else (now - 1)
            now = max(1, now)
            now = min(R, now)

    return False

ans = False
ans = ans or canTakaWin('U', False, 'D', H, sh)
ans = ans or canTakaWin('D', True, 'U', H, sh)
ans = ans or canTakaWin('L', False, 'R', W, sw)
ans = ans or canTakaWin('R', True, 'L', W, sw)

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