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



Sample input:
2 3 3
2 2
RRL
LUD


Sample output:
YES

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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])

# print(Scount)
# print(Tcount)

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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]

#ludr
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')

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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)):
        #print(min_p, max_p)
        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)
    #print(hs)
    #print(ht)
    #print(h_can)
    #print()
    #print(ws)
    #print(wt)
    #print(w_can)
    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))  # type: int
    W = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    s_r = int(next(tokens))  # type: int
    s_c = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    T = next(tokens)  # type: str
    solve(H, W, N, s_r, s_c, S, T)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
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:
        # print(ti)
        # print(ans[RevLRUD[T[i]]], SLRUD[RevLRUD[T[i]]], TLRUD[ti])
        TLRUD[ti] -= 1
    # print(SLRUD)
    # print(TLRUD)
if flag:
    print('NO')
else:
    print('YES')

Submission status: Accepted

Solution code:
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):#left
    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):#right
    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):#up
    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):#down
    if S[i] == "D":
        y += 1
    if y > H-1:
        ans = "NO"
        break
    if T[i] == "U":
        if y > 0 :
            y-= 1            
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def yes():
    print("YES")  # type: str


def no():
    print("NO")  # type: str


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):
        # 高橋名人のR,D作戦
        if S[i] == "R":
            cx += 1
        elif S[i] == "D":
            cy += 1

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

        # 青木名人のL,U作戦
        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):
        # 高橋名人のL,U作戦
        if S[i] == "L":
            cx -= 1
        elif S[i] == "U":
            cy -= 1

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

        # 青木名人のR, D作戦
        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))  # type: int
    W = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    s_r = int(next(tokens))  # type: int
    s_c = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    T = next(tokens)  # type: str
    solve(H, W, N, s_r, s_c, S, T)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


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

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

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

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

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

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

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

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

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

        # print(ri,ra,'c',ci,ca,'s',sr,sc)

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

    return 'YES'


print(main())

Submission status: Accepted

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

r,c = s_r,s_c



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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

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

s = input()
t = input()

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

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

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/agc033/tasks/agc033_b

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

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



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

    #print(l,r,u,d)

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

    #print(l,r,u,d)

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

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

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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

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

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

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

Submission status: Accepted

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

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

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

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

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

print("YES")

Submission status: Accepted

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

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


print('YES')

Submission status: Accepted

Solution code:
###############################################################################

from bisect import bisect_left as binl

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


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


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


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


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


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


###############################################################################


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

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

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

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

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

    print('YES')


if __name__ == '__main__':
    main()