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

Solution (Python):
def main():
    
    
    
    
    
    
    H, W, N = (int(i) for i in input().split())
    sr, sc = (int(i) for i in input().split())
    S = input()
    T = input()
    from collections import Counter
    S_C = Counter(S)
    if sc <= S_C['L']:
        move = 0
        for s, t in zip(S, T):
            if s == "L":
                move += 1
            if sc <= move:
                return print("NO")
            if t == "R":
                move -= 1
                if -move == W - sc + 1:
                    move += 1
    if W - sc + 1 <= S_C['R']:
        move = 0
        for s, t in zip(S, T):
            if s == "R":
                move += 1
            if W - sc + 1 <= move:
                return print("NO")
            if t == "L":
                move -= 1
                if -move == sc:
                    move += 1
    if sr <= S_C['U']:
        move = 0
        for s, t in zip(S, T):
            if s == "U":
                move += 1
            if sr <= move:
                return print("NO")
            if t == "D":
                move -= 1
                if -move == H - sr + 1:
                    move += 1
    if H - sr + 1 <= S_C['D']:
        move = 0
        for s, t in zip(S, T):
            if s == "D":
                move += 1
            if H - sr + 1 <= move:
                return print("NO")
            if t == "U":
                move -= 1
                if -move == sr:
                    move += 1
    print("YES")


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    H, W, N = (int(i) for i in input().split())
    sr, sc = (int(i) for i in input().split())
    S = input()
    T = input()
    from collections import Counter
    S_C = Counter(S)
    if sc <= S_C['L']:
        move = 0
        for s, t in zip(S, T):
            if s == "L":
                move += 1
            if sc <= move:
                return print("NO")
            if t == "R":
                move -= 1
                if -move == W - sc + 1:
                    move += 1
    if W - sc + 1 <= S_C['R']:
        move = 0
        for s, t in zip(S, T):
            if s == "R":
                move += 1
            if W - sc + 1 <= move:
                return print("NO")
            if t == "L":
                move -= 1
                if -move == sc:
                    move += 1
    if sr <= S_C['U']:
        move = 0
        for s, t in zip(S, T):
            if s == "U":
                move += 1
            if sr <= move:
                return print("NO")
            if t == "D":
                move -= 1
                if -move == H - sr + 1:
                    move += 1
    if H - sr + 1 <= S_C['D']:
        move = 0
        for s, t in zip(S, T):
            if s == "D":
                move += 1
            if H - sr + 1 <= move:
                return print("NO")
            if t == "U":
                move -= 1
                if -move == sr:
                    move += 1
    print("YES")


if __name__ == '__main__':
    main()

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

    
    left_limit = 0
    right_limit = w
    for i in range(n-1, -1, -1):
        if t[i] == 'L' and right_limit < w:
            right_limit += 1
        elif t[i] == 'R' and left_limit > 0:
            left_limit -= 1
        if s[i] == 'L':
            left_limit += 1
        elif s[i] == 'R':
            right_limit -= 1
        if left_limit == right_limit:
            print('NO')
            return

    if sc <= left_limit or sc > right_limit:
        print('NO')
        return

    
    up_limit = 0
    down_limit = h
    for i in range(n-1, -1, -1):
        if t[i] == 'U' and down_limit < h:
            down_limit += 1
        elif t[i] == 'D' and up_limit > 0:
            up_limit -= 1
        if s[i] == 'U':
            up_limit += 1
        elif s[i] == 'D':
            down_limit -= 1
        if up_limit == down_limit:
            print('NO')
            return

    if sr <= up_limit or sr > down_limit:
        print('NO')
        return

    print('YES')

if __name__ == '__main__':
    main()

Solution (Python):
H, W, N = map(int, input().split())
s_y, s_x = map(int, input().split())
S = str(input())
T = str(input())




lr = []
ud = []

for tup in zip(S, T):
    for n, i in enumerate(tup):
        if i == "L" or i == "R":
            lr.append((n, i))
        else:
            ud.append((n, i))

def f():
    crd = s_x
    
    
    
    for i, j in lr:
        if i == 0 and j == "L":
            crd -= 1
        if i == 1 and j == "R" and crd < W:
            crd += 1
        if crd == 0:
            return 0
    
    crd = s_x
    for i, j in lr:
        if i == 0 and j == "R":
            crd += 1
        if i == 1 and j == "L" and crd > 1:
            crd -= 1
        if crd == W + 1:
            return 0
    
    crd = s_y
    for i, j in ud:
        if i == 0 and j == "U":
            crd -= 1
        if i == 1 and j == "D" and crd < H:
            crd += 1
        if crd == 0:
            return 0
    
    crd = s_y
    for i, j in ud:
        if i == 0 and j == "D":
            crd += 1
        if i == 1 and j == "U" and crd > 1:
            crd -= 1
        if crd == H + 1:
            return 0
    return 1

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

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    H, W, N = map(int, input().split())
    s, r = map(int, input().split())
    S = input().strip("\n")
    T = input().strip("\n")
    Lb, Rb, Ub, Db = -1, W, -1, H
    for i in reversed(range(N)):
        if i != N - 1:
            if T[i] == "L": Rb = min(Rb + 1, W)
            elif T[i] == "R": Lb = max(Lb - 1, -1)
            elif T[i] == "U": Db = min(Db + 1, H)
            elif T[i] == "D": Ub = max(Ub - 1, 0)

        if S[i] == "L": Lb = Lb + 1
        elif S[i] == "R": Rb = Rb - 1
        elif S[i] == "U": Ub = Ub + 1
        elif S[i] == "D": Db = Db - 1

        if Lb >= W - 1 or Rb <= 0 or Ub >= H - 1 or Db <= 0:
            print("NO")
            break
    else:
        if Lb < r - 1 < Rb and Ub < s - 1 < Db: print("YES")
        else: print("NO")
    
    

    return 0

if __name__ == "__main__":
    solve()

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

ans = 'YES'

x = sc - 1
for i in range(N):
    if S[i] == 'L': x -= 1
    
    if x < 0: ans = 'NO'
    if T[i] == 'R': x = min(x + 1, W - 1)

x = sc - 1
for i in range(N):
    if S[i] == 'R': x += 1
    
    if x >= W: ans = 'NO'
    if T[i] == 'L': x = max(x - 1, 0) 
    
y = sr - 1
for i in range(N):
    if S[i] == 'U': y -= 1
    
    if y < 0: ans = 'NO'
    if T[i] == 'D': y = min(y + 1, H - 1)
    
y = sr - 1
for i in range(N):
    if S[i] == 'D': y += 1
    
    if y >= H: ans = 'NO'
    if T[i] == 'U': y = max(y - 1, 0)

print(ans)

Solution (Python):
h, w, n = map(int, input().split())
src = list(map(int,input().split()))

s = input()
t = input()
s_c, t_c = [], []
for i in ["U", "D", "L", "R"]:
    s_c.append(s.count(i))
    t_c.append(t.count(i))
if (src[0] <= s_c[0]-t_c[1]) or (h-src[0]+1<=s_c[1]-t_c[0] )or (src[1]<=s_c[2]-t_c[3] )or (w-src[1]+1<=s_c[3]-s_c[2]):
    result = "NO"
else:
    result = "YES"
    for i in range(n):
        if s[i]=="U" and src[0]<=h-src[0]+1:
            src[0] -= 1
        elif s[i]=="D" and h-src[0]+1<=src[0]:
            src[0] += 1
        elif s[i]=="L" and src[1]<=w-src[1]+1:
            src[1] -= 1
        elif s[i]=="R" and w-src[1]+1<=src[1]:
            src[1] += 1
        if src[0]<1 or src[0]>h or src[1]<1 or src[1]>w:
            result = "NO"
            break

        if t[i]=="U" and src[0]>h-src[0]+1:
            src[0] -= 1
        elif t[i]=="D" and h-src[0]+1>src[0]:
            src[0] += 1
        elif t[i]=="L" and src[1]>w-src[1]+1:
            src[1] -= 1
        elif t[i]=="R" and w-src[1]+1>src[1]:
            src[1] += 1

print(result)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10


def solve():
    h, w, n = LI()
    sr, sc = LI()
    s = S()[::-1]
    t = S()[::-1]
    
    plus = 0
    minus = 1

    def f(l, start, end):
        tmp = [1, end]
        for i in range(n):
            if t[i] == l[plus]:
                tmp[0] -= 1
                tmp[0] = max(tmp[0], 1)
            elif t[i] == l[minus]:
                tmp[1] += 1
                tmp[1] = min(end, tmp[1])
            if s[i] == l[plus]:
                tmp[1] -= 1
            elif s[i] == l[minus]:
                tmp[0] += 1
            if tmp[1] < tmp[0]:
                return False
        return tmp[0] <= start <= tmp[1]
    
    print("YES" if f(["R", "L"], sc, w) and f(["D", "U"], sr, h) else "NO")
    return



if __name__ == '__main__':
    solve()

Solution (Python):
h,w,n = map(int,input().split())
sr,sc = map(int,input().split())
S = input()
T = input()
hor = [1,w]
ver = [1,h]
flag1 = True
flag2 = True
for i in range(n):
    k = T[n-i-1]
    if (hor[0] == 1 and hor[1] == w):
        None
    else:
        if k == 'L' and hor[1] != w:
            hor[1] += 1
        elif k == 'R' and hor[0] != 1:
            hor[0] -= 1
    k = S[n-i-1]
    if (hor[0] == w and k == 'R') or (hor[1] == 1 and k == 'L'):
        flag1 = False
        break
    else:
        if k == 'L':
            hor[0] += 1
        elif k == 'R':
            hor[1] -= 1
    if hor[0] > hor[1]:
        flag1 = False
        break
for i in range(n):
    k = T[n-i-1]
    if (ver[0] == 1 and ver[1] == h):
        None
    else:
        if k == 'U' and ver[1] != h:
            ver[1] += 1
        elif k == 'D' and ver[0] != 1:
            ver[0] -= 1
    k = S[n-i-1]
    if (ver[0] == h and k == 'D') or (ver[1] == 1 and k == 'U'):
        flag2 = False
        break
    else:
        if k == 'U':
            ver[0] += 1
        elif k == 'D':
            ver[1] -= 1
    if ver[0] > ver[1]:
        flag2 = False
        break
if flag1 == True and flag2 == True:
    if hor[0] <= sc <= hor[1]:
        if ver[0] <= sr <= ver[1]:
            print('YES')
        else:
            print('NO')
    else:
        print('NO')
else:
    print('NO')

Solution (Python):



def main():
  L, R, U, D = 0, 1, 2, 3
  h, w, n = map(int, input().split())
  sr, sc = map(int, input().split())
  sr, sc = sr-1, sc-1
  ls = input().strip()[::-1]
  lt = input().strip()[::-1]
  r = [0, w-1, 0, h-1]
  
  def f(d):
    if d=='L':
      r[L] = (min(max(-1, r[L]+1), w))
    if d=='R':
      r[R] = (min(max(-1, r[R]-1), w))
    if d=='U':
      r[U] = (min(max(-1, r[U]+1), h))
    if d=='D':
      r[D] = (min(max(-1, r[D]-1), h))
      
  def g(d):
    if d=='L':
      r[R] = (min(max(0, r[R]+1), w-1))
    if d=='R':
      r[L] = (min(max(0, r[L]-1), w-1))
    if d=='U':
      r[D] = (min(max(0, r[D]+1), h-1))
    if d=='D':
      r[U] = (min(max(0, r[U]-1), h-1))
      
  def check():
    if r[L] > r[R] or r[L] < 0 or r[R] > w-1:
      return True
    if r[U] > r[D] or r[U] < 0 or r[D] > h-1:
      return True
    return False
  
  for s, t in zip(ls, lt):
    g(t)
    f(s)
    if check():
      print('NO')
      return
  if r[L] <= sc <= r[R] and r[U] <= sr <= r[D]:
    print('YES')
  else:
    print('NO')
    
if __name__=='__main__':
  main()

Solution (Python):
def main():
    H, W, N = map(int, input().split())
    sr, sc = map(int, input().split())
    S = input()
    T = input()
    
    t = sc
    for i in range(N):
        if S[i] == 'R':
            t += 1
        if t > W:
            return True
        if T[i] == 'L' and t > 1:
            t -= 1
    
    t = sc
    for i in range(N):
        if S[i] == 'L':
            t -= 1
        if t < 1:
            return True
        if T[i] == 'R' and t < W:
            t += 1
    
    t = sr
    for i in range(N):
        if S[i] == 'U':
            t -= 1
        if t < 1:
            return True
        if T[i] == 'D' and t < H:
            t += 1
    
    t = sr
    for i in range(N):
        if S[i] == 'D':
            t += 1
        if t > H:
            return True
        if T[i] == 'U' and t > 1:
            t -= 1
    return False
if main():
    print('NO')
else:
    print('YES')

Solution (Python):
def main():
    H, W, N = list(map(int, input().split()))
    sr, sc = list(map(int, input().split()))
    sr, sc = sr - 1, sc - 1
    S = input()
    T = input()
    K = [sc, W - sc - 1, sr, H - sr - 1]
    M = [W - 1, W - 1, H - 1, H - 1]
    Is = {'L': 0, 'R': 1, 'U': 2, 'D': 3}
    It = {'L': 1, 'R': 0, 'U': 3, 'D': 2}
    for s, t in zip(S, T):
        si, ti = Is[s], It[t]
        sti = It[s]
        K[si] -= 1
        if K[si] < 0:
            return False
        if K[sti] == M[sti]:
            K[sti] -= 1
            if K[sti] < 0:
                return False
        K[ti] += 1
        if K[ti] > M[ti]:
            K[ti] = M[ti]
    return True

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

Solution (Python):

import numpy as np
from functools import *

import sys

sys.setrecursionlimit(100000)
input = sys.stdin.readline


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


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


H, W, NN = acinput()

initp = np.array(acinput())

S = list(input())[:-1]
T = list(input())[:-1]




def position_relative(direction, direction_op, cs, flg, seq1, seq2, N):
    global H, W
    
    for i in range(len(seq1)):
        s1 = seq1[i]
        s2 = seq2[i]

        
        if s1 == direction:
            cs += flg

        if flg > 0:
            if cs > N:
                return False

        else:
            if cs <= 0:
                return False
            
            
        
        if s2 == direction_op:
            
            cs -= flg
    
    
        if cs > N:
           cs = N
        
        if cs <= 0:
            cs = 1

    return cs


sr = position_relative("R", "L", initp[1], 1, S, T, W)
sl = position_relative("L", "R", initp[1], -1, S, T, W)
su = position_relative("U", "D", initp[0], -1, S, T, H)
sd = position_relative("D", "U", initp[0], 1, S, T, H)





if sr*sl*su*sd == 0:
    print("NO")
else:
    print("YES")

Solution (Python):
h,w,n = map(int, input().split())
sr,sc = map(int, input().split())
s = input()
t = input()
r=sr-1
for i in range(len(s)):
    if s[i] =="U":
        r-=1
    if r<0:
        print("NO")
        exit()
    if t[i] == "D" and r < h-1:
        r+=1
r=sr-1
for i in range(len(s)):
    if s[i] =="D":
        r+=1
    if h<=r:
        print("NO")
        exit()
    if t[i] == "U" and 0<r:
        r-=1
c=sc-1
for i in range(len(s)):
    if s[i] =="L":
        c-=1
    if c<0:
        print("NO")
        exit()
    if t[i] == "R" and c<w-1:
        c+=1
c=sc-1
for i in range(len(s)):
    if s[i] =="R":
        c+=1
    if c>=w:
        print("NO")
        exit()
    if t[i] == "L" and 0 < c:
        c-=1
print("YES")

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

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

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

    if S[i] == 'L':
        l += 1
    if S[i] == 'R':
        r -= 1
        
    if l > r:
        print('NO')
        exit()

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

    if S[i] == 'U':
        u += 1
    if S[i] == 'D':
        d -= 1

    if u > d:
        print('NO')
        exit()

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

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

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

for i in range(N):
    if T[i] == 'L':
        r = min(W,r+1)
    elif T[i] == 'R':
        l = max(1,l-1)
    elif T[i] == 'U':
        d = min(H,d+1)
    else:
        u = max(1,u-1)
    if S[i] == 'L':
        l = l+1
    elif S[i] == 'R':
        r = r-1
    elif S[i] == 'U':
        u = u+1
    else:
        d = d-1
    if r < l or d < u:
        print('NO')
        break
else:
    if l <= sc <= r and u <= sr <= d:
        print('YES')
    else:
        print('NO')

Solution (Python):
import sys
def main():
    input = sys.stdin.readline
    H,W,N = map(int, input().split())
    start = tuple(map(int, input().split()))
    S = input().rstrip()
    T = input().rstrip()
    D = {
        'R': (0,1),
        'L': (0,-1),
        'U': (-1,0),
        'D': (1,0)
    }
    Dc = ['R', 'L', 'U', 'D']

    def move(current, direction):
        d = D[direction]
        return (current[0]+d[0], current[1]+d[1])

    def is_dropped(a):
        return not (1<=a[0]<=H and 1<=a[1]<=W)

    ans = True
    for i in range(4):
        cur = start
        for j in range(N):
            if S[j] == Dc[i]:
                cur = move(cur, Dc[i])
                if is_dropped(cur): ans = False
            if T[j] == Dc[i^1]:
                cur = move(cur, Dc[i^1])
                if is_dropped(cur): cur = move(cur, Dc[i])
    print('YES' if ans else 'NO')

if __name__ == '__main__':
    main()

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



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(1, l - 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 l <= sc <= r and u <= sr <= d:
    print('YES')
else:
    print('NO')

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



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

    if s in ('L', 'R'):
        if s == 'L':
            l += 1
        else:
            r -= 1
    else:
        if s == 'U':
            u += 1
        else:
            d -= 1
    if l > r or u > d:
        print('NO')
        exit()

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

Solution (Python):
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
S = list(input())
T = list(input())
u, d, l, r = 1, h, 1, w
while S:
  aoki = T.pop()
  if aoki == "U":
    if d<h: d += 1
  elif aoki == "D":
    if u>1: u -= 1
  elif aoki == "L":
    if r<w: r += 1
  else:
    if l>1: l -= 1
  takahashi = S.pop()
  if takahashi == "U":
    if u==d:
      print("NO")
      exit()
    u += 1
  elif takahashi == "D":
    if u==d:
      print("NO")
      exit()
    d -= 1
  elif takahashi == "L":
    if l==r:
      print("NO")
      exit()
    l += 1
  else:
    if l==r:
      print("NO")
      exit()
    r -= 1
if u<=sr<=d and l<=sc<=r:
  print("YES")
else:
  print("NO")

Solution (Python):
H,W,N=map(int, input().split())
sr,sc=map(int, input().split())
S=input()
T=input()
for (x,y),(s,t) in zip(["LR","RL","UD","DU"],[(W-sc,W),(sc-1,W),(H-sr,H),(sr-1,H)]):
    C=[s]*(N+1)
    for i in range(N):
        C[i]+=S[i]==x
        C[i+1]=max(0,C[i]-(T[i]==y))
    if max(C)>=t:
        print("NO")
        break
else:
    print("YES")

Solution (Python):
import sys


def main():
    H,W,N = map(int,input().split())
    sr,sc = map(int,input().split())
    S = input()
    T = input()
    xp,xd = sr,sr
    yp,yd = sc,sc
    
    for i in range(N):
        if S[i] == "D":
            xp += 1
            if xp == H+1:
                print("NO")
                exit()
        elif S[i] == "U":
            xd -= 1
            if xd == 0:
                print("NO")
                exit()
        elif S[i] == "R":
            yp += 1
            if yp == W+1:
                print("NO")
                exit()
        else:
            yd -= 1
            if yd == 0:
                print("NO")
                exit()
        if T[i] == "D":
            if xd != H:
                xd += 1
        elif T[i] == "U":
            if xp != 1:
                xp -= 1
        elif T[i] == "R":
            if yd != W:
                yd += 1
        else:
            if yp != 1:
                yp -= 1
            
    print("YES")

if __name__ == "__main__":
    main()

Solution (Python):
H, W, N, sr, sc, S, T = open(0).read().split()
H, W, N, sr, sc = map(int, (H,W,N,sr,sc))
dpr = [0]*(N+1)
dpl = [0]*(N+1)
dpu = [0]*(N+1)
dpd = [0]*(N+1)
dpr[0] = W-sc
dpl[0] = sc-1
dpu[0] = sr-1
dpd[0] = H-sr
for i in range(N):
  if S[i]=='R':
    dpr[i] -= 1
    if 0>dpr[i]:
      print('NO')
      break
  elif S[i]=='D':
    dpd[i] -= 1
    if 0>dpd[i]:
      print('NO')
      break
  elif S[i]=='L':
    dpl[i] -= 1
    if 0>dpl[i]:
      print('NO')
      break
  else:
    dpu[i] -= 1
    if 0>dpu[i]:
      print('NO')
      break
  if T[i]=='R':
    dpl[i+1] = min(W-1,dpl[i]+1)
    dpr[i+1] = dpr[i]
    dpd[i+1] = dpd[i]
    dpu[i+1] = dpu[i]
  elif T[i]=='L':
    dpr[i+1] = min(W-1,dpr[i]+1)
    dpl[i+1] = dpl[i]
    dpu[i+1] = dpu[i]
    dpd[i+1] = dpd[i]
  elif T[i]=='U':
    dpd[i+1] = min(H-1,dpd[i]+1)
    dpl[i+1]= dpl[i]
    dpu[i+1] = dpu[i]
    dpr[i+1] = dpr[i]
  else:
    dpu[i+1] = min(H-1,dpu[i]+1)
    dpl[i+1] = dpl[i]
    dpr[i+1] = dpr[i]
    dpd[i+1] = dpd[i]
else:
  print('YES')

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

L = [-1]*N
R = [W]*N
U = [-1]*N
D = [H]*N

if S[-1]=="L":
  L[-1] = 0
if S[-1]=="R":
  R[-1] = W-1
if S[-1]=="U":
  U[-1] = 0
if S[-1]=="D":
  D[-1] = H-1

ans = "YES"

for i in range(N-1)[::-1]:
  L[i] = L[i+1]
  R[i] = R[i+1]
  U[i] = U[i+1]
  D[i] = D[i+1]
  if S[i]=="L" and (T[i]!="R" or L[i]<0):
    L[i] += 1
  if S[i]!="L" and T[i]=="R":
    L[i] = max(L[i]-1, -1)
  if S[i]=="R" and (T[i]!="L" or R[i]>=W):
    R[i] -= 1
  if S[i]!="R" and T[i]=="L":
    R[i] = min(R[i]+1, W)
  if S[i]=="U" and (T[i]!="D" or U[i]<0):
    U[i] += 1
  if S[i]!="U" and T[i]=="D":
    U[i] = max(U[i]-1, -1)
  if S[i]=="D" and (T[i]!="U" or D[i]>=H):
    D[i] -= 1
  if S[i]!="D" and T[i]=="U":
    D[i] = min(D[i]+1, H)
  if L[i]+1>=R[i] or U[i]+1>=D[i]:
    ans = "NO"

if sr<=U[0] or D[0]<=sr or sc<=L[0] or R[0]<=sc:
  ans = "NO"
print ans

Solution (Python):

"""
後ろから考えていく

サンプル1のLRで考える

後手が勝つためには2ターン目終了以前に [2,3]にいる必要
→1ターン終了時に[2,2]にいること
→1ターン目初手終了時に[2,3]にいること
→最初に[2,2]にいること

→後手Lがくると右の範囲が1増える
→先手Lがくると左の範囲が1減る

最後に最初の地点が入ってれば後手が勝ち

"""
import sys

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

s,c = map(int,input().split())

S = input()
T = input()

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

for i in range(N):

    i = N - i - 1

    if T[i] == "U":
        d = min(d+1 , H)

    elif T[i] == "D":
        u = max(1 , u-1)

    elif T[i] == "L":
        r = min(r+1 , W)

    else:
        l = max(l-1 , 1)




    if S[i] == "U":
        u += 1

    elif S[i] == "D":
        d -= 1

    elif S[i] == "L":
        l += 1

    else:
        r -= 1


    if l > r or u > d or l > W or r < 1 or u > H or d < 1: 
        print ("NO")
        sys.exit()



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

Solution (Python):
h,w,n = map(int,input().split())
sx,sy = map(int,input().split())
S = input()
T = input()
S = list(S[::-1])
T = list(T[::-1])
area = [1,h,1,w]

for s,t in zip(S,T):
  if t == "R":
    area[2] = max(1,area[2]-1)
  if t == "L":
    area[3] = min(w,area[3]+1)
  if t == "D":
    area[0] = max(1,area[0]-1)
  if t == "U":
    area[1] = min(h,area[1]+1)
  if s == "L":
    area[2] = min(w+1,area[2]+1)
  if s == "R":
    area[3] = max(0,area[3]-1)
  if s == "U":
    area[0] = min(h+1,area[0]+1)
  if s == "D":
    area[1] = max(0,area[1]-1)
  if area[0] > area[1] or area[2] > area[3]:
    print("NO")
    exit()
if area[0] <= sx <= area[1] and area[2] <= sy <= area[3]:
  print("YES")
else:
  print("NO")

Solution (Python):
from collections import defaultdict

H,W,N = map(int, input().split())
sy, sx = map(int, input().split())
T = input()
A = input()

td = defaultdict(int)
ad = defaultdict(int)

for i in range(N):
    td[T[i]] += 1

    
    if sx + max(0, td["R"] - ad["L"]) > W \
    or sx - max(0, td["L"] - ad["R"]) < 1 \
    or sy + max(0, td["D"] - ad["U"]) > H \
    or sy - max(0, td["U"] - ad["D"]) < 1:
        print("NO")
        exit()

    if A[i] == "L":
        ad[A[i]] = min(ad[A[i]] + 1, sx - 1 + td["R"])
    elif A[i] == "R":
        ad[A[i]] = min(ad[A[i]] + 1, W - sx  + td["L"])
    elif A[i] == "U":
        ad[A[i]] = min(ad[A[i]] + 1, sy - 1 + td["D"])
    if A[i] == "D":
        ad[A[i]] = min(ad[A[i]] + 1, H - sy + td["U"])

print("YES")

Solution (Python):

from bisect import *
from collections import *
from heapq import *

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


st=0
end=W-1
sttate=0
endtate=H-1
for i in range(N-1,-1,-1):
    if T[i]=="L":
        if end<W-1:
            end+=1
    if T[i]=="R":
        if st>0:
            st-=1
    if T[i]=="U":
        if endtate<H-1:
            endtate+=1
    if T[i]=="D":
        if sttate>0:
            sttate-=1

    if S[i]=="L":
        st+=1
    if S[i]=="R":
        end-=1
    if S[i]=="U":
        sttate+=1
    if S[i]=="D":
        endtate-=1
    if st>end or sttate>endtate:
        print("NO")
        exit()

if st<=sc-1<=end and sttate<=sr-1<=endtate:
    print("YES")
else:
    print("NO")

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

l, r = 1, W
d, u = 1, H
for s, t in zip(reversed(S), reversed(T)):

    l = max(l - (t == "R"), 1) + (s == "L")
    r = min(r + (t == "L"), W) - (s == "R")

    d = max(d - (t == "U"), 1) + (s == "D")
    u = min(u + (t == "D"), H) - (s == "U")

    if l > r or d > u:
        break

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

Solution (Python):
from sys import stdout
printn = lambda x: stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True 
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

h,w,n = inm()
srow,scol = inm()
s = input().strip()
t = input().strip()
t = t[:-1] + 'M'
l = r = u = d = 0
for i in range(n-1,-1,-1):
    if s[i]=='D' and (t[i-1]!='U' or d==h-1):
        d += 1
    elif s[i]!='D' and t[i-1]=='U' and d>0:
        d -= 1

    if s[i]=='U' and (t[i-1]!='D' or u==h-1):
        u += 1
    elif s[i]!='U' and t[i-1]=='D' and u>0:
        u -= 1

    if s[i]=='L' and (t[i-1]!='R' or l==w-1):
        l += 1
    elif s[i]!='L' and t[i-1]=='R' and l>0:
        l -= 1

    if s[i]=='R' and (t[i-1]!='L' or r==w-1):
        r += 1
    elif s[i]!='R' and t[i-1]=='L' and r>0:
        r -= 1

    if l+r>=w or u+d>=h:
        print('NO')
        exit()

if 1+l<=scol<=w-r and 1+u<=srow<=h-d:
    print('YES')
else:
    print('NO')

Solution (Python):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations
from itertools import accumulate
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
mod=10**9+7

h,w,n=map(int,input().split())
src=list(map(int,input().split()))
s=input()
t=input()
lrud=["L","U","D","R"]
dic={"L":[0,-1],"R":[0,1],"U":[-1,0],"D":[1,0]}
for i in range(4):
    tmp=src[:]
    
    for j in range(n):
        if s[j]==lrud[i]:
            tmp[0]+=dic[s[j]][0]
            tmp[1]+=dic[s[j]][1]
        if lrud[i]=="L" and tmp[1]==0:
            print("NO")
            quit()
        if lrud[i]=="R" and tmp[1]==w+1:
            print("NO")
            quit()
        if lrud[i]=="U" and tmp[0]==0:
            print("NO")
            quit()
        if lrud[i]=="D" and tmp[0]==h+1:
            print("NO")
            quit()
        
        if t[j]==lrud[-1-i]:
            if 0<tmp[0]+dic[t[j]][0]<=h:
                tmp[0]+=dic[t[j]][0]
            if 0<tmp[1]+dic[t[j]][1]<=w:
                tmp[1]+=dic[t[j]][1]
else:
    print("YES")