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 sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

h=sr
for i in range(N):
    if S[i]=='U':
        h-=1
    if h==0:
        NO()
        exit()
    if T[i]=='D':
        if h<H:
            h+=1
h=sr
for i in range(N):
    if S[i]=='D':
        h+=1
    if h>H:
        NO()
        exit()
    if T[i]=='U':
        if h>1:
            h-=1
w=sc
for i in range(N):
    if S[i]=='L':
        w-=1
    if w==0:
        NO()
        exit()
    if T[i]=='R':
        if w<W:
            w+=1
w=sc
for i in range(N):
    if S[i]=='R':
        w+=1
    if w>W:
        NO()
        exit()
    if T[i]=='L':
        if w>1:
            w-=1
YES()

Solution (Python):
from sys import exit
from collections import defaultdict
H,W,N = [int(n) for n in input().split()]
Sr,Sc = [int(n) for n in input().split()]

Dout = H-Sr + 1

Uout = Sr

Rout = W - Sc + 1

Lout = Sc

S = str(input())
T = str(input())
ptrC = defaultdict(int)
ptrA = defaultdict(int)
for s,t in zip(S,T):
    ptrC[s]+=1
    ptrA[t]+=1




def check(outcnt, C, A, lim, B):
    backed = 0
    for s,t in zip(S,T):
        if s == B and outcnt == lim -1 and backed > 0:
            outcnt -= 1
            backed -= 1
        if( s == C ):
            outcnt -= 1
        if( outcnt == 0 ):
            print("NO")
            exit()
        if( t == A and outcnt < lim ):
            outcnt += 1
            backed += 1

check(Rout, "R", "L", W, "L")
check(Lout, "L", "R", W, "R")
check(Uout, "U", "D", H, "D")
check(Dout, "D", "U", H, "U")
print("YES")

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

lst = [0,0,0,0] 






for i in range(N):
    if S[i] == 'L':
        lst[0] += 1
    if S[i] == 'R':
        lst[1] += 1
    if S[i] == 'U':
        lst[2] += 1
    if S[i] == 'D':
        lst[3] += 1
    
    if lst[0] >= sc+1 or lst[1] >= W-sc or lst[2] >= sr+1 or lst[3] >= H-sr:
        print ('NO')
        sys.exit()
    if T[i] == 'L':
        
        
        
        lst[1] -= 1
        if lst[1] <= -(sc+1)+1:
            lst[1] = -(sc+1)+1
    if T[i] == 'R':
        
        
        
        lst[0] -= 1
        if lst[0] <= -(W-sc)+1:
            lst[0] = -(W-sc)+1
    if T[i] == 'U':
        
        
        
        lst[3] -= 1
        if lst[3] <= -(sr+1)+1:
            lst[3] = -(sr+1)+1
    if T[i] == 'D':
        
        
        
        lst[2] -= 1
        if lst[2] <= -(H-sr)+1:
            lst[2] = -(H-sr)+1
    

print ('YES')

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

atk_U = [0]*N
atk_D = [0]*N
atk_L = [0]*N
atk_R = [0]*N
u = 0
d = 0
l = 0
r = 0
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
  atk_U[i] = u
  atk_D[i] = d
  atk_L[i] = l
  atk_R[i] = r

def_U = [0]*N
def_D = [0]*N
def_L = [0]*N
def_R = [0]*N
u = 0
d = 0
l = 0
r = 0
for i in range(N-1):
  if T[i] == 'U':
    if u + 1 - atk_D[i] < Sr:
      u += 1
  if T[i] == 'D':
    if d + 1 - atk_U[i] < H - Sr + 1:
      d += 1
  if T[i] == 'L':
    if l + 1 - atk_R[i] < Sc:
      l += 1
  if T[i] == 'R':
    if r + 1 - atk_L[i] < W - Sc + 1:
      r += 1
  def_U[i+1] = d
  def_D[i+1] = u
  def_L[i+1] = r
  def_R[i+1] = l

dam_U = max([atk_U[i] - def_U[i] for i in range(N)])
dam_D = max([atk_D[i] - def_D[i] for i in range(N)])
dam_L = max([atk_L[i] - def_L[i] for i in range(N)])
dam_R = max([atk_R[i] - def_R[i] for i in range(N)])
if dam_U < Sr and dam_D < H - Sr + 1 and dam_L < Sc and dam_R < W - Sc + 1:
  print('YES')
else:
  print('NO')

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



su = sr
sd = sr
sle = sc
sri = sc 
flag = False
for ss, tt in zip(S, T):

    if ss == "U":
        su -= 1
    if ss == "D":
        sd += 1
    if ss == "R":
        sri += 1
    if ss == "L":
        sle -= 1

    if su == 0 or sd > H or sri > W or sle == 0:
        flag = True    

    if tt == "U" and sd > 1:
        sd -= 1
    if tt == "D" and su < H: 
        su += 1
    if tt == "R" and sle < W:
        sle += 1
    if tt == "L" and sri > 1:
        sri -= 1

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

Solution (Python):
"""
LRとUDは独立に考えて良い
"""

import unittest


def main():
    H, W, N = map(int, input().split())

    sr, sc = map(int, input().split())

    S = input()
    T = input()

    print(solve(H, W, N, sr, sc, S, T))


def pp(c,s):
    
    return True


def solve(h, w, n, sr, sc, s, t):
    s_lr = []
    s_ud = []
    for c in s[::-1]:
        if c == 'L':
            s_lr.append(-1)
            s_ud.append(0)
        elif c == 'R':
            s_lr.append(+1)
            s_ud.append(0)
        elif c == 'U':
            s_lr.append(0)
            s_ud.append(-1)
        elif c == 'D':
            s_lr.append(0)
            s_ud.append(+1)

    t_lr = []
    t_ud = []
    for c in t[::-1]:
        if c == 'L':
            t_lr.append(-1)
            t_ud.append(0)
        elif c == 'R':
            t_lr.append(+1)
            t_ud.append(0)
        elif c == 'U':
            t_lr.append(0)
            t_ud.append(-1)
        elif c == 'D':
            t_lr.append(0)
            t_ud.append(+1)

    if aoki_win(n, s_ud, t_ud, h, sr) and aoki_win(n, s_lr, t_lr, w, sc):
        return 'YES'
    else:
        return 'NO'


def aoki_win(n, chokudai, aoki, l, sp):
    """
    最後から範囲を削っていく
    """
    
    pp('AO', aoki)
    pp('CH',chokudai)

    chokudai_minus = 0
    chokudai_plus = 0
    for c in chokudai:
        if c == 1:
            chokudai_plus += 1
        elif c == -1:
            chokudai_minus += 1
    if 1 <= sp-chokudai_minus and sp+chokudai_plus <= l:
        
        return True
    chokudai_right = l if chokudai[0] == 1 else l+1
    chokudai_left = 1 if chokudai[0] == -1 else 0
    
    

    for i in range(1, n):
        
        if aoki[i] == 1:
            
            chokudai_left = max(0, chokudai_left-1)
        elif aoki[i] == -1:
            
            chokudai_right = min(l+1, chokudai_right+1)
        if chokudai_left+1 >= chokudai_right:
            
            
            return False
        if chokudai[i] == 1:
            chokudai_right = max(chokudai_right-1, 1)
        elif chokudai[i] == -1:
            chokudai_left = min(chokudai_left+1, l)
        if chokudai_left+1 >= chokudai_right:
            
            
            return False
    
    if sp<=chokudai_left or chokudai_right<=sp:
        
        return False
    
    

    return True

class TestSolve(unittest.TestCase):
    def test_solve_case_1(self):
        h = 2
        w = 3
        n = 3
        sr = 2
        sc = 2
        s = 'RRL'
        t = 'LUD'
        actual = solve(h,w,n,sr,sc,s,t)
        expected = 'YES'
        self.assertEqual(expected, actual)

    def test_solve_case_2(self):
        h = 4
        w = 3
        n = 5
        sr = 2
        sc = 2
        s = 'UDRRR'
        t = 'LLDUD'
        actual = solve(h,w,n,sr,sc,s,t)
        expected = 'NO'
        self.assertEqual(expected, actual)

    def test_solve_case_3(self):
        h = 5
        w = 6
        n = 11
        sr = 2
        sc = 1
        s = 'RLDRRUDDLRL'
        t = 'URRDRLLDLRD'
        actual = solve(h,w,n,sr,sc,s,t)
        expected = 'NO'
        self.assertEqual(expected, actual)

    def test_solve_case_4(self):
        h = 1
        w = 3
        n = 6
        sr = 1
        sc = 2
        s = 'LRRRRR'
        t = 'LRLLRL'
        actual = solve(h,w,n,sr,sc,s,t)
        expected = 'NO'
        self.assertEqual(expected, actual)

    def test_solve_case_5(self):
        h = 1
        w = 3
        n = 4
        sr = 1
        sc = 2
        s = 'LLLL'
        t = 'RRRR'
        actual = solve(h,w,n,sr,sc,s,t)
        expected = 'YES'
        self.assertEqual(expected, actual)

    def test_solve_case_6(self):
        h = 3
        w = 2
        n = 4
        sr = 2
        sc = 2
        s = 'UDLUU'
        t = 'DRRRR'
        actual = solve(h,w,n,sr,sc,s,t)
        expected = 'NO'
        self.assertEqual(expected, actual)


if __name__ == "__main__":
    main()

Solution (Python):
import sys

readline = sys.stdin.readline

H, W, N = [int(a) for a in readline().split()]

sr, sc = [int(a) for a in readline().split()]

S = readline().strip()
T = readline().strip()

S_max = {'L':0, 'R':0, 'U':0, 'D':0}
T_max = {'L':0, 'R':0, 'U':0, 'D':0}
maxes = {'L':0, 'R':0, 'U':0, 'D':0}
anti = {'L':'R', 'R':'L', 'U':'D', 'D':'U'}

def check():
    return (-maxes['L'] + sc < 1) or (maxes['R'] + sc > W) or \
    (-maxes['U'] + sr < 1) or (maxes['D'] + sr > H)

for i in range(N):





    
    maxes[S[i]] += 1
    if check():
        print('NO')
        exit()
    maxes[anti[T[i]]] -= 1
    maxes['L'] = max(maxes['L'], sc - W)
    maxes['R'] = max(maxes['R'], -sc + 1)
    maxes['U'] = max(maxes['U'], sr - H)
    maxes['D'] = max(maxes['D'], -sr + 1)







print('YES')

Solution (Python):
def li():
    return list(map(int, input().split()))


[H, W, N] = li()
s = li()
S = input()
T = input()

if __name__ == '__main__':
    
    is_no = False
    takahashi = [s[0], s[1]]
    for index, (S_i, T_i) in enumerate(zip(S, T)):
        if S_i == 'L':
            takahashi[1] = takahashi[1] - 1
        if S_i == 'U':
            takahashi[0] = takahashi[0] - 1
        if min(takahashi) <= 0:
            is_no = True
            break
        if T_i == 'R':
            takahashi[1] = min(takahashi[1] + 1, W)
        if T_i == 'D':
            takahashi[0] = min(takahashi[0] + 1, H)

    if not is_no:
        
        takahashi = [s[0], s[1]]
        for index, (S_i, T_i) in enumerate(zip(S, T)):
            if S_i == 'R':
                takahashi[1] = takahashi[1] + 1
            if S_i == 'D':
                takahashi[0] = takahashi[0] + 1
            if takahashi[0] > H or takahashi[1] > W:
                is_no = True
                break
            if T_i == 'L':
                takahashi[1] = max(takahashi[1] - 1, 1)
            if T_i == 'U':
                takahashi[0] = max(takahashi[0] - 1, 1)

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

Solution (Python):
h,w,n=map(int,input().split())
r,c=map(int,input().split())
s=input()
t=input()
import sys
l_l=c
r_l=w+1-c
u_l=r
d_l=h+1-r
s_count=[0,0,0,0]
t_count=[0,0,0,0]
if s[0]=='L':
    s_count[0]=s_count[0]+1
elif s[0]=='R':
    s_count[1]=s_count[1]+1
elif s[0]=='U':
    s_count[2]=s_count[2]+1
elif s[0]=='D':
    s_count[3]=s_count[3]+1
if s_count[0]-t_count[1]>=l_l:
    print('NO')
    sys.exit()
if s_count[1]-t_count[0]>=r_l:
    print('NO')
    sys.exit()
if s_count[2]-t_count[3]>=u_l:
    print('NO')
    sys.exit()
if s_count[3]-t_count[2]>=d_l:
    print('NO')
    sys.exit()
for i in range(n-1):
    if t[i]=='L' and t_count[0]+1-s_count[1]<l_l:
        t_count[0]=t_count[0]+1
    elif t[i]=='R' and t_count[1]+1-s_count[0]<r_l:
        t_count[1]=t_count[1]+1
    elif t[i]=='U' and t_count[2]+1-s_count[3]<u_l:
        t_count[2]=t_count[2]+1
    elif t[i]=='D' and t_count[3]+1-s_count[2]<d_l:
        t_count[3]=t_count[3]+1
    if s[i+1]=='L':
        s_count[0]=s_count[0]+1
    elif s[i+1]=='R':
        s_count[1]=s_count[1]+1
    elif s[i+1]=='U':
        s_count[2]=s_count[2]+1
    elif s[i+1]=='D':
        s_count[3]=s_count[3]+1
    if s_count[0]-t_count[1]>=l_l:
        print('NO')
        sys.exit()
    if s_count[1]-t_count[0]>=r_l:
        print('NO')
        sys.exit()
    if s_count[2]-t_count[3]>=u_l:
        print('NO')
        sys.exit()
    if s_count[3]-t_count[2]>=d_l:
        print('NO')
        sys.exit()
print('YES')

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

S = input()
T = input()
nowh = sr-1
noww = sc-1
flag = False
FLAG = False
for i in range(N):
    if S[i] == 'U':
        nowh -= 1
    if nowh < 0 or nowh >= H:
        flag = True
        break
    if T[i] == 'D' and nowh < H-1:
        nowh += 1
if flag:
    FLAG = True

nowh = sr-1
noww = sc-1
flag = False
for i in range(N):
    if S[i] == 'D':
        nowh += 1
    if nowh < 0 or nowh >= H:
        flag = True
        break
    if T[i] == 'U' and nowh > 0:
        nowh -= 1
if flag:
    FLAG = True

nowh = sr-1
noww = sc-1
flag = False
for i in range(N):
    if S[i] == 'R':
        noww += 1
    if noww < 0 or noww >= W:
        flag = True
        break
    if T[i] == 'L' and noww > 0:
        noww -= 1
if flag:
    FLAG = True

nowh = sr-1
noww = sc-1
flag = False
for i in range(N):
    if S[i] == 'L':
        noww -= 1
    if noww < 0 or noww >= W:
        flag = True
        break
    if T[i] == 'R' and noww < W-1:
        noww += 1
if flag:
    FLAG = True

if FLAG:
    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 = sc
r = W -sc + 1
u = sr
d = H - sr + 1

s = [0] * 4
ans = 'YES'
for i in range(N):
    if S[i] == 'L':
        l -= 1
        if r == W:
            r -= 1
    elif S[i] == 'R':
        r -= 1
        if l == W:
            l -= 1
    elif S[i] == 'U':
        u -= 1
        if d == H:
            d -= 1
    elif S[i] == 'D':
        d -= 1
        if u == H:
            u -= 1

    if l*r*u*d <= 0:
        ans ='NO'
        break

    if T[i] == 'L' and r < W:
        r += 1
    elif T[i] == 'R' and l < W:
        l += 1
    elif T[i] == 'U' and d < H:
        d += 1
    elif T[i] == 'D' and u < H:
        u += 1
print(ans)

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

Scount = {'L':[0],'R':[0],'U':[0],'D':[0]}
Tcount = {'L':[0],'R':[0],'U':[0],'D':[0]}

rev = {'L':'R','D':'U','U':'D','R':'L'}

for i in range(N):
    for c in 'LRUD':
        if S[i] == c:
            Scount[S[i]].append(Scount[c][-1]+1)
        else:
            Scount[c].append(Scount[c][-1])
    for c in 'LRUD':
        if T[i] == c:
            if c == 'L':
                Tcount[T[i]].append(min(Tcount[T[i]][-1]+1,  Scount['R'][i+1] + sc - 1))
            if c == 'R':
                Tcount[T[i]].append(min(Tcount[T[i]][-1]+1,  W + Scount['L'][i+1] - sc))
            if c == 'D':
                Tcount[T[i]].append(min(Tcount[T[i]][-1]+1,  H + Scount['U'][i+1] - sr))
            if c == 'U':
                Tcount[T[i]].append(min(Tcount[T[i]][-1]+1,  Scount['D'][i+1] + sr - 1))
        else:
            Tcount[c].append(Tcount[c][-1])




for i in range(1, N+1):
    if (sc - (Scount['L'][i] - Tcount['R'][i-1]) < 1) or \
        (sc + (Scount['R'][i] - Tcount['L'][i-1]) > W) or \
        (sr + (Scount['D'][i] - Tcount['U'][i-1]) > H) or \
        (sr - (Scount['U'][i] - Tcount['D'][i-1]) < 1):
        print("NO")
        exit(0)

print("YES")

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
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

h,w,n = na()
sh,sw = na()
s = ns()
t = ns()

iit = [sw,sh,h-sh+1,w-sw+1]


tshi = [0,0,0,0]
aoki = [0,0,0,0]

for i in range(n):
    if s[i] == 'L':
        tshi[0] += 1
    if s[i] == 'U':
        tshi[1] += 1
    if s[i] == 'D':
        tshi[2] += 1
    if s[i] == 'R':
        tshi[3] += 1
    for j in range(4):
        if tshi[j] >= iit[j]+aoki[3-j]:
            print('NO')
            exit()
    if t[i] == 'L':
        aoki[0] += 1
    if t[i] == 'U':
        aoki[1] += 1
    if t[i] == 'D':
        aoki[2] += 1
    if t[i] == 'R':
        aoki[3] += 1
    for j in range(4):
        if aoki[j] >= iit[j]+tshi[3-j]:
            aoki[j] -= 1

print('YES')

Solution (Python):
def getlist():
	return list(map(int, input().split()))

H, W, N = getlist()
h, w = getlist()
S = list(input())
T = list(input())

sl, sr, su, sd = 0, 0, 0, 0
tl, tr, tu, td = 0, 0, 0, 0
judge = "YES"

for i in range(N):

	if S[i] == "L":
		sl += 1
		if sl >= tr + w:
			judge = "NO"
			break
	elif S[i] == "R":
		sr += 1
		if sr >= W - w + tl + 1:
			judge = "NO"
			break
	elif S[i] == "U":
		su += 1
		if su >= td + h:
			judge = "NO"
			break
	else:
		sd += 1
		if sd >= H - h + tu + 1:
			judge = "NO"
			break
			
	if T[i] == "L":
		tl += 1
	elif T[i] == "R":
		tr += 1
	elif T[i] == "U":
		tu += 1
	else:
		td += 1

	if tl >= sr + w:
		tl -= 1
	if tr >= sl + W - w + 1:
		tr -= 1
	if tu >= sd + h:
		tu -= 1
	if td >= su + H - h + 1:
		td -= 1

	
print(judge)

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

Solution (Python):
H,W,N = map(int,input().split())
ah,aw = map(int,input().split())
d = [aw,W-aw+1,ah,H-ah+1]
S = list(input())
T = list(input())

C = []
for i,LRUD in enumerate([["L","R"],["R","L"],["U","D"],["D","U"]]):
  if d[i] <= S.count(LRUD[0]):
    if d[i] <= S.count(LRUD[0])-T.count(LRUD[1]):
      print("NO")
      exit()
    else:
      C.append([i,LRUD])
for x in C:
  count = d[x[0]]
  if S[0] == x[1][0]:count -= 1
  for i in range(len(S)-1):
    if T[i] == x[1][1]:
      if (x[1][1] == "R" or x[1][1]== "L") and count < W:count += 1
      elif (x[1][1] == "U" or x[1][1]== "D") and count < H:count += 1
    if S[i+1] == x[1][0]:count -= 1
    if count <= 0:
      print("NO")
      exit()

print("YES")

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

SR = sr
SC = sc
sr = SR
sc = SC
for i in range(n):
    if S[i] == "L":
        sc -= 1
    if sc <= 0:
        break
    if T[i] == "R" and sc < w:
        sc += 1
if sc <= 0:
    win = 1


sr = SR
sc = SC
for i in range(n):
    if S[i] == "R":
        sc += 1
    if sc > w:
        break
    if T[i] == "L" and sc > 1:
        sc -= 1
if sc > w:
    win = 1


sr = SR
sc = SC
for i in range(n):
    if S[i] == "D":
        sr += 1
    if sr > h:
        break
    if T[i] == "U" and sr > 1:
        sr -= 1
if sr > h:
    win = 1


sr = SR
sc = SC
for i in range(n):
    if S[i] == "U":
        sr -= 1
    if sr <= 0:
        break
    if T[i] == "D" and sr < h:
        sr += 1
if sr <= 0:
    win = 1

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

Solution (Python):
from sys import exit
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
ul = 1
dl = H
rl = W
ll = 1
for s, t in zip(S[1::][::-1], T[:-1:][::-1]):
    if s == 'U':
        ul += 1
    elif s == 'D':
        dl -= 1
    elif s == 'L':
        ll += 1
    else:
        rl -= 1
    if ul > dl or ll > rl or ul == H + 1 or dl == 0 or ll == W + 1 or rl == 0:
        print('NO')
        exit()
    if t == 'U':
        dl = min(H, dl+1)
    elif t == 'D':
        ul = max(1, ul-1)
    elif t == 'L':
        rl = min(W, rl+1)
    else:
        ll = max(1, ll-1)
s = S[0]
if s == 'U':
    ul += 1
elif s == 'D':
    dl -= 1
elif s == 'L':
    ll += 1
else:
    rl -= 1

if ul <= sr <= dl and ll <= sc <= rl:
    print('YES')
else:
    print('NO')

Solution (Python):

import sys

YES = "YES"
NO = "NO"

def can_leave(n, p, S, T):
    min_p, max_p = p, p
    for i in range(len(S)):
        
        if S[i] > 0:
            max_p += 1
        if S[i] < 0:
            min_p -= 1
        if max_p > n or min_p < 1:
            return False

        if min_p < n and T[i] > 0:
            min_p += 1
        if max_p > 1 and T[i] < 0:
            max_p -= 1
    return True


def solve(h: int, w: int, N: int, sr: int, sc: int, S: str, T: str):
    def make(st, n):
        ret_h, ret_w = [], []
        for i in range(n):
            if st[i] == 'U':
                ret_h.append(-1)
                ret_w.append(0)
            if st[i] == 'D':
                ret_h.append(1)
                ret_w.append(0)
            if st[i] == 'L':
                ret_w.append(-1)
                ret_h.append(0)
            if st[i] == 'R':
                ret_w.append(1)
                ret_h.append(0)
        return ret_h, ret_w

    hs, ws = make(S, N)
    ht, wt = make(T, N)
    h_can = can_leave(h, sr, hs, ht)
    w_can = can_leave(w, sc, ws, wt)
    
    
    
    
    
    
    
    if  h_can and w_can:
        ret = YES
    else:
        ret = NO
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  
    W = int(next(tokens))  
    N = int(next(tokens))  
    s_r = int(next(tokens))  
    s_c = int(next(tokens))  
    S = next(tokens)  
    T = next(tokens)  
    solve(H, W, N, s_r, s_c, S, T)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
SLRUD = [0 for i in range(4)]
TLRUD = [0 for i in range(4)]
LRUD = {'L':0, 'R':1, 'U':2, 'D':3}
RevLRUD = {'L':1, 'R':0, 'U':3, 'D':2}
ans = [sc, W-sc+1, sr, H-sr+1]
flag = False
for i in range(N):
    si = LRUD[S[i]]
    ti = LRUD[T[i]]
    SLRUD[si] += 1
    if ans[si] - (SLRUD[si] - TLRUD[RevLRUD[S[i]]]) <= 0:
        flag = True
        break
    TLRUD[ti] += 1
    if ans[ti] + SLRUD[RevLRUD[T[i]]] - TLRUD[ti] <= 0:
        
        
        TLRUD[ti] -= 1
    
    
if flag:
    print('NO')
else:
    print('YES')

Solution (Python):
H, W, N = map( int, input().split())
sx, sy = map( int, input().split())
sx, sy = sy-1, sx-1
S = input()
T = input()
ans = "YES"
x = sx
for i in range(N):
    if S[i] == "L":
        x -= 1
    if x < 0:
        ans = "NO"
        break
    if T[i] == "R":
        if x < W-1:
            x += 1
x = sx
for i in range(N):
    if S[i] == "R":
        x += 1
    if x > W-1:
        ans = "NO"
        break
    if T[i] == "L":
        if x > 0 :
            x -= 1
y = sy
for i in range(N):
    if S[i] == "U":
        y -= 1
    if y < 0:
        ans = "NO"
        break
    if T[i] == "D":
        if y < H-1 :
            y+= 1

y = sy
for i in range(N):
    if S[i] == "D":
        y += 1
    if y > H-1:
        ans = "NO"
        break
    if T[i] == "U":
        if y > 0 :
            y-= 1            
print(ans)

Solution (Python):

import sys
INF = float("inf")


def yes():
    print("YES")  


def no():
    print("NO")  


def solve(H: int, W: int, N: int, s_r: int, s_c: int, S: str, T: str):

    winner = "保留"

    cy, cx = s_r, s_c
    for i in range(N):
        
        if S[i] == "R":
            cx += 1
        elif S[i] == "D":
            cy += 1

        if H < cy or W < cx:
            winner = "TAKAHASHI"
            break

        
        if T[i] == "L" and cx > 1:
            cx -= 1
        elif T[i] == "U" and cy > 1:
            cy -= 1

    if winner == "TAKAHASHI":
        no()
        return

    cy, cx = s_r, s_c
    for i in range(N):
        
        if S[i] == "L":
            cx -= 1
        elif S[i] == "U":
            cy -= 1

        if cy <= 0 or cx <= 0:
            winner = "TAKAHASHI"
            break

        
        if T[i] == "R" and cx < W:
            cx += 1
        elif T[i] == "D" and cy < H:
            cy += 1

    if winner == "TAKAHASHI":
        no()
    else:
        yes()
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  
    W = int(next(tokens))  
    N = int(next(tokens))  
    s_r = int(next(tokens))  
    s_c = int(next(tokens))  
    S = next(tokens)  
    T = next(tokens)  
    solve(H, W, N, s_r, s_c, S, T)


if __name__ == '__main__':
    main()

Solution (Python):




def main():
  height, width, _ = map(int, input().split())
  r, c = map(int, input().split())
  s_coms = input()[::-1]
  t_coms = input()[::-1]
  left = 0
  right = 0
  up = 0
  down = 0

  for s, t in zip(s_coms, t_coms):
    if t == 'L':
      right = max(right - 1, 0)
    elif t == 'R':
      left = max(left - 1, 0)
    elif t == 'U':
      down = max(down - 1, 0)
    elif t == 'D':
      up = max(up - 1, 0)

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

    if left >= width or right >= width or up >= height or down >= height:
      print('NO', flush=True)
      return

  if up < r <= height - down and left < c <= width - right:
    print('YES')
  else:
    print('NO')


if __name__ == '__main__':
  main()

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):
MAX_INT = int(10e10)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

H,W,N = IL()
Sr,Sc = IL()
s = S()
t = S()

U = s.count("U")
D = s.count("D")
L = s.count("L")
R = N - (U+D+L)

if U >= Sr:
    cnt = Sr
    for i in range(N):
        if s[i] == "U":
            cnt -= 1
        if cnt == 0:
            print("NO")
            exit()
        if t[i] == "D":
            cnt = min(cnt+1,H)

if D >= H-Sr+1:
    cnt = Sr
    for i in range(N):
        if s[i] == "D":
            cnt += 1
        if cnt == H+1:
            print("NO")
            exit()
        if t[i] == "U":
            cnt = max(cnt-1,1)

if L >= Sc:
    cnt = Sc
    for i in range(N):
        if s[i] == "L":
            cnt -= 1
        if cnt == 0:
            print("NO")
            exit()
        if t[i] == "R":
            cnt = min(cnt+1,W)

if R >= W-Sc+1:
    cnt = Sc
    for i in range(N):
        if s[i] == "R":
            cnt += 1
        if cnt == W+1:
            print("NO")
            exit()
        if t[i] == "L":
            cnt = max(cnt-1,1)

print("YES")