Problem:
 Score : 600 points    Problem Statement We have a rectangular grid of squares with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i -th row from the top and the j -th column from the left.
On this grid, there is a piece, which is initially placed at square (s_r,s_c) .  Takahashi and Aoki will play a game, where each player has a string of length N .
Takahashi's string is S , and Aoki's string is T . S and T both consist of four kinds of letters: L , R , U and D .  The game consists of N steps. The i -th step proceeds as follows:   First, Takahashi performs a move. He either moves the piece in the direction of S_i , or does not move the piece.  Second, Aoki performs a move. He either moves the piece in the direction of T_i , or does not move the piece.   Here, to move the piece in the direction of L , R , U and D , is to move the piece from square (r,c) to square (r,c-1) , (r,c+1) , (r-1,c) and (r+1,c) , respectively. If the destination square does not exist, the piece is removed from the grid, and the game ends, even if less than N steps are done.  Takahashi wants to remove the piece from the grid in one of the N steps.
Aoki, on the other hand, wants to finish the N steps with the piece remaining on the grid.
Determine if the piece will remain on the grid at the end of the game when both players play optimally.      Constraints  2 \leq H,W \leq 2 \times 10^5  2 \leq N \leq 2 \times 10^5  1 \leq s_r \leq H  1 \leq s_c \leq W  |S|=|T|=N  S and T consists of the four kinds of letters L , R , U and D .         Input Input is given from Standard Input in the following format:  H  W  N  s_r  s_c  S  T       Output If the piece will remain on the grid at the end of the game, print YES ; otherwise, print NO .        Sample Input 1 2 3 3
2 2
RRL
LUD      Sample Output 1 YES  Here is one possible progress of the game:   Takahashi moves the piece right. The piece is now at (2,3) .  Aoki moves the piece left. The piece is now at (2,2) .  Takahashi does not move the piece. The piece remains at (2,2) .  Aoki moves the piece up. The piece is now at (1,2) .  Takahashi moves the piece left. The piece is now at (1,1) .  Aoki does not move the piece. The piece remains at (1,1) .        Sample Input 2 4 3 5
2 2
UDRRR
LLDUD      Sample Output 2 NO       Sample Input 3 5 6 11
2 1
RLDRRUDDLRL
URRDRLLDLRD      Sample Output 3 NO

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)

def main():
    h,w,n = LI()
    sc,sr = LI()
    s = S()
    t = S()
    ra = ri = 0
    ca = ci = 0
    for i in range(n-1,-1,-1):
        si = s[i]
        ti = t[i]

        if ti == 'L':
            if ra > 0:
                ra -= 1
        elif ti == 'R':
            if ri > 0:
                ri -= 1
        elif ti == 'U':
            if ca > 0:
                ca -= 1
        else:
            if ci > 0:
                ci -= 1

        if si == 'L':
            ri += 1
        elif si == 'R':
            ra += 1
        elif si == 'U':
            ci += 1
        else:
            ca += 1

        if ri + ra >= w:
            return 'NO'
        if ci + ca >= h:
            return 'NO'

        

    if sr <= ri or sr + ra > w:
        return 'NO'
    if sc <= ci or sc + ca > h:
        return 'NO'

    return 'YES'


print(main())

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

r,c = s_r,s_c



for i in range(N):
    if S[i] == "L":
        c -= 1
    elif S[i] == "U":
        r -= 1
    if not (1<= r <= H and 1<= c <= W):
        print("NO")
        exit()
    if T[i] == "R":
        if c < W:
            c += 1
    elif T[i] == "D":
        if r < H:
            r += 1
r,c = s_r,s_c
for i in range(N):
    if S[i] == "R":
        c += 1
    elif S[i] == "D":
        r += 1
    if not (1<= r <= H and 1<= c <= W):
        print("NO")
        exit()
    if T[i] == "L":
        if 1 < c:
            c -= 1
    elif T[i] == "U":
        if 1 < r:
            r -= 1
print("YES")

Solution (Python):



h,w,n = [int(i) for i in input().split()]
h0,w0 = [int(i) for i in input().split()]

s = input()
t = input()

rs = reversed(s)
rt = reversed(t)

lw,rw=1,w
lh,rh=1,h

ok=1
for tak,ao in zip(rs,rt):
    if ao == "L":
        if rw<w:
            rw += 1
    if ao == "R":
        if lw>1:
            lw -= 1
    if ao == "U":
        if rh < h:
            rh += 1
    if ao == "D":
        if lh > 1:
            lh -= 1

    if tak == "L":
        lw += 1
    if tak == "R":
        rw -= 1
    if tak == "U":
        lh += 1
    if tak == "D":
        rh -= 1

    if lw > rw or lh > rh:
        ok=0
        break

if not (lw <= w0 <= rw and lh < h0 <= rh):
    ok=0

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

Solution (Python):
H,W,n=map(int,input().split())
h,w=map(int,input().split())
s=list(input())
t=list(input())
sr=0
sl=0
su=0
sd=0
tr=0
tl=0
tu=0
td=0

rl=W-w
ll=w-1
ul=h-1
dl=H-h
nr=w
nl=w
nu=h
nd=h
ans="YES"

for i in range(n):
    if s[i]=="R":
        nr+=1
        sr+=1
    if s[i]=="L":
        nl-=1
        sl+=1
    if s[i]=="U":
        nu+=1
        su+=1
    if s[i]=="D":
        nd-=1
        sd+=1
    if i!=0:
        if t[i-1]=="R":
            nl+=1
            if -lp<rl:
                tr+=1
        if t[i-1]=="L":
            nr-=1
            if -rp<ll:
                tl+=1
        if t[i-1]=="U":
            nd+=1
            if -dp<ul:
                tu+=1
        if t[i-1]=="D":
            nu-=1
            if -up<dl:
                td+=1
    rp=sr-tl
    lp=sl-tr
    up=su-td
    dp=sd-tu
    
    if rl<rp or ll<lp or ul<up or dl<dp:
        ans="NO"
print(ans)

Solution (Python):


HWN = [int(i) for i in input().split()]
s = [int(i) for i in input().split()]
S = list(str(input()))
T = list(str(input()))

Rn = HWN[1] - s[1] + 1
Ln = HWN[1] - Rn + 1
Dn = HWN[0] - s[0] + 1
Un = HWN[0] - Dn + 1



l = 0
r = 0
u = 0
d = 0
stat = 0
for i in range(HWN[2]):
    if S[i] == 'L':
        l += 1
    if S[i] == 'R':
        r += 1
    if S[i] == 'U':
        u += 1
    if S[i] == 'D':
        d += 1

    

    if l >= Ln or r >= Rn or u >= Un or d >= Dn:
        stat = 1
        break
    
    if T[i] == 'L':
        r -= 1
    if T[i] == 'R':
        l -= 1
    if T[i] == 'U':
        d -= 1
    if T[i] == 'D':
        u -= 1
    
    if -l >= Rn:
        l = -Rn + 1
    if -r >= Ln:
        r = -Ln + 1
    if -u >= Dn:
        u = -Dn + 1
    if -d >= Un:
        d = -Un + 1

    

    if l >= Ln or r >= Rn or u >= Un or d >= Dn:
        stat = 1
        break
    

if stat == 1:
    print('NO')
else:
    print('YES')

Solution (Python):
h,w,n=map(int,input().split())
sr,sc=map(int,input().split())
s=input()
t=input()
f=0
y1=sc;y2=sc;x1=sr;x2=sr
for i1 in range(n):
  if s[i1]=='R':
    y1+=1
  if y1==w+1:
    f=1
    break
  if t[i1]=='L' and y1!=1:
    y1-=1

for i2 in range(n):
  if s[i2]=='L':
    y2-=1
  if y2==0:
    f=1
    break
  if t[i2]=='R' and y2!=w:
    y2+=1

for i3 in range(n):
  if s[i3]=='U':
    x1-=1
  if x1==0:
    f=1
    break
  if t[i3]=='D' and x1!=h:
    x1+=1

for i4 in range(n):
  if s[i4]=='D':
    x2+=1
  if x2==h+1:
    f=1
    break
  if t[i4]=='U' and x2!=1:
    x2-=1

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

Solution (Python):

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

H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
stS = [v for v in input()[:-1]]
stT = [v for v in input()[:-1]]

ans = "YES"

T = "U"
A = "D"
m = sr

for s, t in zip(stS, stT):
    
    if s == T:
        m -= 1
        if m <= 0:
            ans = "NO"
            print(ans)
            exit()
    if t == A:
        if m < H:
            m += 1

T = "D"
A = "U"
m = sr

for s, t in zip(stS, stT):
    
    if s == T:
        m += 1
        if m > H:
            ans = "NO"
            print(ans)
            exit()
    if t == A:
        if m > 1:
            m -= 1

T = "R"
A = "L"
m = sc

for s, t in zip(stS, stT):
    
    if s == T:
        m += 1
        if m > W:
            ans = "NO"
            print(ans)
            exit()
    if t == A:
        if m > 1:
            m -= 1
T = "L"
A = "R"
m = sc

for s, t in zip(stS, stT):
    
    if s == T:
        m -= 1
        if m <= 0:
            ans = "NO"
            print(ans)
            exit()
    if t == A:
        if m < H:
            m += 1
print(ans)

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

y,x = r,c

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

y,x = r,c

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

y,x = r,c

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

y,x = r,c

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

print("YES")

Solution (Python):
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
sr, sc = sr - 1, sc - 1
S = input()
T = input()
D = ['L', 'R', 'U', 'D']
rev = {'L': 'R', 'R': 'L', 'U': 'D', 'D': 'U'}
d = {'L': (0, -1), 'R': (0, 1), 'U': (-1, 0), 'D': (1, 0)}
for K in D:
    tr, tc = sr, sc
    for j in range(N):
        if S[j] == K:
            tr, tc = tr + d[K][0], tc + d[K][1]
            if not (0 <= tr < H and 0 <= tc < W):
                print('NO')
                exit()
        if T[j] == rev[K]:
            tr, tc = tr + d[rev[K]][0], tc + d[rev[K]][1]
            if tr == H:
                tr -= 1
            elif tr == -1:
                tr += 1

            if tc == W:
                tc -= 1
            elif tc == -1:
                tc += 1


print('YES')

Solution (Python):


from bisect import bisect_left as binl

def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * sy) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x





def main():
    h, w, n = intin()
    init_pos_h, init_pos_w = intin()
    s = input()
    t = input()

    pos_h = init_pos_h
    pos_w = init_pos_w
    for i in range(n):
        if s[i] == 'L':
            pos_w -= 1
        if pos_w <= 0:
            print('NO')
            return
        if t[i] == 'R' and pos_w < w:
            pos_w += 1

    pos_h = init_pos_h
    pos_w = init_pos_w
    for i in range(n):
        
        if s[i] == 'R':
            pos_w += 1
        
        if pos_w > w:
            print('NO')
            return
        if t[i] == 'L' and pos_w > 1:
            pos_w -= 1

    pos_h = init_pos_h
    pos_w = init_pos_w
    for i in range(n):
        if s[i] == 'U':
            pos_h -= 1
        if pos_h <= 0:
            print('NO')
            return
        if t[i] == 'D' and pos_h < h:
            pos_h += 1

    pos_h = init_pos_h
    pos_w = init_pos_w
    for i in range(n):
        if s[i] == 'D':
            pos_h += 1
        if pos_h > h:
            print('NO')
            return
        if t[i] == 'U' and pos_h > 1:
            pos_h -= 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()

ans = 0

tu = sr - 1
td = h - sr
tl = sc - 1
tr = w - sc

tkh_count_u = 0
tkh_count_d = 0
tkh_count_l = 0
tkh_count_r = 0

aok_count_u = 0
aok_count_d = 0
aok_count_l = 0
aok_count_r = 0

for i in range(n):
    if s[i] == "U":
        tkh_count_u += 1
    if s[i] == "D":
        tkh_count_d += 1
    if s[i] == "L":
        tkh_count_l += 1
    if s[i] == "R":
        tkh_count_r += 1

    if tu < tkh_count_u - aok_count_d:
        ans = 1
        break
    if td < tkh_count_d - aok_count_u:
        ans = 1
        break
    if tl < tkh_count_l - aok_count_r:
        ans = 1
        break
    if tr < tkh_count_r - aok_count_l:
        ans = 1
        break

    if t[i] == "U":
        if aok_count_u - tkh_count_d < tu:
          aok_count_u += 1
    if t[i] == "D":
        if aok_count_d - tkh_count_u < td:
          aok_count_d += 1
    if t[i] == "L":
        if aok_count_l - tkh_count_r < tl:
          aok_count_l += 1
    if t[i] == "R":
        if aok_count_r - tkh_count_l < tr:
          aok_count_r += 1

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

Solution (Python):
def solve(H, W, N, sr, sc, S, T):
    
    i = sr
    for s, t in zip(S, T):
        if s == "U":
            i -= 1
        if i < 1:
            return False
        if t == "D" and i + 1 <= H:
            i += 1
    
    i = sr
    for s, t in zip(S, T):
        if s == "D":
            i += 1
        if i > H:
            return False
        if t == "U" and i - 1 >= 1:
            i -= 1

    
    i = sc
    for s, t in zip(S, T):
        if s == "R":
            i += 1
        if i > W:
            return False
        if t == "L" and i - 1 >= 1:
            i -= 1

    
    i = sc
    for s, t in zip(S, T):
        if s == "L":
            i -= 1
        if i < 1:
            return False
        if t == "R" and i + 1 <= W:
            i += 1

    return True

if __name__ == "__main__":
    H, W, N = tuple(map(int, input().split(" ")))
    sr, sc = tuple(map(int, input().split(" ")))
    S = input()
    T = input()
    print("YES" if solve(H, W, N, sr, sc, S, T) else "NO")

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

ans = True
si = sr
sj = sc

for i in range(N):
    if S[i] == "R":
        sj += 1
    if sj > W:
        ans = False
        break
    if T[i] == "L":
        sj = max(1,sj -1)
si = sr
sj = sc

for i in range(N):
    if S[i] == "L":
        sj -= 1
    if sj < 1 or (not ans):
        ans = False
        break
    if T[i] == "R":
        sj = min(W,sj +1)
si = sr
sj = sc

for i in range(N):
    if S[i] == "D":
        si += 1
    if si > H or (not ans):
        ans = False
        break
    if T[i] == "U":
        si = max(1,si -1)
si = sr
sj = sc

for i in range(N):
    if S[i] == "U":
        si -= 1
    if si < 1 or (not ans):
        ans = False
        break
    if T[i] == "D":
        si = min(H,si +1)

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

Solution (Python):
H, W, N = [int(_) for _ in input().split()]
R, C = [int(_) for _ in input().split()]
S = input()
T = input()


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


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


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


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

print('YES')

Solution (Python):
H,W,N = list(map(int,input().split()))
h,w = list(map(int,input().split()))
h2 = h
w2 = w

S = input()
T = input()

result = "YES"


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



if result == "YES":
    for i in range(N):
        if S[i] == "R":
            w2 += 1
            if w2 > W:
                result = "NO"
                break
        elif S[i] == "D":
            h2 += 1
            if h2 > H:
                result = "NO"
                break
        
        if T[i] == "L":
            w2 -= 1
            if w2 < 1:
                w2 += 1
        elif T[i] == "U":
            h2 -= 1
            if h2 < 1:
                h2 += 1
        


print(result)

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

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

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

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

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

print("YES")

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


flag = 0
right = 0
left = 0
up = 0
down = 0

for i in range(n):
    if s[i] == "R":
        right += 1
    if right >= w - sc + 1:
        flag = 1
        break
    if t[i] == "L" and -right < sc - 1:
        right -= 1


    if s[i] == "L":
        left += 1
    if left >= sc:
        flag = 1
        break
    if t[i] == "R" and -left < w - sc:
        left -= 1

    if s[i] == "U":
        up += 1
    if up >= sr:
        flag = 1
        break
    if t[i] == "D" and -up < h - sr:
        up -= 1

    if s[i] == "D":
        down += 1
    if down >= h - sr + 1:
        flag = 1
        break
    if t[i] == "U" and -down < sr - 1:
        down -= 1


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

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    h,w = LI()
    a = [sys.stdin.readline() for i in range(h)]
    s = 0
    p = [None for i in range(w*h)]
    q = [None for j in range(w*h)]
    l = 0
    for i in range(h):
        for j in range(w):
            if a[i][j] == "
                p[s] = (i,j)
                s += 1
            else:
                q[l] = (i,j)
                l += 1
    if s == h*w:
        print(0)
        quit()
    ans = 0
    M = w*h
    for i,j in q[:h*w-s]:
        m = M
        for x,y in p[:s]:
            if abs(i-x)+abs(j-y) < m:
                m = abs(i-x)+abs(j-y)
        if m > ans:
            ans = m
    print(ans)
    return


def B():
    h,w,n = LI()
    sy,sx = LI()
    sy -= 1
    sx -= 1
    s = S()
    t = S()
    ks = list("LRUD")
    kt = list("RLDU")
    ds = [[0,-1],[0,1],[-1,0],[1,0]]
    dt = [[0,1],[0,-1],[1,0],[-1,0]]
    for i in range(4):
        y,x = [sy,sx]
        for j in range(n):
            if s[j] == ks[i]:
                y+=ds[i][0]
                x+=ds[i][1]
                if y < 0 or y >= h or x < 0 or x >= w:
                    print("NO")
                    quit()
            if t[j] == kt[i]:
                y += dt[i][0]
                x += dt[i][1]
                if y < 0:y = 0
                elif y >= h:y = h-1
                elif x < 0:x=0
                elif x >= w:x=w-1
    print("YES")
    return


def C():
    n = I()
    return


def D():
    n = I()
    return


def E():
    n = I()
    return


def F():
    n = I()
    return


if __name__ == "__main__":
    B()

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

for [s, t, d, l] in [["L", "R", sw, w], ["R", "L", w-sw+1, w], ["U", "D", sh, h], ["D", "U", h-sh+1, h]]:

  x = d
  for i in range(n):

    if S[i] == s:
      x -= 1
    if x == 0:
      print("NO")
      exit()
    if T[i] == t and x < l:
      x += 1
    

print("YES")

Solution (Python):



from collections import deque
import heapq
import math
import bisect


def main():
    H, W, N = map(int, input().split())
    sr, sc = map(int, input().split())
    S, T = input(), input()
    dic = {"L" : 0, "R" : 1, "U" : 2, "D" : 3}
    dic2 = {"L" : 1, "R" : 0, "U" : 3, "D" : 2}

    cnt = [0] * 4
    need = [sc, W-sc+1, sr, H-sr+1]
    for i in range(N):
        cnt[dic[S[i]]] += 1
        for j in range(4):
            if need[j] <= cnt[j]:
                print("NO")
                exit()

        if T[i] == "L" and 1 < sc + cnt[1]:
            cnt[dic2[T[i]]] -= 1
        elif T[i] == "R" and sc - cnt[0] < W:
            cnt[dic2[T[i]]] -= 1
        elif T[i] == "U" and 1 < sr + cnt[3]:
            cnt[dic2[T[i]]] -= 1
        elif T[i] == "D" and sr - cnt[2] < H:
            cnt[dic2[T[i]]] -= 1

    print("YES")


if __name__ == '__main__':
    main()

Solution (Python):


import sys


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

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    h, w, n = LI()
    sr, sc = LI_()
    S = SI()
    T = SI()

    D = {0: ('L', 'R', -1), 1: ('R', 'L', 1), 2: ('U', 'D', -1), 3: ('D', 'U', 1)}
    is_drop = False
    for t in range(2):
        taka, aoki, d = D[t]
        x = sc
        for i in range(n):
            if S[i] == taka:
                x += d
                if x < 0 or x >= w:
                    is_drop = True
                    break
            if T[i] == aoki:
                x -= d
                if x < 0:
                    x = 0
                if x >= w:
                    x = w - 1

    for t in range(2, 4):
        taka, aoki, d = D[t]
        y = sr
        for i in range(n):
            if S[i] == taka:
                y += d
                if y < 0 or y >= h:
                    is_drop = True
                    break
            if T[i] == aoki:
                y -= d
                if y < 0:
                    y = 0
                if y >= h:
                    y = h - 1

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



if __name__ == '__main__':
    main()

Solution (Python):
list=input().split(" ")
h=int(list[0])
w=int(list[1])
n=int(list[2])

list=input().split(" ")
y=int(list[0])
x=int(list[1])


ato=[x, w-x+1, y, h-y+1]
countt=[0,0,0,0]
counta=[0,0,0,0]
counta2=[0,0,0,0] 
kano=[0,0,0,0]


taka=input()
aoki=input()

switch=0

for i in range(n):
    if i>0:
        if aoki[i-1]=='L':
            if counta[0]-countt[1]<x-1:
                counta[0]+=1
        elif aoki[i-1]=='R':
            if counta[1]-countt[0]<w-x:
                counta[1]+=1
        elif aoki[i-1]=='U':
            if counta[2]-countt[3]<y-1:
                counta[2]+=1
        elif aoki[i-1]=='D':
            if counta[3]-countt[2]<h-y:
                counta[3]+=1
    
    if taka[i]=='L':
        countt[0]+=1
    elif taka[i]=='R':
        countt[1]+=1
    elif taka[i]=='U':
        countt[2]+=1
    elif taka[i]=='D':
        countt[3]+=1
    
    
    countn=[countt[0]-counta[1], countt[1]-counta[0], countt[2]-counta[3], countt[3]-counta[2]]
    
    
    
    for j in range(4):
        if countn[j]>=ato[j]:
            switch=1

if switch==1:
    print('NO')
else:
    print('YES')

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


l = 1
r = W
u = 1
d = 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)
    else:
        u = max(u-1, 1)

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

    if l > r or u > d:
        print("NO")
        exit()
    
if u<=Sr<=d and l<=Sc<=r:
    print("YES")
else:
    print("NO")

Solution (Python):
import sys
def input():
	return sys.stdin.readline()[:-1]

H, W, n = map(int, input().split())
r, c = map(int, input().split())
s = input()
t = input()

L, R, U, D = c, W+1-c, r, H+1-r

dp = [[0 for _ in range(4)] for _ in range(n+1)]
dp[0][0] = L
dp[0][1] = R
dp[0][2] = U
dp[0][3] = D
for i in range(1, n+1):
	if dp[i-1][0] == 1 and s[i-1] == "L":
		print("NO")
		sys.exit()
	if dp[i-1][1] == 1 and s[i-1] == "R":
		print("NO")
		sys.exit()
	if dp[i-1][2] == 1 and s[i-1] == "U":
		print("NO")
		sys.exit()
	if dp[i-1][3] == 1 and s[i-1] == "D":
		print("NO")
		sys.exit()
	for j in range(4):
		dp[i][j] = dp[i-1][j]
	if s[i-1] == "L":
		dp[i][0] -= 1
	if s[i-1] == "R":
		dp[i][1] -= 1
	if s[i-1] == "U":
		dp[i][2] -= 1
	if s[i-1] == "D":
		dp[i][3] -= 1
	if t[i-1] == "L" and dp[i][1] < W:
		dp[i][1] += 1
	if t[i-1] == "R" and dp[i][0] < W:
		dp[i][0] += 1
	if t[i-1] == "U" and dp[i][3] < H:
		dp[i][3] += 1
	if t[i-1] == "D" and dp[i][2] < H:
		dp[i][2] += 1

	


print("YES")

Solution (Python):
H, W, n = [int(i) for i in input().split()]
r, c = [int(i) for i in input().split()]
S = input()
T = input()

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

need = [c, W - c + 1, r, H - r + 1]

for i in range(n):
    need[f[S[i]]] -= 1
    if need[f[S[i]]] <= 0:
        print('NO')
        exit()
    need[rf[T[i]]] = min(W if T[i] == 'L' or T[i] == 'R' else H , need[rf[T[i]]] + 1)

print('YES')

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))

h,w,n = getList()
sy, sx = getList()
taka = input()
aoki = input()

def game(taka, aoki, mt, ma, buf, bmax):
    for ta, ao in zip(taka, aoki):
        if ta == mt:
            buf -= 1
            if buf == 0:
                return False
        if ao == ma:
            buf = min(buf + 1, bmax)

    return True

ga = game(taka,aoki,"U", "D", sy, h)
gb = game(taka,aoki,"D", "U", h - sy + 1, h)
gc = game(taka,aoki,"L", "R", sx, w)
gd = game(taka,aoki,"R", "L", w - sx + 1, w)

if ga and gb and gc and gd:
    print("YES")

else:
    print("NO")

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

h,w,n = [int(x) for x in input().split()]

sy,sx = [int(x) for x in input().split()]

S = input().rstrip()
T = input().rstrip()

smallx = bigx = sx
smally = bigy = sy

dx = 0
dy = 0

for i in range(n):
    c = S[i]
    if c=='R':
        bigx += 1
    elif c=='L':
        smallx -= 1
    elif c=='U':
        smally -= 1
    elif c=='D':
        bigy += 1

    if smallx<1:
        tmp = min(1 - smallx, dx)
        dx -= tmp
        smallx += tmp
        bigx += tmp
    
    if smally<1:
        tmp = min(1 - smally, dy)
        dy -= tmp
        smally += tmp
        bigy += tmp
    

    if smallx < 1 or bigx > w or smally < 1 or bigy > h:
        print 'NO'
        sys.exit()


    c = T[i]
    if c=='R':
        smallx += 1
    elif c=='L':
        bigx -= 1
    elif c=='U':
        bigy -= 1
    elif c=='D':
        smally += 1

    
    if smallx>bigx:
        smallx = bigx
        dx += 1
    if smally>bigy:
        smally = bigy
        dy += 1

    if smallx<1:
        tmp = min(1 - smallx, dx)
        dx -= tmp
        smallx += tmp
        bigx += tmp
    
    if smally<1:
        tmp = min(1 - smally, dy)
        dy -= tmp
        smally += tmp
        bigy += tmp


    if smallx < 1 or bigx > w or smally < 1 or bigy > h:
        print 'NO'
        sys.exit()
print 'YES'
sys.exit()

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

BB = [1, 1, 1, 1]
DD = [-1, 1, -1, 1]
B = [sc, sc, sr, sr]
UB = [W, W, H, H]
C = "LRUD"
ok = 0

def check(l, r, sz, ps):
    lb = 0; rb = sz+1
    for i in range(N-1, -1, -1):
        s = S[i]; t = T[i]
        if t == l:
            rb = min(rb+1, sz+1)
        elif t == r:
            lb = max(lb-1, 0)
        if s == l:
            lb += 1
        elif s == r:
            rb -= 1
        if rb <= lb+1:
            return 0
    if ps <= lb or rb <= ps:
        return 0
    return 1

if not check("L", "R", W, sc) or not check("U", "D", H, sr):
    print("NO")
else:
    print("YES")

Solution (Python):
H,W,N = map(int,input().split())
sr,sc = map(int,input().split())
S = input()
T = input()
hr = sc
hl = sc
hu = sr
hd = sr
State = "YES"
for i in range(N):
    if S[i] == "R":
        hr += 1
        if hr > W:
            State = "NO"
            break
    elif S[i] == "L":
        hl -= 1
        if hl < 1:
            State = "NO"
            break
    elif S[i] == "U":
        hu -= 1
        if hu < 1:
            State = "NO"
            break
    else:
        hd += 1
        if hd > H:
            State = "NO"
            break
    if T[i] == "R":
        if hl < W:
            hl += 1
    if T[i] == "L":
        if hr > 1:
            hr -= 1
    if T[i] == "U":
        if hd > 1:
            hd -= 1
    if T[i] == "D":
        if hu < H:
            hu += 1
print(State)

Solution (Python):
h,w,n=map(int,input().split())
r,c=map(int,input().split())
s=input()
t=input()
xr=xl=xd=xu=yr=yl=yd=yu=0
for i in range(n):
    xr+=s[i]=='R'
    xl+=s[i]=='L'
    xd+=s[i]=='D'
    xu+=s[i]=='U'
    if w-c-(xr-yl)<0 or c-1-(xl-yr)<0 or h-r-(xd-yu)<0 or r-1-(xu-yd)<0:
        print('NO')
        break
    yr+=t[i]=='R'
    yr-=w-c-(yr-xl)<0
    yl+=t[i]=='L'
    yl-=c-1-(yl-xr)<0
    yd+=t[i]=='D'
    yd-=h-r-(yd-xu)<0
    yu+=t[i]=='U'
    yu-=r-1-(yu-xd)<0
else:
    print('YES')

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

flag = 0
hidari = sc
migi = sc
ue = sr
shita = sr
for i in range(N):
  if S[i] == 'L':
    hidari -= 1
  if S[i] == 'R':
    migi += 1
  if S[i] == 'U':
    ue -= 1
  if S[i] == 'D':
    shita += 1
  if hidari == 0 or migi == W+1 or ue == 0 or shita == H+1 :
    flag = 1
  
  if T[i] == 'R' and hidari != W:
    hidari += 1
  if T[i] == 'L' and migi != 1:
    migi -= 1
  if T[i] == 'D' and ue != H:
    ue += 1
  if T[i] == 'U' and shita != 1:
    shita -= 1

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