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 bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(H, W, N, s, S, T):
    s_ = s
    lx = 0
    rx = W-1
    ly = 0
    ry = H-1
    for s, t in reversed(list(zip(S, T))):
        if t == 'R':
            lx = max(0, lx-1)
        elif t == 'L':
            rx = min(W-1, rx+1)
        elif t == 'D':
            ly = max(0, ly-1)
        elif t == 'U':
            ry = min(H-1, ry+1)

        if s == 'L':
            lx += 1
        elif s == 'R':
            rx -= 1
        elif s == 'U':
            ly += 1
        elif s == 'D':
            ry -= 1

        if lx > rx:
            return 'NO'
        elif ly > ry:
            return 'NO'

    s = s_
    
    if not (lx <= s[1]-1 <= rx):
        return 'NO'
    elif not (ly <= s[0]-1 <= ry):
        return 'NO'
    return 'YES'


def main():
    H, W, N = read_int_n()
    s = read_int_n()
    S = read_str()
    T = read_str()
    print(slv(H, W, N, s, S, T))


if __name__ == '__main__':
    main()

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



L, R = 1, w

for i in range(n)[::-1]:
    if t[i] == 'R':
        
        
        L = max(L-1, 1)
    if t[i] == 'L':
        
        
        R = min(R+1, w)
    if s[i] == 'R':
        
        
        R -= 1
    if s[i] == 'L':
        
        
        L += 1
    if R < L: is_drop = True

if not L <= sc <= R: is_drop = True



L, R = 1, h

for i in range(n-1, -1, -1):
    if t[i] == 'U':
        
        
        R = min(R+1, h)
    if t[i] == 'D':
        
        
        L = max(L-1, 1)
    if s[i] == 'U':
        
        
        L += 1
    if s[i] == 'D':
        R -= 1

    if R < L: is_drop = True

if not L <= sr <= R: is_drop = True

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

Solution (Python):
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = input()
t = input()
sr -= 1; sc -= 1
 
L=0; R=w-1; U=0; D=h-1;
is_drop = False
for i in range(n)[::-1]:
    if t[i] == 'U': D = min(D+1, h-1)
    if t[i] == 'D': U = max(U-1, 0)
    if t[i] == 'L': R = min(R+1, w-1)
    if t[i] == 'R': L = max(L-1, 0)
    if s[i] == 'U': U += 1
    if s[i] == 'D': D -= 1
    if s[i] == 'L': L += 1
    if s[i] == 'R': R -= 1
    if R < L or D < U: is_drop = True
if (not L <= sc <= R) or (not U <= sr <= D):  is_drop = True

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

Solution (Python):

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

def takahashi(p, M):
    if M == 'L':
        p[0] -= 1
    if M == 'R':
        p[1] += 1
    if M == 'U':
        p[2] -= 1
    if M == 'D':
        p[3] += 1
    return p

def aoki(p, M):
    if M == 'L':
        p[1] = max(1, p[1]-1)
    if M == 'R':
        p[0] = min(W, p[0]+1)
    if M == 'U':
        p[3] = max(1, p[3]-1)
    if M == 'D':
        p[2] = min(H, p[2]+1)
    return p

for n in range(N):
    p = takahashi(p, S[n])
    if p[0] == 0 or p[1] == W+1 or p[2] == 0 or p[3] == H+1:
        print('NO')
        exit()
    p = aoki(p, T[n])
print('YES')

Solution (Python):
import sys

def main():
    input = sys.stdin.readline
    H, W, N = map(int, input().split())
    ini_r, ini_c = map(int, input().split())
    S = str(input().strip())[::-1]
    T = str(input().strip())[::-1]

    dp = [0] * (N+1)
    dp[0] = (1, H, 1, W)
    for i in range(N):
        s, t, = S[i], T[i]
        d, u, l, r = dp[i]

        if t == 'L':
            r = min(W, r+1)
        elif t == 'R':
            l = max(1, l-1)
        elif t == 'U':
            u = min(H, u+1)
        else:
            d = max(1, d-1)

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

        if u < d or r < l:
            return 'NO'

        dp[i+1] = (d, u, l, r)

    d, u, l, r = dp[N]
    if not(d <= ini_r <= u and l <= ini_c <= r):
        return 'NO'
    else:
        return 'YES'


if __name__ == '__main__':
    print(main())

Solution (Python):

H, W, N = map(int, input().split())
sr, sc = map(lambda x: int(x) - 1, input().split())
S = input()
T = input()
l, r = 0, W
u, d = 0, H
for i in range(N)[::-1]:
    if T[i] == 'L': r = min(W, r + 1)
    if T[i] == 'R': l = max(0, l - 1)
    if T[i] == 'U': d = min(H, d + 1)
    if T[i] == 'D': u = max(0, u - 1)
    if S[i] == 'L': l += 1
    if S[i] == 'R': r -= 1
    if S[i] == 'U': u += 1
    if S[i] == 'D': d -= 1
    if l == r or u == d: 
        print("NO")
        exit()

print(("NO", "YES")[l <= sc < r and u <= sr < d])

Solution (Python):
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
taka_array = list(
    map(int,
        input().replace("L", "0").replace("R", "1").replace("U", "2").replace(
            "D", "3")))
aoki_array = list(
    map(int,
        input().replace("L", "0").replace("R", "1").replace("U", "2").replace(
            "D", "3")))


limit_array = [0, 0, 0, 0]
size_array = [W, H]

for i, (taka, aoki) in enumerate(zip(taka_array[::-1], aoki_array[::-1])):
    if i != 0 and limit_array[(aoki // 2 * 4 + 1 - aoki)] != 0:
        limit_array[(aoki // 2 * 4 + 1 - aoki)] -= 1  

    limit_array[taka] += 1
    if limit_array[taka] == size_array[taka // 2]:
        print("NO")
        exit()
    
if 1 + limit_array[0] <= sc and W - limit_array[1] >= sc and 1 + limit_array[2] <= sr and H - limit_array[3] >= sr:
    print("YES")
else:
    print("NO")

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(lambda x: int(x) - 1, input().split())
S = input()
T = input()
l, r = 0, W
u, d = 0, H
for i in range(N)[::-1]:
    if T[i] == 'L': r = min(W, r + 1)
    if T[i] == 'R': l = max(0, l - 1)
    if T[i] == 'U': d = min(H, d + 1)
    if T[i] == 'D': u = max(0, u - 1)
    if S[i] == 'L': l += 1
    if S[i] == 'R': r -= 1
    if S[i] == 'U': u += 1
    if S[i] == 'D': d -= 1
    if l == r or u == d: 
        print("NO")
        exit()

print(("NO", "YES")[l <= sc < r and u <= sr < d])

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

True_arr = [0, 0, 0, 0]

LRUD_S = {"L":0, "R":1, "U":2, "D":3}
LRUD_T = {"R":0, "L":1, "D":2, "U":3}

for i in range(N):
    if True_arr[LRUD_T[T[-i-1]]] > 0:
        True_arr[LRUD_T[T[-i-1]]] -= 1
    
    True_arr[LRUD_S[S[-i-1]]] += 1
    if True_arr[0] == W or True_arr[1] == W or True_arr[2] == H or True_arr[3] == H:
        break

if Sr > True_arr[2] and Sr <= H - True_arr[3] and Sc > True_arr[0] and Sc <= W - True_arr[1]:
    print("YES")
else:
    print("NO")

Solution (Python):
import sys
input=sys.stdin.readline
h,w,n=map(int,input().split())
sx,sy=map(int,input().split())
S=input(); T=input()
u,d=1,h; l,r=1,w
ans='YES'
for i in reversed(range(n)):
    if T[i]=='U':
        d=min(h,d+1)
    if T[i]=='D':
        u=max(1,u-1)
    if T[i]=='L':
        r=min(w,r+1)
    if T[i]=='R':
        l=max(1,l-1)
    if S[i]=='U':
        u+=1
        if u==h+1:
            print('NO')
            break
    if S[i]=='D':
        d-=1
        if d==0:
            print('NO')
            break
    if S[i]=='L':
        l+=1
        if l==w+1:
            print('NO')
            break
    if S[i]=='R':
        r-=1
        if r==0:
            print('NO')
            break
    if r<l or d<u:
        print('NO')
        break
else:
    if sx<u or sx>d or sy<l or sy>r:
        print('NO')
    else:
        print('YES')

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

x = sc

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

x = sc

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

y = sr

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

y = sr

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

print("YES")

Solution (Python):
 
def solve():
    H,W,N= [int(x) for x in input().split()]
     
    Sr,Sc = [int(x) for x in input().split()]
     
    S = input()
    T = input()
     
    minu = Sr
    maxd = Sr
    maxr = Sc
    minl = Sc
    for i in range(N):
        if S[i] == 'U':
            minu -= 1
            if minu == 0:
                return "NO"
        if S[i] == 'D':
            maxd += 1
            if maxd == H+1:
                return "NO"
        if S[i] == 'R':
            maxr += 1
            if maxr == W+1:
                return "NO"
        if S[i] == 'L':
            minl -= 1
            if minl == 0:
                return "NO"
 
        if T[i] == 'D' and minu < H:
            minu += 1
        if T[i] == 'U' and maxd > 1:
            maxd -= 1
        if T[i] == 'L' and maxr > 1:
            maxr -= 1
        if T[i] == 'R' and minl < W:
            minl += 1
 
    return "YES"
 
print(solve())

Solution (Python):
import sys
input=sys.stdin.readline
h,w,n=map(int,input().split())
sx,sy=map(int,input().split())
S=input(); T=input()
u,d=sx,sx; l,r=sy,sy
for i in range(n):
    if S[i]=='U':
        u-=1
    if S[i]=='D':
        d+=1
    if S[i]=='L':
        l-=1    
    if S[i]=='R':
        r+=1
    if u<1 or d>h or l<1 or r>w:
        print('NO')
        break
    if T[i]=='U':
        d=max(1,d-1)
    if T[i]=='D':
        u=min(h,u+1)
    if T[i]=='L':
        r=max(1,r-1)
    if T[i]=='R':
        l=min(w,l+1)
else:
    print('YES')

Solution (Python):
h,w,n = map(int,input().split())
sr,sc = map(int,input().split())
s = input()
t = input()
wr = sc
wl = sc
hu = sr
hd = sr
def taka(k,wr,wl,hu,hd):
    if k=='U':
        hu -= 1
    if k=='D':
        hd += 1
    if k=='R':
        wr += 1
    if k=='L':
        wl -= 1
    return wr,wl,hu,hd
def ao(k,wr,wl,hu,hd):
    if k=='U':
        hd = max(1,hd-1)
    if k=='D':
        hu = min(h,hu+1)
    if k=='R':
        wl = min(w,wl+1)
    if k=='L':
        wr = max(1,wr-1)
    return wr,wl,hu,hd
for i in range(n):
    wr,wl,hu,hd = taka(s[i],wr,wl,hu,hd)
    if wl==0 or hu==0 or hd==h+1 or wr==w+1:
        print('NO')
        exit()
    wr,wl,hu,hd = ao(t[i],wr,wl,hu,hd)
print('YES')

Solution (Python):


H,W,N = map(int,input().split())
init_r,init_c = map(int,input().split())
S = input()
T = input()

r = W+1
l = 0
u = 0
d = H+1

ans = "YES"

for i in range(N):
    t = N-1-i
    
    direction = T[t]
    if direction == "R" and l != 0:
        l -= 1
    elif direction == "L" and r != W+1:
        r += 1
    elif direction == "U" and d != H+1:
        d += 1
    elif direction == "D" and u != 0:
        u -= 1
        

    t = N-1-i
    direction = S[t]
    if direction == "R":
        r -=1
        if r == 1:
            ans = "NO"
            break
    elif direction == "L":
        l +=1
        if l == W:
            ans = "NO"
            break
    elif direction == "U":
        u +=1
        if u == H:
            ans = "NO"
            break
    elif direction == "D":
        d -=1
        if d == 1:
            ans = "NO"
            break
    if r <= l:
        ans = "NO"
        break    
    if d <= u:
        ans = "NO"
        break

if init_c <= l or init_c >= r:
    ans = "NO"
if init_r <= u or init_r >= d:
    ans = "NO"
print(ans)

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

Hmin = 0
Hmax = H-1
Wmin = 0
Wmax = W-1
for i in range(N-1):
    s = S[-1-i]
    if s == 'U':
        Hmin += 1
    elif s == 'D':
        Hmax -= 1
    elif s == 'L':
        Wmin += 1
    else: 
        Wmax -= 1

    t = T[-2-i]
    if t == 'D' and Hmin > 0 and Hmin<=Hmax:
        Hmin -= 1
    elif t == 'U' and Hmax < H-1 and Hmin<=Hmax:
        Hmax += 1
    elif t == 'R' and Wmin > 0 and Wmin<=Wmax:
        Wmin -= 1
    elif t == 'L' and Wmax < W-1 and Wmin<=Wmax:
        Wmax += 1

s = S[-N]
if s == 'U':
    Hmin += 1
elif s == 'D':
    Hmax -= 1
elif s == 'L':
    Wmin += 1
else: 
    Wmax -= 1

if Hmin <= Sr-1 <= Hmax and Wmin <= Sc-1 <= Wmax:
    print('YES')
else:
    print('NO')

Solution (Python):


H,W,N = map(int,input().split())
init_r,init_c = map(int,input().split())
S = input()
T = input()

r = W+1
l = 0
u = 0
d = H+1


ans = "YES"

for i in range(N):
    t = N-1-i

    
    direction = T[t]

    if direction == "R" and l != 0:
        l -= 1
    elif direction == "L" and r != W+1:
        r += 1
    elif direction == "U" and d != H+1:
        d += 1
    elif direction == "D" and u != 0:
        u -= 1

    

    t = N-1-i
    direction = S[t]

    if direction == "R":
        r -=1
        if r == 1:
            ans = "NO"
            break
    elif direction == "L":
        l +=1
        if l == W:
            ans = "NO"
            break
    elif direction == "U":
        u +=1
        if u == H:
            ans = "NO"
            break
    elif direction == "D":
        d -=1
        if d == 1:
            ans = "NO"
            break
    if r <= l:
        ans = "NO"
        break    
    if d <= u:
        ans = "NO"
        break
        






if init_c <= l or init_c >= r:
    ans = "NO"

        
if init_r <= u or init_r >= d:
    ans = "NO"




    
print(ans)

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

x1 = x

is_no = False
for s, t in zip(S, T):
    if s == 'R':
        x1 += 1
    if x1 == W + 1:
        is_no = True
        break
    if t == 'L':
        if x1 != 1:
            x1 -= 1

x1 = x
for s, t in zip(S, T):
    if s == 'L':
        x1 -= 1
    if x1 == 0:
        is_no = True
        break
    if t == 'R':
        if x1 != W:
            x1 += 1

y1 = y
for s, t in zip(S, T):
    if s == 'U':
        y1 -= 1
    if y1 == 0:
        is_no = True
        break
    if t == 'D':
        if y1 != H:
            y1 += 1

y1 = y
for s, t in zip(S, T):
    if s == 'D':
        y1 += 1
    if y1 == H + 1:
        is_no = True
        break
    if t == 'U':
        if y1 != 1:
            y1 -= 1

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

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

wr=Sc
wl=Sc
hu=Sr
hd=Sr


ans='YES'
for i in range(N):
    if S[i]=='R':
        wr+=1
    elif S[i]=='L':
        wl-=1
    elif S[i]=='U':
        hu-=1
    elif S[i]=='D':
        hd+=1
    if wr>W or wl<1 or hu<1 or hd>H:
        ans='NO'
        break

    if T[i]=='R':
        wl = min(W,wl+1)
    elif T[i]=='L':
        wr = max(1,wr-1)
    elif T[i]=='U':
        hd= max(1,hd-1)
    elif T[i]=='D':
        hu=min(H,hu+1)

print(ans)

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

ssr -= 1
ssc -= 1

A = ["L", "R", "U", "D"]

A2 = {"L": -1, "R": 1, "U": -1, "D": 1}

D ={"L": "R", "R": "L", "U": "D", "D": "U"}

ans = "YES"

flg = 0

for a in A :
    
    sr = ssr
    sc = ssc
    
    if flg :
        break
        
    for i in range(N) :
        if (a in ["L", "R"]) and (a == S[i]) :
            sc += A2[a]
            
            if (sc < 0) or (sc >= W) :
                ans = "NO"
                flg = 1
                break

        elif (a in ["U", "D"]) and (a == S[i]) :
            sr += A2[a]
            
            if (sr < 0) or (sr >= H) :
                ans = "NO"
                flg = 1
                break
            
        if (a in ["L", "R"]) and (D[a] == T[i]) :
            if (sc == 0) and (D[a] == "L") :
                continue
            elif (sc == W-1) and (D[a] == "R") :
                continue
            sc += A2[D[a]]
            

            
        elif (a in ["U", "D"]) and (T[i] == D[a]) :
            if (sr == 0) and (D[a] == "U") :
                continue
            elif (sr == H-1) and (D[a] == "D") :
                continue
            sr += A2[D[a]]
        
print(ans)

Solution (Python):
h, w, n = map(int, input().split())
ar, ac = map(int, input().split())
s = input()
t = input()
r = ac
l = ac
u = ar
d = ar
ans = False

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

Solution (Python):
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
 
up = [0] * (N+1)
down = [0] * (N+1)
right = [0] * (N+1)
left = [0] * (N+1)
maxU, maxD, maxR, maxL = 0, 0, 0, 0
 
for i in range(N):
    if S[i] == "L":
        left[i+1] = left[i] + 1
        right[i+1] = right[i]
        up[i+1] = up[i]
        down[i+1] = down[i]
        maxL = max(maxL, left[i+1])
    elif S[i] == "R":
        right[i+1] = right[i] + 1
        up[i+1] = up[i]
        down[i+1] = down[i]
        left[i+1]  = left[i]
        maxR = max(maxR, right[i+1])
    elif S[i] == "U":
        up[i+1] = up[i] + 1
        right[i+1] = right[i]
        down[i+1] = down[i]
        left[i+1]  = left[i]
        maxU = max(maxU, up[i+1])
    else:
        down[i+1] = down[i] + 1
        right[i+1] = right[i]
        up[i+1] = up[i]
        left[i+1]  = left[i]
        maxD = max(maxD, down[i+1])
    if T[i] == "L":
        right[i+1] = max(right[i+1] - 1, -sc + 1)
    elif T[i] == "R":
        left[i+1] = max(left[i+1] - 1, sc - W)
    elif T[i] == "U":
        down[i+1] = max(down[i+1] - 1, -sr + 1)
    else:
        up[i+1] = max(up[i+1] - 1, sr - H)
 
if sr + maxD > H or sr - maxU <= 0 or sc + maxR > W or sc - maxL <= 0:
    print("NO")
else:  print("YES")

Solution (Python):
from collections import Counter
h, w, n = map(int,input().split())
x, y = map(int,input().split())
S = input()
T = input()
cs = Counter(S)
u = x
d = h-x+1
l = y
r = w-y+1
if not (u <= cs['U'] or d <= cs['D'] or l <= cs['L'] or r <= cs['R']):
    print('YES')
    exit()

a = x
if u <= cs['U']:
    for s, t in zip(S, T):
        if s == 'U':
            a -= 1
        if a < 1:
            print('NO')
            exit()
        if t == 'D' and a < h:
            a += 1
        if a < 1:
            print('NO')
            exit()
    
a = x   
if d <= cs['D']:
    for s, t in zip(S, T):
        if s == 'D':
            a += 1
        if a > h:
            print('NO')
            exit()
        if t == 'U' and a > 1:
            a -= 1
        if a > h:
            print('NO')
            exit()

a = y
if l <= cs['L']:
    for s, t in zip(S, T):
        if s == 'L':
            a -= 1
        if a < 1:
            print('NO')
            exit()
        if t == 'R' and a < w:
            a += 1
        if a < 1:
            print('NO')
            exit()
            
a = y
if r <= cs['R']:
    for s, t in zip(S, T):
        if s == 'R':
            a += 1
        if a > w:
            print('NO')
            exit()
        if t == 'L' and a > 1:
            a -= 1
        if a > w:
            print('NO')
            exit()

print('YES')

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

l = sc
r = sc
u = sr
d = sr

flag = True
for i in range(n):
    if s[i] == "R":
        r += 1
    if s[i] == "L":
        l -= 1
    if s[i] == "U":
        u -= 1
    if s[i] == "D":
        d += 1
    if l==0 or r==w+1 or u==0 or d==h+1:
        print("NO")
        flag = False
        break
    if t[i] == "R" and l != w:
        l += 1
    if t[i] == "L" and r != 1:
        r -= 1
    if t[i] == "U" and d != 1:
        d -= 1
    if t[i] == "D" and u != h:
        u += 1

if flag != False:
    print("YES")

Solution (Python):
H,W,N = map(int,input().split())
sr, sc = map(int,input().split())
S = input()
T = input()
L = sc-1
R = W-sc
U = sr-1
D = H-sr
for s, t in zip(S,T):
    if s == 'L':
        L -= 1
    elif s == 'R':
        R -= 1
    elif s == 'U':
        U -= 1
    elif s == 'D':
        D -= 1
    if min(L,R,U,D) < 0:
        print('NO')
        quit()
    if t == 'L' and R < W-1:
        R += 1
    elif t == 'R' and L < W-1:
        L += 1
    elif t == 'U' and D < H-1:
        D += 1
    elif t == 'D' and U < H-1:
        U += 1
print('YES')

Solution (Python):
import sys
from itertools import accumulate

def main():
    input = sys.stdin.readline
    H, W, N = map(int, input().split())
    r, c = map(int, input().split())
    S = str(input().strip())
    T = str(input().strip())

    R = c
    L = c
    U = r
    D = r

    i = 0
    while i < N:
        s, t = S[i], T[i]

        if s == 'R':
            R += 1
        elif s == 'L':
            L -= 1
        elif s == 'U':
            U -= 1
        else:
            D += 1

        if R > W or L <= 0 or U <= 0 or D > H:
            return 'NO'

        if t == 'R':
            L += 1
            L = min(L, W)
        elif t == 'L':
            R -= 1
            R = max(R, 1)
        elif t == 'U':
            D -= 1
            D = max(D, 1)
        else:
            U += 1
            U = min(U, H)

        i += 1

    return 'YES' 


if __name__ == '__main__':
    print(main())

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

S_d = {"L":0,"R":0,"U":0,"D":0}
M_d = {"L":0,"R":0,"U":0,"D":0}

reverse_d = {"L":"R","R":"L","U":"D","D":"U"}

S_d[S[0]] += 1
M_d[S[0]] += 1

if ((sr == 1 and S[0] == "U") or
        (sr == H and S[0] == "D") or
        (sc == 1 and S[0] == "L") or
        (sc == W and S[0] == "R")):
    print("NO")
else:
    for i in range(N-1):
        M_d[reverse_d[T[i]]] = max(0,M_d[reverse_d[T[i]]]-1)
        S_d[reverse_d[T[i]]] = S_d[reverse_d[T[i]]] - 1
        M_d[S[i+1]] += 1
        S_d[S[i+1]] += 1
        if (M_d["U"] >= H or M_d["D"] >= H or M_d["L"] >= W or M_d["R"] >= W):
            print("NO")
            break
        if (S_d["U"]>=sr or S_d["D"]>= H+1-sr or S_d["L"]>=sc or S_d["R"]>=W+1-sc):
            print("NO")
            break
    else:
        print("YES")

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

ld=H-sr
lu=sr-1
lr=W-sc
ll=sc-1

for i in range(N):
  x=S[i]
  y=T[i]

  if x=='U':
    lu-=1
  elif x=='D':
    ld-=1
  elif x=='L':
    ll-=1
  elif x=='R':
    lr-=1

  

  if lu<0 or ld<0 or ll<0 or lr<0:
    print('NO')
    exit()

  if y=='U' and ld<H-1:
    ld+=1
  elif y=='D' and lu<H-1:
    lu+=1
  elif y=='L' and lr<W-1:
    lr+=1
  elif y=='R' and ll<W-1:
    ll+=1

  

print('YES')

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

ld=H-sr
lu=sr-1
lr=W-sc
ll=sc-1

for i in range(N):
  x=S[i]
  y=T[i]

  if x=='U':
    lu-=1
  elif x=='D':
    ld-=1
  elif x=='L':
    ll-=1
  elif x=='R':
    lr-=1

  

  if lu<0 or ld<0 or ll<0 or lr<0:
    print('NO')
    exit()

  if y=='U' and ld<H-1:
    ld+=1
  elif y=='D' and lu<H-1:
    lu+=1
  elif y=='L' and lr<W-1:
    lr+=1
  elif y=='R' and ll<W-1:
    ll+=1

  

print('YES')

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

up = h
down = 1 
right = w
left = 1
for i in range(n-1,-1,-1):
    if i != n-1:
        if t[i] == 'D':
            down = max(down-1,1)
        elif t[i] == 'U':
            up = min(up+1,h)
        elif t[i] == 'R':
            left = max(left-1,1)
        else:
            right = min(right+1,w)
    if s[i] == 'D':
        up -= 1
    elif s[i] == 'U':
        down += 1
    elif s[i] == 'R':
        right -= 1
    else:
        left += 1
    
    
    

    if right == 0 or left == w or up == 0 or down == h:
        print('NO')
        exit()
    if right<left or up<down:
        print('NO')
        exit()

if up>=y and down<=y and right >= x and left<=x:
    print('YES')
else:
    print('NO')

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

Solution (Python):
import numpy as np

H, W, N = map(int, input().split())
si, sj = map(int, input().split())
si, sj = si - 1, sj - 1

S = input()
T = input()


min_i = si
max_i = si
min_j = sj
max_j = sj

piece_remaining = True

for t in range(N):
	if S[t] == 'U':
		min_i -= 1
	elif S[t] == 'D':
		max_i += 1
	elif S[t] == 'L':
		min_j -= 1
	elif S[t] == 'R':
		max_j += 1

	if min_i < 0 or H <= max_i or min_j < 0 or W <= max_j:
		piece_remaining = False
		break

	if T[t] == 'U':
		max_i = max(0, max_i - 1)
	elif T[t] == 'D':
		min_i = min(H - 1, min_i + 1)
	elif T[t] == 'L':
		max_j = max(0, max_j - 1)
	elif T[t] == 'R':
		min_j = min(W - 1, min_j + 1)

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