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):
def is_keepable(size, sub, add, start):
    small, big = 1, size
    for i in range(N - 1, -1, -1):
        if T[i] == sub:
            big = min(big + 1, size)
        elif T[i] == add:
            small = max(small - 1, 1)
        if S[i] == sub:
            small += 1
        elif S[i] == add:
            big -= 1
        if small > big:
            return False
    return small <= start <= big


H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
if is_keepable(H, "U", "D", sr) and is_keepable(W, "L", "R", sc):
    print("YES")
else:
    print("NO")

Solution (Python):
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()[::-1]
T = input()[::-1]
l = u = 1
r = W
d = H
for s, t in zip(S,T):
    if t == "U":
        d = min(d + 1, H)
    elif t == "D":
        u = max(1, u - 1)
    elif t == "R":
        l = max(1, l - 1)
    else:
        r = min(r + 1, W)


    if s == "U":
        u += 1
    elif s == "D":
        d -= 1
    elif s == "R":
        r -= 1
    else:
        l += 1
    if l > r or u > d or l > W or r < 1 or u > H or d < 1:
        print("NO")
        break
else:
    if u <= sr <= d and l <= sc <= r:
        print("YES")
    else:
        print("NO")

Solution (Python):
H, W, N = map(int, input().split())
*s, = map(lambda x: int(x) - 1, input().split())
S = input()
T = input()
l = t = 0
b = H
r = W
if S[-1] == 'L':
    l += 1
elif S[-1] == 'R':
    r -= 1
elif S[-1] == 'U':
    t += 1
else:
    b -= 1

for i in range(N - 2, -1, -1):
    a = T[i]
    if a == 'L':
        r += 1
        r = min(r, W)
    elif a == 'R':
        l -= 1
        l = max(l, 0)
    elif a == 'U':
        b += 1
        b = min(b, H)
    else:
        t -= 1
        t = max(t, 0)
    a = S[i]
    if a == 'L':
        l += 1
    elif a == 'R':
        r -= 1
    elif a == 'U':
        t += 1
    else:
        b -= 1
    if b <= t or r <= l:
        print('NO')
        exit()

print('YES' if t <= s[0] < b and l <= s[1] < r else 'NO')

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

y,x = y-1, x-1
xmin, xmax, ymin, ymax = 0,W-1, 0,H-1
S = S[::-1]
T = T[::-1]

d = {
  'L': (1,0),
  'R': (-1,0),
  'U': (0,1),
  'D': (0,-1),
}
 
for i in range(N):
  dx,dy = d[T[i]]
  xmin = xmin + dx if dx < 0 else xmin
  xmax = xmax + dx if dx > 0 else xmax
  ymin = ymin + dy if dy < 0 else ymin
  ymax = ymax + dy if dy > 0 else ymax

  if xmin < 0: xmin = 0
  if xmax >= W: xmax = W-1
  if ymin < 0: ymin = 0
  if ymax >= H: ymax = H-1

  dx,dy = d[S[i]]
  xmin = xmin + dx if dx > 0 else xmin
  xmax = xmax + dx if dx < 0 else xmax
  ymin = ymin + dy if dy > 0 else ymin
  ymax = ymax + dy if dy < 0 else ymax
  
  
  if xmin > xmax or ymin > ymax:
    print("NO")
    
    
    exit(0)

if xmin <= x <= xmax and ymin <= y <= ymax:
  print("YES")
else:
  print("NO")

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

d=[0,h-1,0,w-1]
f=True
for i in range(n-1,-1,-1):
  if t[i]=="D":
    d[0]=max(d[0]-1,0)
  if t[i]=="U":
    d[1]=min(d[1]+1,h-1)
  if t[i]=="R":
    d[2]=max(d[2]-1,0)
  if t[i]=="L":
    d[3]=min(d[3]+1,w-1)
  
  
  if s[i]=="U":
    d[0]+=1
  if s[i]=="D":
    d[1]-=1
  if s[i]=="L":
    d[2]+=1
  if s[i]=="R":
    d[3]-=1
  if d[0]>d[1] or d[2]>d[3]:
    f=False
    break

if f and d[0]<=x and x<=d[1] and d[2]<=y and y<=d[3]:
  print("YES")
else:
  print("NO")

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

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

for i in range(n):
    
    if t[i] == "R":
        l = max(1,l-1)
    elif t[i] == "L":
        r = min(w,r+1)
    elif t[i] == "D":
        u = max(1,u-1)
    else:
        d = min(h,d+1)
    if s[i] == "R":
        r -= 1
    elif s[i] == "L":
        l += 1
    elif s[i] == "D":
        d -= 1
    else:
        u += 1
    if r < l or d < u:
        print("NO")
        exit()

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

Solution (Python):
import functools
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
import random
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


H, W, N = read_values()
YP, XP = read_index()
S = input().strip()
T = input().strip()


def main():
    X = [0, W]
    Y = [0, H]
    for t, s in zip(T[::-1], S[::-1]):
        if t == "L":
            X[1] = min(W, X[1] + 1)
        elif t == "R":
            X[0] = max(0, X[0] - 1)
        elif t == "U":
            Y[1] = min(H, Y[1] + 1)
        elif t == "D":
            Y[0] = max(0, Y[0] - 1)

        if s == "L":
            X[0] += 1
        elif s == "R":
            X[1] -= 1
        elif s == "U":
            Y[0] += 1
        elif s == "D":
            Y[1] -= 1

        if X[0] >= X[1] or Y[0] >= Y[1]:
            print("NO")
            return

    if X[0] <= XP < X[1] and Y[0] <= YP < Y[1]:
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.buffer.readline




def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return float(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))



def main():
	h,w,n=MI()
	sr,sc=MI()
	S=RD()
	T=RD()

	ans="YES"

	
	x=w-sc+1
	for i,j in zip(S,T):
		if i=="R":
			x-=1
			if x==0:
				ans="NO"
		if j=="L":
			if x<w:
				x+=1

	
	x=sc
	for i,j in zip(S,T):
		if i=="L":
			x-=1
			if x==0:
				ans="NO"
		if j=="R":
			if x<w:
				x+=1

	
	x=h-sr+1
	for i,j in zip(S,T):
		if i=="D":
			x-=1
			if x==0:
				ans="NO"
		if j=="U":
			if x<h:
				x+=1

	
	x=sr
	for i,j in zip(S,T):
		if i=="U":
			x-=1
			if x==0:
				ans="NO"
		if j=="D":
			if x<h:
				x+=1

	print(ans)










if __name__ == "__main__":
	main()

Solution (Python):
h,w,n=map(int,input().split())
sh,sw=map(int,input().split())
s=input()
t=input()
r='RLDU'
q='LRUD'
p=dict(zip(r,[w-sw+1,sw,h-sh+1,sh]))
ds=dict(zip(r,[0]*4))
dt=dict(zip(r,[0]*4))
for i,j in zip(s,t):
  ds[i]+=1
  for k,l in zip(r,q):
    if ds[k]-dt[l]>=p[k]:
      print('NO')
      exit()
  dt[j]+=1
  for k in r:
    dt[k]=min(dt[k],p[k]+ds[k]-1)
print('YES')

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


cur = sc
for i in range(n):
    if s[i] == "R":
        cur += 1
    if cur > w:
        fl = False
    if t[i] == "L" and cur > 2:
        cur -= 1
        

cur = sc
for i in range(n):
    if s[i] == "L":
        cur -= 1
    if cur <= 0:
        fl = False
    if t[i] == "R" and cur < w:
        cur += 1
        

cur = sr
for i in range(n):
    if s[i] == "U":
        cur -= 1
    if cur <= 0:
        fl = False
    if t[i] == "D" and cur < h:
        cur += 1
        

cur = sr
for i in range(n):
    if s[i] == "D":
        cur += 1
    if cur > h:
        fl = False
    if t[i] == "U" and cur > 2:
        cur -= 1
        
if fl:
    print("YES")
else:
    print("NO")

Solution (Python):
H, W, N = map(int, input().split())
x, y = map(int, input().split())
S = input().rstrip()
T = input().rstrip()
L, R, U, D = 1, W, 1, H
for s, t in zip(reversed(S),reversed(T)):
    if t=="L": R = min(R+1,W)
    if t=="R": L = max(L-1,1)
    if t=="U": D = min(D+1,H)
    if t=="D": U = max(U-1,1)
    if s=="L": L = L+1
    if s=="R": R = R-1
    if s=="U": U = U+1
    if s=="D": D = D-1
    if L>R or U>D:
        print("NO")
        exit()
if L<=y<=R and U<=x<=D:
    print("YES")
else:
    print("NO")

Solution (Python):

max2 = lambda x,y: x if x > y else y
min2 = lambda x,y: x if x < y else y

def solve(H,W,start,S,T):
    limit = [W,H]
    offsets = [0]*4

    for s,t in zip(reversed(S),reversed(T)):
        offsets[t^1] = max2(offsets[t^1]-1,0)
        offsets[s] += 1
        if offsets[s]+offsets[s^1] >= limit[s//2]:
            return False
    h,w = start
    l,r,u,d = offsets
    return l <= w < W-r and u <= h < H-d



if __name__ == '__main__':
    H,W,N = map(int,input().split())
    start = tuple(map(lambda x: int(x)-1,input().split()))

    d = {'L':0,'R':1,'U':2,'D':3}

    S = tuple(d[c] for c in input())
    T = tuple(d[c] for c in input())


    print('YES' if solve(H,W,start,S,T) else 'NO')

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


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

    c = dict()
    c["L"], c["R"] = sc, sc
    c["U"], c["D"] = sr, sr

    for first, second in zip(s, t):
        if first == "L" or first == "U":
            c[first] -= 1
        else:
            c[first] += 1

        if c["L"] == 0 or c["R"] == w + 1 or c["U"] == 0 or c["D"] == h + 1:
            return print("NO")

        if second == "L":
            c["R"] -= 1
            c["R"] = max(1, c["R"])
        if second == "R":
            c["L"] += 1
            c["L"] = min(w, c["L"])
        if second == "U":
            c["D"] -= 1
            c["D"] = max(1, c["D"])
        if second == "D":
            c["U"] += 1
            c["U"] = min(h, c["U"])

    print("YES")


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
from collections import *

H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()[:-1]
T = input()[:-1]
U, D, L, R = 0, H-1, 0, W-1

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

if U<=sr-1<=D and L<=sc-1<=R:
    print('YES')
else:
    print('NO')

Solution (Python):
def main():
    H, W, N = list(map(int, input().split()))
    sr, sc = list(map(lambda x: int(x) - 1, input().split()))
    S = input()
    T = input()
    
    ok_left, ok_right = 0, W - 1
    ok_up, ok_down = 0, H - 1
    for n in range(N - 1, -1, -1):
        s = S[n]
        if s == 'L':
            ok_left += 1
        if s == 'R':
            ok_right -= 1
        if s == 'U':
            ok_up += 1
        if s == 'D':
            ok_down -= 1
        if ok_left > ok_right or ok_up > ok_down:
            print('NO')
            return
        if n > 0:
            t = T[n - 1]
            if t == 'L':
                ok_right = min(ok_right + 1, W - 1)
            if t == 'R':
                ok_left = max(ok_left - 1, 0)
            if t == 'U':
                ok_down = min(ok_down + 1, H - 1)
            if t == 'D':
                ok_up = max(ok_up - 1, 0)
    if ok_left <= sc <= ok_right and ok_up <= sr <= ok_down:
        print('YES')
    else:
        print('NO')


if __name__ == '__main__':
    main()

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    H, W, n = map(int, input().split())
    sr, sc = map(int, input().split())
    S = input()[::-1]
    T = input()[::-1]

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

        if s == "R":
            right -= 1
        elif s == "L":
            left += 1
        elif s == "U":
            up -= 1
        else:
            bottom += 1

        if left > right or bottom > up:
            print("NO")
            break
    else:
        print("YES" if left <= sc <= right and bottom <= H - sr + 1 <= up else "NO")


if __name__ == '__main__':
    resolve()

Solution (Python):
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = input()
t = input()
ll = rr = sc
uu = dd = sr
for ss, tt in zip(s, t):
    if ss == "L":
        ll -= 1
        if ll == 0:
            print("NO")
            exit()
    elif ss == "R":
        rr += 1
        if rr == w + 1:
            print("NO")
            exit()
    elif ss == "U":
        uu -= 1
        if uu == 0:
            print("NO")
            exit()
    else:
        dd += 1
        if dd == h + 1:
            print("NO")
            exit()
            
    if tt == "L":
        rr -= 1
        rr = max(rr, 1)
    elif tt == "R":
        ll += 1
        ll = min(ll, w)
    elif tt == "U":
        dd -= 1
        dd = max(dd, 1)
    else:
        uu += 1
        uu = min(uu, h)
print("YES")

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    H, W, N = in_nn()
    sy, sx = in_nn()
    S = in_s()
    T = in_s()

    
    now_x = sx
    for i in range(N):
        if S[i] == 'L':
            now_x -= 1
        if now_x == 0:
            print('NO')
            exit()
        if T[i] == 'R' and now_x < W:
            now_x += 1

    
    now_x = sx
    for i in range(N):
        if S[i] == 'R':
            now_x += 1
        if now_x == W + 1:
            print('NO')
            exit()
        if T[i] == 'L' and now_x > 1:
            now_x -= 1

    
    now_y = sy
    for i in range(N):
        if S[i] == 'U':
            now_y -= 1
        if now_y == 0:
            print('NO')
            exit()
        if T[i] == 'D' and now_y < H:
            now_y += 1

    
    now_y = sy
    for i in range(N):
        if S[i] == 'D':
            now_y += 1
        if now_y == H + 1:
            print('NO')
            exit()
        if T[i] == 'U' and now_y > 1:
            now_y -= 1

    print('YES')


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue



sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7


ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))




def main():
    h, w, n = ns()
    y, x = ns()
    s = list(input())
    t = list(input())

    su, sd, sl, sr = 0, 0, 0, 0
    tu, td, tl, tr = 0, 0, 0, 0

    yes = True

    for si, ti in zip(s, t):
        if si == "U":
            su += 1
        elif si == "D":
            sd += 1
        elif si == "L":
            sl += 1
        else:
            sr += 1

        if y - (su - td) < 1:
            yes = False
        if y + sd - tu > h:
            yes = False
        if x - (sl - tr) < 1:
            yes = False
        if x + sr - tl > w:
            yes = False

        if ti == "U":
            tu = min(tu + 1, y - 1 + sd)
        elif ti == "D":
            td = min(td + 1, h - y + su)
        elif ti == "L":
            tl = min(tl + 1, x - 1 + sr)
        else:
            tr = min(tr + 1, w - x + sl)

    print("YES" if yes else "NO")


if __name__ == '__main__':
    main()

Solution (Python):
h , w , n = map(int,input().split())
sr , sc = map(int,input().split())
s = input()
t = input()
scou = [0,0,0,0] 
tcou = [0,0,0,0]
hou = {"L":0,"R":1,"U":2,"D":3}
for i in range(n):
    scou[hou[s[i]]] += 1
    if sc - scou[0] + tcou[1] <= 0:
        print("NO")
        exit()
    if sc + scou[1] - tcou[0] >= w + 1:
        print("NO")
        exit()
    if sr - scou[2] + tcou[3] <= 0:
        print("NO")
        exit()
    if sr + scou[3] - tcou[2] >= h + 1:
        print("NO")
        exit()
    
    tcou[hou[t[i]]] += 1
    if sc - tcou[0] + scou[1] <= 0:
        tcou[0] -= 1
    if sc + tcou[1] - scou[0] >= w+1:
        tcou[1] -= 1
    if sr - tcou[2] + scou[3] <= 0:
        tcou[2] -= 1
    if sr + tcou[3] - scou[2] >= h+1:
        tcou[3] -= 1

print("YES")

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    H, W, N = map(int, input().split())
    sr, sc = map(int, input().split())
    sr = H + 1 - sr
    S = input()[::-1]
    T = input()[::-1]

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

        if s == "R":
            right -= 1
        elif s == "L":
            left += 1
        elif s == "U":
            up -= 1
        elif s == "D":
            bottom += 1

        if left > right or bottom > up:
            print("NO")
            exit()

    print("YES" if left <= sc <= right and bottom <= sr <= up else "NO")


if __name__ == '__main__':
    resolve()

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache





MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b: a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





H,W,N = IL()
sy,sx = IL()
A = S()
B = S()

X = [sx,sx]
Y = [sy,sy]
for i in range(N):
  a = A[i]
  if a == "L":
    if X[0] == 1:
      print("NO")
      exit()
    else:
      X[0] -= 1
  if a == "R":
    if X[1] == W:
      print("NO")
      exit()
    else:
      X[1] += 1
  if a == "U":
    if Y[0] == 1:
      print("NO")
      exit()
    else:
      Y[0] -= 1
  if a == "D":
    if Y[1] == H:
      print("NO")
      exit()
    else:
      Y[1] += 1
  a = B[i]
  if a == "L":
    X[1] = max(1,X[1]-1)
  if a == "R":
    X[0] = min(W,X[0]+1)
  if a == "U":
    Y[1] = max(1,Y[1]-1)
  if a == "D":
    Y[0] = min(H,Y[0]+1)
  
print("YES")

Solution (Python):















h, w, n = list(map(int, input().split()))
row, column = list(map(int, input().split()))
s_sente = input()
s_gote = input()

ans = [0, h-1, 0, w-1]
for i in reversed(range(n)):
    g = s_gote[i]
    if g == 'U':
        ans[1] = min(ans[1] + 1, h-1)
    elif g == 'D':
        ans[0] = max(ans[0] - 1, 0)
    elif g == 'L':
        ans[3] = min(ans[3] + 1, w-1)
    elif g == 'R':
        ans[2] = max(ans[2] - 1, 0)
    

    s = s_sente[i]
    if s == 'U':
        ans[0] = ans[0] + 1
    elif s == 'D':
        ans[1] = ans[1] - 1
    elif s == 'L':
        ans[2] = ans[2] + 1
    elif s == 'R':
        ans[3] = ans[3] - 1
    
    if not (ans[0] <= ans[1] and ans[2] <= ans[3]):
        print('NO')
        exit()
    

if ans[0] <= row-1 <= ans[1] and ans[2] <= column-1 <= ans[3]:
    
    print('YES')
else:
    print('NO')

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


h,w,n = list(map(int, input().split()))
rr,cc = list(map(int, input().split()))
rr -= 1
cc -= 1
s = input()
t = input()
def sub(s,t,init,limit):
    c = init
    for ss,tt in zip(s,t):
        if ss:
            c += 1
            if c>=limit:
                ans = True
                break
        if tt:
            c -= 1
            c = max(0, c)
    else:
        ans = False
    return ans
v1 = sub([item=="U" for item in s], [item=="D" for item in t], h-1-rr, h)
v2 = sub([item=="D" for item in s], [item=="U" for item in t], rr, h)
v3 = sub([item=="L" for item in s], [item=="R" for item in t], w-1-cc, w)
v4 = sub([item=="R" for item in s], [item=="L" for item in t], cc, w)
if v1 or v2 or v3 or v4:
    ans = "NO"
else:
    ans = "YES"
print(ans)

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


nL, nR = 1, W

sr = H + 1 - sr
nD, nU = 1, H

for s,t in zip(S[::-1],T[::-1]):
    
    
    if t == "R" :nL -= 1
    elif t == "L": nR += 1
    elif t == "U": nD -= 1
    elif t == "D": nU += 1

    
    if nL < 1: nL = 1
    if nR > W: nR = W
    if nD < 1: nD = 1
    if nU > H: nU = H

    
    if s == "R" :nR -= 1
    elif s == "L": nL += 1
    elif s == "U": nU -= 1
    elif s == "D": nD += 1


    if nL > nR or nD > nU:
        print("NO")
        exit()


if (nL <= sc <= nR) and (nD <= sr <= nU):
    print("YES")
else:
    print("NO")

Solution (Python):

"""
1つの駒が二次元グリッド上(HxW)にある。最初駒は(sr,sc)にある。
iターン目にそれぞれSiとTi方向に動くか、もしくは動かないを選択できる。
高橋くんはSiの動作を選ぶことができ、盤面から駒を出す事ができたら勝つ。(h or w < 1 or H < h or W < w)
青木くんはTiの操作を選ぶことができ、ゲーム終了までずっと盤面上に駒が存在すれば勝つ。
両者が最適に行動した時、どちらが勝つか出力せよ。(青木くんが勝つならYES)

"""
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

H,W,N = map(int,readline().split())
sh,sw = map(int,readline().split())
S = readline().rstrip().decode('utf-8')
T = readline().rstrip().decode('utf-8')


UDLR = [sh-1,H-sh,sw-1,W-sw]

UDLR3 = [H-sh,sh-1,W-sw,sw-1]
UDLR3 = [-i for i in UDLR3]


UDLR2 = [0,0,0,0]

m = {"U":0,"D":1,"L":2,"R":3}
m2 = {"U":1,"D":0,"L":3,"R":2}
for i in range(N):
    s,t = m[S[i]],m2[T[i]]

    UDLR2[s] += 1
    
    for i in range(4):
        if UDLR2[i] > UDLR[i]:
            print("NO")
            exit()
    
    if UDLR2[t] -1 >= UDLR3[t]:
        UDLR2[t] -= 1

print("YES")

Solution (Python):
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


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


def main():
    H, W, N = map(int, input().split())
    sh, sw = map(int, input().split())
    S = input()
    T = input()
    count_S = defaultdict(int)
    count_T = defaultdict(int)

    for i in range(N):
        s = S[i]
        t = T[i]

        left = min(sw - (count_S["L"] - count_T["R"]), sw)
        right = max(sw + count_S["R"] - count_T["L"], sw)
        top = min(sh - (count_S["U"] - count_T["D"]), sh)
        bottom = max(sh + count_S["D"] - count_T["U"], sh)

        if s == "L":
            if left - 1 == 0:
                print("NO")
                return
        elif s == "R":
            if right == W:
                print("NO")
                return
        elif s == "U":
            if top - 1 == 0:
                print("NO")
                return
        elif s == "D":
            if bottom == H:
                print("NO")
                return

        count_S[s] += 1

        right = max(sw - (count_S["L"] - count_T["R"]), sw)
        left = min(sw + count_S["R"] - count_T["L"], sw)
        bottom = max(sh - (count_S["U"] - count_T["D"]), sh)
        top = min(sh + count_S["D"] - count_T["U"], sh)

        if t == "L":
            if left - 1 == 0:
                continue
        elif t == "R":
            if right == W:
                continue
        elif t == "U":
            if top - 1 == 0:
                continue
        elif t == "D":
            if bottom == H:
                continue

        count_T[t] += 1

    print("YES")


if __name__ == "__main__":
    main()

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

tmove=[(0,-1),(0,1),(-1,0),(1,0)]
amove=[(0,1),(0,-1),(1,0),(-1,0)]
m=[('L','R'),('R','L'),('U','D'),('D','U')]
for i in range(4):
    nh,nw=sr,sc
    t,a=tmove[i],amove[i]
    for k in range(N):
        if S[k]==m[i][0]:
            nh,nw=nh+t[0],nw+t[1]
        if nh<0 or nw<0 or nh>=H or nw>=W:
            print('NO')
            exit()
        gh,gw=nh+a[0],nw+a[1]
        if T[k]==m[i][1]:
            if gh>=0 and gh<H and gw>=0 and gw<W:
                nh,nw=gh,gw
print('YES')

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

H, W, N = mapint()
sy, sx = mapint()
S = list(str(input()))
T = list(str(input()))
L, R = sx-1, sx-1
U, D = sy-1, sy-1
for s, t in zip(S, T):
    if s=='L':
        L -= 1
        if L<0:
            print('NO')
            break
    elif s=='R':
        R += 1
        if R>=W:
            print('NO')
            break
    elif s=='U':
        U -= 1
        if U<0:
            print('NO')
            break
    else:
        D += 1
        if D>=H:
            print('NO')
            break
    if t=='L':
        R = max(R-1, 0)
    elif t=='R':
        L = min(L+1, W-1)
    elif t=='U':
        D = max(D-1, 0)
    else:
        U = min(U+1, H-1)
else:
    print('YES')

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

h -= 1
w -= 1
for i in range(N):
    if S[i] == "L":
        if w-1 < W-w-1:
            w -= 1
    elif S[i] == "R":
        if w > W-w-2:
            w += 1
    elif S[i] == "U":
        if h-1 < H-h-1:
            h -= 1
    elif S[i] == "D":
        if h > H-h-2:
            h += 1
    if w == 0 or h == 0 or h == H or w == W:
        print("NO")
        break

    if T[i] == "L":
        if w-1 > W-w-1:
            w -= 1
    elif T[i] == "R":
        if w < W-w-2:
            w += 1
    elif T[i] == "U":
        if h-1 > H-h-1:
            h -= 1
    elif T[i] == "D":
        if h < H-h-2:
            h += 1
else:
    print("YES")