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):
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor


"""
"""
H = gi()
W = gi()
N = gi()
sr = gi()
sc = gi()
S = gw()
T = gw()
d_to_i = {"L": 0, "R": 1, "U": 2, "D": 3}
cnt = [[0 for x in range(4)] for i in range(2)]


def o(pos, si, ti, d, sb, tb):
    for i in range(N):
        if (d_to_i[S[i]] == si):
            pos += d
            if pos == sb:
                return 1
        if (d_to_i[T[i]] == ti):
            if (pos - d != tb):
                pos -= d
    return 0


off = 0

off = o(sc, 0, 1, -1, 0, W + 1)
off = off or o(sc, 1, 0, 1, W + 1, 0)
off = off or o(sr, 2, 3, -1, 0, H + 1)
off = off or o(sr, 3, 2, 1, H + 1, 0)

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

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

Rc = Lc = sc
Uc = Dc = sr

for i in range(N):
  if   S[i] == "L":
    Lc -= 1
  elif S[i] == "R":
    Rc += 1
  elif S[i] == "U":
    Uc -= 1
  elif S[i] == "D":
    Dc += 1
  
  if Lc < 1 or W < Rc or Uc < 1 or H < Dc:
    print("NO")
    break

  if   T[i] == "L" and 1 < Rc:
    Rc -= 1
  elif T[i] == "R" and Lc < W:
    Lc += 1
  elif T[i] == "U" and 1 < Dc:
    Dc -= 1
  elif T[i] == "D" and Uc < H:
    Uc += 1
else:
  print("YES")

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

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

            if l>r:
                print("NO")
                return
            if t=="U" and (r==0 or l==n):
                print("NO")
                return
            if t=="R" and (r==0 or l==n):
                print("NO")
                return

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

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

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

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

if __name__ == '__main__':
    examB()

"""

"""

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


def examB():
    H, W, N = LI()
    sr, sc = LI()
    S = SI()
    T = SI()

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

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

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

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

if __name__ == '__main__':
    examB()

"""

"""

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

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

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

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

Solution (Python):
h,w,n = map(int, input().split())
sr,sc = map(int, input().split())
s = input()
t = input()
now = [sc,sc,sr,sr]
ans = "YES"
for i,j in zip(s,t):
  if i == "L":
    now[0] -= 1
    if now[0] <= 0:
      ans = "NO"
      break
  elif i == "R":
    now[1] += 1
    if now[1] > w:
      ans = "NO"
      break
  elif i == "U":
    now[2] -= 1
    if now[2] <= 0:
      ans = "NO"
      break
  else:
    now[3] += 1
    if now[3] > h:
      ans = "NO"
      break
  if j == "L":
    now[1] = max(1,now[1]-1)
  elif j == "R":
    now[0] = min(w,now[0]+1)
  elif j == "U":
    now[3] = max(1,now[3]-1)
  else:
    now[2] = min(h,now[2]+1)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
H, W, N = map(int, input().split())
y, x = map(int, input().split())
S = list(input())[: -1]
T = list(input())[: -1]
def no():
  print("NO")
  exit(0)

v = x + 0
for i in range(N):
  if S[i] == "R": v += 1
  if v > W or v < 1: no()
  if T[i] == "L":
    if v > 1:
      v -= 1
  if v > W or v < 1: no()


v = x + 0
for i in range(N):
  if S[i] == "L": v -= 1
  if v > W or v < 1: no()
  if T[i] == "R":
    if v < W:
      v += 1
  if v > W or v < 1: no()


v = y + 0
for i in range(N):
  if S[i] == "U": v -= 1
  if v > H or v < 1: no()
  if T[i] == "D":
    if v < H:
      v += 1
  if v > H or v < 1: no()

v = y + 0
for i in range(N):
  if S[i] == "D": v += 1
  if v > H or v < 1: no()
  if T[i] == "U":
    if v > 1:
      v -= 1
  if v > H or v < 1: no()

print("YES")

Solution (Python):
H,W,N = map(int,input().split())
sr,sc = map(int,input().split())
S = input().strip()
T = input().strip()
cur = sc
flag=0
for i in range(N):
    if S[i]=="R":
        cur += 1
        if cur>W:
            flag = 1
            break
    if cur>1 and T[i]=="L":
        cur -= 1
cur = sc
for i in range(N):
    if S[i]=="L":
        cur -= 1
        if cur<1:
            flag = 1
            break
    if cur<W and T[i]=="R":
        cur += 1
cur = sr
for i in range(N):
    if S[i]=="U":
        cur -= 1
        if cur<1:
            flag = 1
            break
    if cur<H and T[i]=="D":
        cur += 1
cur = sr
for i in range(N):
    if S[i]=="D":
        cur += 1
        if cur>H:
            flag = 1
            break
    if cur>1 and T[i]=="U":
        cur -= 1
if flag==1:
    print("NO")
else:
    print("YES")

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



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

for i in range(N)[::-1]:
    if i==N-1:
        l = 1+(int(S[i]=='L'))
        r = W-(int(S[i]=='R'))
        u = 1+(int(S[i]=='U'))
        d = H-(int(S[i]=='D'))
    else:
        l = max(l+int(S[i]=='L')-int(T[i]=='R'), 1+(int(S[i]=='L')))
        r = min(r-int(S[i]=='R')+int(T[i]=='L'), W-(int(S[i]=='R')))
        u = max(u+int(S[i]=='U')-int(T[i]=='D'), 1+(int(S[i]=='U')))
        d = min(d-int(S[i]=='D')+int(T[i]=='U'), H-(int(S[i]=='D')))
    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

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

dic1={"U":0,"D":1,"R":2,"L":3}
dic2={"U":1,"D":0,"R":3,"L":2}
dic3={"U":-1,"D":1,"R":1,"L":-1}
data=[[sr],[sr],[sc],[sc]]
for i in range(N):
    data[dic1[S[i]]].append(dic3[S[i]])
    data[dic2[T[i]]].append(dic3[T[i]])

h=data[0]
for i in range(1,len(h)):
    h[i]=min(H-1,h[i]+h[i-1])
    if h[i]==-1:
        print("NO")
        sys.exit()
h=data[1]
for i in range(1,len(h)):
    h[i]=max(0,h[i]+h[i-1])
    if h[i]==H:
        print("NO")
        sys.exit()
h=data[3]
for i in range(1,len(h)):
    h[i]=min(W-1,h[i]+h[i-1])
    if h[i]==-1:
        print("NO")
        sys.exit()
h=data[2]
for i in range(1,len(h)):
    h[i]=max(0,h[i]+h[i-1])
    if h[i]==W:
        print("NO")
        sys.exit()
print("YES")

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    h,w,n=map(int,input().split())
    i0,j0=map(int,input().split())
    S=input()
    T=input()

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

        if(s=='U'): u+=1
        elif(s=='D'): d-=1
        elif(s=='L'): l+=1
        elif(s=='R'): r-=1
        if(u>d or l>r):
            print("NO")
            return

    print("YES" if(u<=i0<=d and l<=j0<=r) else "NO")
resolve()

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

Solution (Python):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

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


H, W, N = list(map(int, sys.stdin.buffer.readline().split()))
h, w = list(map(int, sys.stdin.buffer.readline().split()))
S = sys.stdin.buffer.readline().decode().rstrip()
T = sys.stdin.buffer.readline().decode().rstrip()


def solve():
    
    
    l = 0
    r = W + 1
    if S[-1] == 'L':
        l += 1
    if S[-1] == 'R':
        r -= 1
    for i in reversed(range(N - 1)):
        if T[i] == 'L':
            r += 1
            r = min(r, W + 1)
        if T[i] == 'R':
            l -= 1
            l = max(l, 0)

        if S[i] == 'L':
            l += 1
            l = max(l, 0)
        if S[i] == 'R':
            r -= 1
            r = min(r, W + 1)

        if W <= l:
            return False
        if r <= 1:
            return False
    if not l < w < r:
        return False

    u = 0
    d = H + 1
    if S[-1] == 'U':
        u += 1
    if S[-1] == 'D':
        d -= 1
    for i in reversed(range(N - 1)):
        if T[i] == 'U':
            d += 1
            d = min(d, H + 1)
        if T[i] == 'D':
            u -= 1
            u = max(u, 0)

        if S[i] == 'U':
            u += 1
            u = max(u, 0)
        if S[i] == 'D':
            d -= 1
            d = min(d, H + 1)

        if H <= u:
            return False
        if d <= 1:
            return False
    if not u < h < d:
        return False

    return True


ok = solve()
if ok:
    print('YES')
else:
    print('NO')

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

y=sr
for i,j in zip(s,t):
    if i=="U":
        y-=1
        if y==0:
            print("NO")
            exit()
    if j=="D" and y<h:
        y+=1
y=sr
for i,j in zip(s,t):
    if i=="D":
        y+=1
        if y==h+1:
            print("NO")
            exit()
    if j=="U" and y>1:
        y-=1

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

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

print("YES")

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

y=sr
for i,j in zip(s,t):
    if i=="U":
        y-=1
        if y==0:
            print("NO")
            exit()
    if j=="D" and y<h:
        y+=1
y=sr
for i,j in zip(s,t):
    if i=="D":
        y+=1
        if y==h+1:
            print("NO")
            exit()
    if j=="U" and y>1:
        y-=1

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

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

print("YES")

Solution (Python):
from collections import defaultdict

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

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

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

Solution (Python):
import sys
import decimal

stdin = sys.stdin
 
ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip()  

H, W, N = rl()
start = rl()
S = list(rs())
T = list(rs())
bool = True


cur = start[0]
for i in range(N):
    if S[i] == 'U':
        cur -= 1
    if cur == 0:
        bool = False
        break
    if T[i] == 'D' and cur < H:
        cur += 1
    
cur = start[0]
for i in range(N):

    if S[i] == 'D':
        cur += 1
    if cur == H+1:
        bool = False
        break
    if T[i] == 'U' and cur > 1:
        cur -= 1
    
cur = start[1]
for i in range(N):
    if S[i] == 'R':
        cur += 1
    if cur == W+1:
        bool = False
        break
    if T[i] == 'L' and cur > 1:
        cur -= 1    

cur = start[1]
for i in range(N):
    if S[i] == 'L':
        cur -= 1
    if cur == 0:
        bool = False
        break
    if T[i] == 'R' and cur < W:
        cur += 1

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

Solution (Python):
def main():
    h,w,n=map(int,input().split())
    r,c=map(int,input().split())
    r-=1
    c-=1
    s=input()
    t=input()
    rl=[]
    ud=[]
    for i in range(n-1,-1,-1):
        if t[i]=="D":
            ud.append("+"+"S")
        elif t[i]=="U":
            ud.append("-"+"S")
        elif t[i]=="R":
            rl.append("+"+"S")
        else:
            rl.append("-"+"S")
        if s[i]=="D":
            ud.append("+"+"T")
        elif s[i]=="U":
            ud.append("-"+"T")
        elif s[i]=="R":
            rl.append("+"+"T")
        else:
            rl.append("-"+"T")
    
    

    L=0
    R=w-1
    for i in rl:
        if i[1]=="T":
            if i[0]=="-":
                L+=1
            else:
                R-=1
        else:
            if i[0]=="+":
                L=max(L-1,0)
            else:
                R=min(R+1,w-1)
        if L>R:
            print("NO")
            return 0
    if c<L or R<c:
        print("NO")
        return 0

    L=0
    R=h-1
    for i in ud:
        if i[1]=="T":
            if i[0]=="-":
                L+=1
            else:
                R-=1
        else:
            if i[0]=="+":
                L=max(L-1,0)
            else:
                R=min(R+1,h-1)
        if L>R:
            print("NO")
            return 0
    if r<L or R<r:
        print("NO")
        return 0

    print("YES")
main()

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, 0, 0, 0

for i in range(N):
    if T[i] == 'L' and r > 0:
        r -= 1
    elif T[i] == 'R' and l > 0:
        l -= 1
    elif T[i] == 'U' and d > 0:
        d -= 1
    elif T[i] == 'D' and u > 0:
        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 == W or u+d == H:
        print('NO')
        exit()

if l < sc and sc <= W-r and u < sr and sr <= H-d:
    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()

    xl,xr,yu,yd = sc,sc,sr,sr

    for i in range(n):
        if s[i] == "L":
            xl -= 1
        if s[i] == "R":
            xr += 1
        if s[i] == "U":
            yu -= 1
        if s[i] == "D":
            yd += 1
        if xl <= 0 or xr > w or yu <= 0 or yd > h:
            return "NO"
        if t[i] == "R" and xl < w:
            xl += 1
        if t[i] == "L" and xr > 1:
            xr -= 1
        if t[i] == "D" and yu < h:
            yu += 1
        if t[i] == "U" and yd > 1:
            yd -= 1
    
    return("YES")
print(main())

Solution (Python):
d='URLD'
*y,s,t=open(0).read().split()
h,w,n,r,c=map(int,y)
a=0
for i,j in enumerate(d):
  x=[h-r+1,c,w-c+1,r][i]
  for k in range(n):
    x+=(s[k]==j)
    a|=x>[h,w,w,h][i]
    x-=(dict(zip(d,d[::-1]))[t[k]]==j)*x>1
print('YNEOS'[a::2])

Solution (Python):
d='URLD'
*y,s,t=open(0).read().split()
h,w,n,r,c=map(int,y)
b=[h,w,w,h]
o=[h-r+1,c,w-c+1,r]
a=0
for i,j in enumerate(d):
  x=o[i]
  for k in range(n):
    x+=(s[k]==j)
    a|=x>b[i]
    x-=(dict(zip(d,d[::-1]))[t[k]]==j)*x>1
print('YNEOS'[a::2])

Solution (Python):
d='URLD'
*y,s,t=open(0).read().split()
h,w,n,r,c=map(int,y)
b=[h,w,w,h]
o=[h-r+1,c,w-c+1,r]
a=0
for i,j in enumerate(d):
  x=o[i]
  for k in range(n):
    x+=(s[k]==j)
    if x>b[i]:a=1
    x-=(dict(zip(d,d[::-1]))[t[k]]==j)*x>1
print('YNEOS'[a::2])

Solution (Python):
d='URLD'
*y,s,t=open(0).read().split()
h,w,n,r,c=map(int,y)
b=[h,w,w,h]
o=[h-r+1,c,w-c+1,r]
t=[dict(zip(d,d[::-1]))[i] for i in t]
a=0
for i,j in enumerate(d):
  x=o[i]
  for k in range(n):
    x+=(s[k]==j)
    if x>b[i]:a=1
    x-=(t[k]==j)*x>1
print('YNEOS'[a::2])

Solution (Python):
def M():return map(int,input().split())
d='URLD'
h,w,n=M()
b=[h,w,w,h]
r,c=M()
o=[h-r+1,c,w-c+1,r]
s=input()
t=[dict(zip(d,d[::-1]))[i] for i in input()]
a=0
for i,j in enumerate(d):
  x=o[i]
  for k in range(n):
    x+=(s[k]==j)
    if x>b[i]:a=1
    x-=(t[k]==j)*x>1
print('YNEOS'[a::2])

Solution (Python):
def M():return map(int,input().split())
d='URLD'
h,w,n=M()
b=[h,w,w,h]
r,c=M()
o=[h-r+1,c,w-c+1,r]
s=input()
t=[dict(zip(d,d[::-1]))[i] for i in input()]
a=0
for i,j in enumerate(d):
  x=o[i]
  for k in range(n):
    x+=(s[k]==j)
    if x>b[i]:a=1
    if x>1:x-=(t[k]==j)
print('YNEOS'[a::2])

Solution (Python):
def M():return map(int,input().split())
d='URLD'
*b,n=M()
b+=b[::-1]
r,c=M()
o=[b[0]-r+1,c,b[1]-c+1,r]
s=input()
t=[dict(zip(d,d[::-1]))[i] for i in input()]
a=0
for i,j in enumerate(d):
  x=o[i]
  for k in range(n):
    x+=(s[k]==j)
    if x>b[i]:a=1
    if x>1:x-=(t[k]==j)
print('YNEOS'[a::2])

Solution (Python):
d='URLD'
h,w,n=map(int,input().split())
b=[h,w,w,h]
r,c=map(int,input().split())
o=[h-r+1,c,w-c+1,r]
s=input()
t=[dict(zip(d,d[::-1]))[i] for i in input()]
a=0
for i,j in enumerate(d):
  x=o[i]
  for k in range(n):
    x+=(s[k]==j)
    if x>b[i]:a=1
    if x>1:x-=(t[k]==j)
print('YNEOS'[a::2])

Solution (Python):
d='URLD'
m={'U':0,'R':1,'D':2,'U':3}
m=dict(zip(d,range(4)))
v={'L':'R','R':'L','D':'U','U':'D'}
v=dict([(d[i],d[3-i]) for i in range(4)])
v=dict(zip(d,d[::-1]))
h,w,n=map(int,input().split())
b=[h,w,w,h]
r,c=map(int,input().split())
o=[h-r+1,c,w-c+1,r]
s=input()
t=[v[i] for i in input()]
a=1
for j,i in m.items():
    c=o[i]
    for k in range(n):
        c+=(s[k]==j)
        if c>b[i]:
            a=0
        if c>1:
            c-=(t[k]==j)
if a:
    print('YES')
else:
    print('NO')

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

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

show_flg=False


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

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

Solution (Python):
H,W,N=map(int,input().split())
r,c=map(int,input().split())

choku=list(input())
ao=list(input())
choku=choku[::-1]
ao=ao[::-1]

Dwn=1
Up=H
Lef=1
Rgt=W

def chokudai(c):
    global Dwn
    global Up
    global Lef
    global Rgt
    if choku[c]=="D":
        Dwn+=1
    if choku[c]=='U':
        Up-=1
    if choku[c]=='L':
        Lef+=1
    if choku[c]=='R':
        Rgt-=1
    if Dwn>Up or Lef>Rgt:
        print("NO")
        exit()


def kensho(k):
    global Dwn
    global Up
    global Lef
    global Rgt
    if ao[k]=="U":
        Dwn=max(1,Dwn-1)
    if ao[k]=="D":
        Up=min(H,Up+1)
    if ao[k]=="R":
        Lef=max(1,Lef-1)
    if ao[k]=="L":
        Rgt=min(W,Rgt+1)

for i in range(N):
    kensho(i)
    
    chokudai(i)
    

if Dwn<=H-r+1 and H-r+1<=Up and Lef<=c and c<=Rgt:
    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 = 1
r = W
d = H
u = 1
ans = "YES"
for i in range(N-1, -1, -1):
    if T[i] == "L":
        r = min(W, r+1)
    elif T[i] == "R":
        l = max(1, l-1)
    elif T[i] == "D":
        u = max(1, u-1)
    else:
        d = min(H, d+1)
    if S[i] == "L":
        l += 1
    elif S[i] == "R":
        r -= 1
    elif S[i] == "D":
        d -= 1
    else:
        u += 1
    if l > r or u > d:
        ans = "NO"
        break
        
if sr < u or d < sr or sc < l or r < sc:
    ans = "NO"
    
print(ans)

Solution (Python):
H, W, N = map(int,input().split())
sr, sc = map(int,input().split())
S = str(input())
T = str(input())
tk_ls = ['L', 'R', 'U', 'D']
ao_ls = ['R', 'L', 'D', 'U']
move_ls = [sc, W-sc+1, sr, H-sr+1]
limit_ls = [W, W, H, H]
for j in range(4):
    tk = tk_ls[j]
    ao = ao_ls[j]
    move = move_ls[j]
    limit = limit_ls[j]
    for i in range(N):
        if S[i] == tk:
            move -= 1
        if move == 0:
            print("NO")
            exit()
        if T[i] == ao and move < limit:
            move += 1
print("YES")

Solution (Python):
h,w,n=[int(j) for j in input().split()]
R,C=[int(j) for j in input().split()]
s=input()
t=input()


x=C
l,r=1,w
for i in range(n)[::-1]:
    if t[i]=="R":
        l-=1
    elif t[i]=="L":
        r+=1
    if l==0:
        l=1
    if r>w:
        r=w
    if s[i]=="R":
        r-=1
    elif s[i]=="L":
        l+=1
    if  l>r:
        print("NO")
        exit()
if not l<=x<=r:
    print("NO")
    exit()

x=h+1-R
l,r=1,h
for i in range(n)[::-1]:
    if t[i]=="U":
        l-=1
    elif t[i]=="D":
        r+=1
    if l==0:
        l=1
    if r>h:
        r=h
    if s[i]=="U":
        r-=1
    elif s[i]=="D":
        l+=1
    if l>r:
        print("NO")
        exit()
if not l<=x<=r:
    print("NO")
    exit()
print("YES")

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

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

ok = True

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

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

now_u = sr
for i in range(N):
    if S[i] == 'U':
        if now_u == 1:
            ok = False
            break
        now_u -= 1
    if T[i] == 'D':
        now_u = min(now_u+1, H)

now_d = sr
for i in range(N):
    if S[i] == 'D':
        if now_d == H:
            ok = False
            break
        now_d += 1
    if T[i] == 'U':
        now_d = max(now_d-1, 1)

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

Solution (Python):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial
import heapq, bisect
import math
import itertools


import queue
from collections import deque



def main():
    high, width, num = map(int, input().split())
    s_h, s_w = map(int, input().split())
    data1 = list(input())
    data2 = list(input())

    nokori = s_h - 1
    count = 0
    for i in range(num):
        if data1[i] == 'U':
            count += 1
        if count > nokori:
            print('NO')
            sys.exit()
        if data2[i] == 'D' and s_h - count < high:
            count -= 1

    nokori = s_w - 1
    count = 0
    for i in range(num):
        if data1[i] == 'L':
            count += 1
        if count > nokori:
            print('NO')
            sys.exit()
        if data2[i] == 'R' and s_w - count < width:
            count -= 1

    nokori = high - s_h
    count = 0
    for i in range(num):
        if data1[i] == 'D':
            count += 1
        if count > nokori:
            print('NO')
            sys.exit()
        if data2[i] == 'U' and s_h - 1 + count > 0:
            count -= 1

    nokori = width - s_w
    count = 0
    for i in range(num):
        if data1[i] == 'R':
            count += 1
        if count > nokori:
            print('NO')
            sys.exit()
        if data2[i] == 'L' and s_w - 1 + count > 0:
            count -= 1
        

    print('YES')



if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict

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

u = 1
d = H
l = 1
r = W
flag = False
for i in range(N-1,-1,-1):
  if i != N-1:
    if T[i] == 'U': d = min(d+1,H)
    elif T[i] == 'D': u = max(u-1,1)
    elif T[i] == 'L': r = min(r+1,W)
    else: l = max(l-1,1)
  if S [i] == 'U': u += 1
  elif S[i] == 'D': d -= 1
  elif S[i] == 'L': l += 1
  else: r -= 1
  if l>r or u>d:
    flag = True
    break

if flag or not (u<=sr<=d and l<=sc<=r):
  print('NO')
else:
  print('YES')

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


def b():
    M = [[h-sr,h-1], [sr-1,h-1], [w-sc,w-1], [sc-1,w-1]]
    D = [["U","D"], ["D","U"], ["L","R"], ["R","L"]]

    for i in range(4):
        
        j = 0
        while j < n and M[i][0] >= 0:
            if s[j] == D[i][1]:
                M[i][0] -= 1
                if M[i][0] < 0:
                    return "NO"
            if t[j] == D[i][0]:
                M[i][0] = min(M[i][0]+1, M[i][1])                
            
            j += 1
    return "YES"

print(b())