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: Wrong Answer

Solution code:
#!/usr/bin/env python3
#AGC33 B

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w,n = LI()
sr,sc = LI()
sr -= 1
sc -= 1
w -= 1
h -= 1
s = input()
t = input()
left,right = 0,w
for i in range(n)[::-1]:
    if t[i] == 'R':
        if left > 0:
            left -= 1
    elif t[i] == 'L':
        if right < w:
            right += 1
    if s[i] == 'R':
        right -= 1
    elif s[i] == 'L':
        left += 1
    if right < 0 or left > w:
        print('NO')
        quit()
down,up = 0,h
for i in range(n)[::-1]:
    if t[i] == 'U':
        if down > 0:
            down -= 1
    elif t[i] == 'D':
        if up < h:
            up += 1
    if s[i] == 'U':
        up -= 1
    elif s[i] == 'D':
        down += 1
    if up < 0 or down > h:
        print('NO')
        quit()
if left <= sc <= right and down <= sr <= up:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#AGC33 B

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w,n = LI()
sr,sc = LI()
sr -= 1
sc -= 1
w -= 1
h -= 1
s = input()
t = input()
left,right = 0,w
for i in range(n)[::-1]:
    if t[i] == 'R':
        if left > 0:
            left -= 1
    elif t[i] == 'L':
        if right < w:
            right += 1
    if s[i] == 'R':
        right -= 1
    elif s[i] == 'L':
        left += 1
    if right < left:
        print('NO')
        quit()
    #print(i,'left',left,'right',right)
down,up = 0,h
for i in range(n)[::-1]:
    if t[i] == 'U':
        if down > 0:
            down -= 1
    elif t[i] == 'D':
        if up < h:
            up += 1
    if s[i] == 'U':
        up -= 1
    elif s[i] == 'D':
        down += 1
    if up < down:
        print('NO')
        quit()
    #print(i,'up',up,'down',down)
#print(left,right,down,up)
if left <= sc <= right and down <= sr <= up:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#AGC33 B

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w,n = LI()
sr,sc = LI()
sr -= 1
sc -= 1
s = input()
t = input()
left,right = 0,w
for i in range(n)[::-1]:
    if t[i] == 'R':
        if left > 0:
            left -= 1
    elif t[i] == 'L':
        if right < w:
            right += 1
    if s[i] == 'R':
        right -= 1
        if right <= 0:
            print('NO')
            quit()
    elif s[i] == 'L':
        left += 1
        if left >= w:
            print('NO')
            quit()
down,up = 0,h
for i in range(n)[::-1]:
    if t[i] == 'U':
        if down > 0:
            down -= 1
    elif t[i] == 'D':
        if up < h:
            up += 1
    if s[i] == 'U':
        up -= 1
        if up <= 0:
            print('NO')
            quit()
    elif s[i] == 'D':
        down += 1
        if down >= h:
            print('NO')
            quit()
#print(left,right,down,up)
if left <= sc <= right and down <= sr <= up:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

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

def solve():
    Ls,Rs,Us,Ds=0,0,0,0
    Lt,Rt,Ut,Dt=0,0,0,0
    for c in S:
        if c == 'L':Ls+=1
        if c == 'R':Rs+=1
        if c == 'U':Us+=1
        if c == 'D':Ds+=1
    for c in T:
        if c == 'L':Lt+=1
        if c == 'R':Rt+=1
        if c == 'U':Ut+=1
        if c == 'D':Dt+=1
  
    print("NO" if Sc-Ls+Rt <= 0 or
                  Sc+Rs-Lt > W or
                  Sr-Us+Dt <=0 or
                  Sr+Ds-Ut > H else "YES")
solve()

Submission status: Wrong Answer

Solution code:
#B問題
H,W,N = map(int,input().split())
y,x = map(int,input().split())
S = list(str(input()))
T = list(str(input()))

flag = 0
#L
sy,sx = y,x
for i in range(N):
    if S[i] == "L":
        sx-=1
        if sx == 0:
            flag = 1
            break
    if T[i] == "R":
        if sx <= W-1:
            sx+=1
    
#R
sy,sx = y,x
for i in range(N):
    if S[i] == "R":
        sx+=1
        if sx == W+1:
            flag = 1
            break
    if T[i] == "L":
        if sx >= 2:
            sx-=1

#U
sy,sx = y,x
for i in range(N):
    if S[i] == "U":
        sy+=1
        if sy == H+1:
            flag = 1
            break
    if T[i] == "D":
        if sy >= 2:
            sy-=1

#D
sy,sx = y,x
for i in range(N):
    if S[i] == "D":
        sy-=1
        if sy == 0:
            flag = 1
            break
    if T[i] == "U":
        if sy <= H-1:
            sy+=1

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

left = 1
right = W
bottom = 1
top = H

if S[-1] == 'L':
    left += 1
elif S[-1] == 'R':
    right -= 1
elif S[-1] == 'U':
    bottom += 1
elif S[-1] == 'D':
    top -= 1

for i in range(N - 1)[:: -1]:
    if T[i] == 'L':
        right = min(W, right + 1)
    elif T[i] == 'R':
        left = max(1, left - 1)
    elif T[i] == 'U':
        bottom = min(H, top + 1)
    elif T[i] == 'D':
        top = max(1, bottom - 1)

    if S[i] == 'L':
        left += 1
    elif S[i] == 'R':
        right -= 1
    elif S[i] == 'U':
        bottom += 1
    elif S[i] == 'D':
        top -= 1

    if left > right or bottom > top:
        print('NO')
        break
else:
    if left <= sc <= right and bottom <= sr <= top:
        print('YES')
    else:
        print('NO')

Submission status: Wrong Answer

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

left = 1
right = W
bottom = 1
top = H

if S[-1] == 'L':
    left += 1
elif S[-1] == 'R':
    right -= 1
elif S[-1] == 'U':
    bottom += 1
elif S[-1] == 'D':
    top -= 1

for i in range(N - 1)[:: -1]:
    if T[i] == 'L':
        right = min(W, right + 1)
    elif T[i] == 'R':
        left = max(1, left - 1)
    elif T[i] == 'U':
        bottom = min(H, bottom + 1)
    elif T[i] == 'D':
        top = max(1, top - 1)

    if S[i] == 'L':
        left += 1
    elif S[i] == 'R':
        right -= 1
    elif S[i] == 'U':
        bottom += 1
    elif S[i] == 'D':
        top -= 1

    if left > right or bottom > top:
        print('NO')
        break
else:
    if left <= sc <= right and bottom <= sr <= top:
        print('YES')
    else:
        print('NO')

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

left = 1
right = w
bot = 1
top = h

# 最終回後手は何もしない
if s[-1] == "L":
    left += 1
elif s[-1] == "R":
    right -= 1
elif s[-1] == "U":
    top -= 1
elif s[-1] == "D":
    bot += 1

for i in range(n-2, -1, -1):
    # 後手
    # 区間を広げる事ができる
    if t[i] == "L":
        right = min(w, right+1)
    elif t[i] == "R":
        left = max(1, left-1)
    elif t[i] == "U":
        bot = max(1, bot-1)
    elif t[i] == "D":
        top = min(h, top+1)
    # 先手
    # 区間を狭める
    if s[i] == "L":
        left += 1
    elif s[i] == "R":
        right -= 1
    elif s[i] == "U":
        top -= 1
    elif s[i] == "D":
        bot += 1
    if left > right or bot > top:
        print("NO")
        exit()
#print(left, right)
#print(bot, top)
if left <= sc <= right and bot <= sr <=top:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

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

left = 1
right = w
bot = 1
top = h

# 最終回後手は何もしない
if s[-1] == "L":
    left += 1
elif s[-1] == "R":
    right -= 1
elif s[-1] == "U":
    top -= 1
elif s[-1] == "D":
    bot += 1

for i in range(n-2, -1, -1):
    # 後手
    # 区間を広げる事ができる
    if t[i] == "L":
        right = min(w, right+1)
    elif t[i] == "R":
        left = max(0, left-1)
    elif t[i] == "U":
        bot = max(0, bot-1)
    elif t[i] == "D":
        top = min(h, top+1)
    # 先手
    # 区間を狭める
    if s[i] == "L":
        left += 1
    elif s[i] == "R":
        right -= 1
    elif s[i] == "U":
        top -= 1
    elif s[i] == "D":
        bot += 1
    if left >= right or bot >= top:
        print("NO")
        exit()
#print(left, right)
#print(bot, top)
if left <= sc <= right and bot <= sr <=top:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

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

left = 1
right = w
bot = 1
top = h

# 初回は後手は何もしない
if s[-1] == "L":
    left += 1
elif s[-1] == "R":
    right -= 1
elif s[-1] == "U":
    top -= 1
elif s[-1] == "D":
    bot += 1
if left > right or bot > top:
    print("NO")
    exit()

for i in range(n-2, -1, -1):
    # 後手
    # 区間を広げる事ができる
    if t[i] == "L":
        right = min(w, right+1)
    elif t[i] == "R":
        left = max(0, left-1)
    elif t[i] == "U":
        bot = max(0, bot-1)
    elif t[i] == "D":
        top = min(h, top+1)
    # 先手
    # 区間を狭める
    if s[i] == "L":
        left += 1
    elif s[i] == "R":
        right -= 1
    elif s[i] == "U":
        top -= 1
    elif s[i] == "D":
        bot += 1
    if left > right or bot > top:
        print("NO")
        exit()
#print(left, right)
#print(bot, top)
if left <= sc <= right and bot <=sr<=top:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

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

left = 1
right = w
bot = 1
top = h

# 初回は後手は何もしない
if s[-1] == "L":
    left += 1
elif s[-1] == "R":
    right -= 1
elif s[-1] == "U":
    top -= 1
elif s[-1] == "D":
    bot += 1

for i in range(n-2, -1, -1):
    # 後手
    # 区間を広げる事ができる
    if t[i] == "L":
        right = min(w, right+1)
    elif t[i] == "R":
        left = max(0, left-1)
    elif t[i] == "U":
        bot = max(0, bot-1)
    elif t[i] == "D":
        top = min(h, top+1)
    # 先手
    # 区間を狭める
    if s[i] == "L":
        left += 1
    elif s[i] == "R":
        right -= 1
    elif s[i] == "U":
        top -= 1
    elif s[i] == "D":
        bot += 1
    if left > right or bot > top:
        print("NO")
        exit()
#print(left, right)
#print(bot, top)
if left <= sc <= right and bot <=sr<=top:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

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

left = 1
right = w
bot = 1
top = h

# 初回は後手は何もしない
if s[-1] == "L":
    left += 1
elif s[-1] == "R":
    right -= 1
elif s[-1] == "U":
    top -= 1
elif s[-1] == "D":
    bot += 1

for i in range(n-2, -1, -1):
    # 後手
    # 区間を広げる事ができる
    if t[i] == "L":
        right = min(w, right+1)
    elif t[i] == "R":
        left = max(0, left-1)
    elif t[i] == "U":
        bot = max(0, bot-1)
    elif t[i] == "D":
        top = min(h, top+1)
    # 先手
    # 区間を狭める
    if s[i] == "L":
        left += 1
    elif s[i] == "R":
        right -= 1
    elif s[i] == "U":
        top -= 1
    elif s[i] == "D":
        bot += 1
    if left > right or bot > top:
        print("NO")
        exit()
#print(left, right)
#print(bot, top)
if left <= sc <= right and bot <=sr<=top:
    print("YES")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
A = {'L': 0, 'R': 1, 'U': 2, 'D': 3}
D = {'L': 1, 'R': 0, 'U': 3, 'D': 2}
L = [0, 0, 0, 0]
B = [sc, W + 1 - sc, H + 1 - sr, sr]
for i in range(N - 1, -1, -1):
  L[D[T[i]]] = max(L[D[T[i]]] - 1, 0)
  L[A[S[i]]] += 1
  if L[A[S[i]]] >= B[A[S[i]]]:
    print('NO')
    break
else:
  print('YES')

Submission status: Wrong Answer

Solution code:
h,w,n = map(int,input().split())
sr,sc = map(int,input().split())
t = input()
s = input()
hei = [1,h]
wid = [1,w]
for i in range(n-1,-1,-1):
  if s[i] == "L":
    wid[1] = min(wid[1]+1,w)
  elif s[i] == "R":
    wid[0] = max(wid[0]-1,0)
  elif s[i] == "U":
    hei[1] = min(hei[1]+1,h)
  elif s[i] == "D":
    hei[0] = max(hei[0]-1,0)
  if t[i] == "L":
    wid[0] += 1
  elif t[i] == "R":
    wid[1] -= 1
  elif t[i] == "U":
    hei[0] += 1
  elif t[i] == "D":
    hei[1] -= 1
  if wid[0]==wid[1] or hei[0]==hei[1]:
    print("NO")
    exit(0)
if hei[0] <= sc <= hei[1] and wid[0] <= sr <= wid[1]:
  print("YES")
else:
  print("NO")

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

# ここに居たら後手の価値
L,R = 1,W
D,U = 1,H
for s,t in zip(S[::-1],T[::-1]):
    if t == 'L': R += 1
    elif t == 'D': U += 1
    elif t == 'R': L -= 1
    elif t == 'U': D -= 1
    
    if L == 0: L = 1
    if D == 0: D = 1
    if R > W: R = W
    if U > H: U = H

    if s == 'L': L += 1
    elif s == 'R': R -= 1
    elif s == 'D': D += 1
    elif s == 'U': U -= 1
    
    if L>R or D>U:
        # 後手の勝てる場所が存在せず
        break

bl = (L <= sc <= R) and (D <= sr <= U)
answer = 'YES' if bl else 'NO'
print(answer)

Submission status: Wrong Answer

Solution code:
h,w,n=map(int,input().split())
sy,sx=map(int,input().split())
s=input()
t=input()
cnt1=[[0,0,0,0] for _ in range(n+1)]
cnt2=[[0,0,0,0] for _ in range(n+1)]
dic={'L':0,'R':1,'U':2,'D':3}
for i in range(n):
  tmp1=s[i]
  tmp2=t[i]
  cnt1[i+1][dic[tmp1]]=1
  cnt2[i+1][dic[tmp2]]=1
for i in range(2,n+1):
  for j in range(4):
    cnt1[i][j]+=cnt1[i-1][j]
    cnt2[i][j]+=cnt2[i-1][j]
flag=True
posl=-1
posr=-1
posu=-1
posd=-1
for i in range(n):
  if s[i]=='L' and posl==-1:
    posl=i+1
  if s[i]=='R' and posr==-1:
    posr=i+1
  if s[i]=='U' and posu==-1:
    posu=i+1
  if s[i]=='D' and posd==-1:
    posd=i+1
for i in range(4):
  if i==0:
    if posl==-1:
      continue
    diff=min(w,sx+cnt2[posl-1][i+1])
  if i==1:
    if posr==-1:
      continue
    diff=w-max(1,sx-cnt2[posr-1][i-1])+1
  if i==2:
    if posu==-1:
      continue
    diff=min(h,sy+cnt2[posu-1][i+1])
  if i==3:
    if posd==-1:
      continue
    diff=h-max(1,sy-cnt2[posd-1][i-1])+1
  for j in range(1,n+1):
    tmp=cnt1[j][i]
    if i==0:
      if tmp-(cnt2[i][i+1]-cnt2[posl-1][i+1])>diff:
        flag=False
    elif i==1:
      if tmp-(cnt2[i][i-1]-cnt2[posr-1][i-1])>diff:
        flag=False
    elif i==2:
      if tmp-(cnt2[i][i+1]-cnt2[posu-1][i+1])>diff:
        flag=False
    elif i==3:
      if tmp-(cnt2[i][i-1]-cnt2[posd-1][i-1])>diff:
        flag=False
if flag==True:
  print('YES')
else:
  print('NO')

Submission status: Wrong Answer

Solution code:
h,w,n=map(int,input().split())
sy,sx=map(int,input().split())
s=input()
t=input()
cnt1=[[0,0,0,0] for _ in range(n+1)]
cnt2=[[0,0,0,0] for _ in range(n+1)]
dic={'L':0,'R':1,'U':2,'D':3}
for i in range(n):
  tmp1=s[i]
  tmp2=t[i]
  cnt1[i+1][dic[tmp1]]=1
  cnt2[i+1][dic[tmp2]]=1
for i in range(2,n+1):
  for j in range(4):
    cnt1[i][j]+=cnt1[i-1][j]
    cnt2[i][j]+=cnt2[i-1][j]
flag=True
for i in range(4):
  for j in range(1,n+1):
    tmp=cnt1[j][i]
    if i==0:
      if sx==w and cnt1[j][i+1]>=1:
        if tmp>=min(w,sx+cnt2[j-1][i+1])-1:
          flag=False        
      else:
        if tmp>=min(w,sx+cnt2[j-1][i+1]):
          flag=False        
    elif i==1:
      if sx==1 and cnt1[j][i-1]>=1:
        if tmp>=(w-max(1,sx-cnt2[j-1][i-1])+1)-1:
          flag=False
      else:
        if tmp>=(w-max(1,sx-cnt2[j-1][i-1])+1):
          flag=False
    elif i==2:
      if sy==h and cnt1[j][i+1]>=1:
        if tmp>=min(h,sy+cnt2[j-1][i+1])-1:
          flag=False
      else:
        if tmp>=min(h,sy+cnt2[j-1][i+1]):
          flag=False
    elif i==3:
      if sy==1 and cnt1[j][i-1]>=1:
        if tmp>=(h-max(1,sy-cnt2[j-1][i-1])+1)-1:
          flag=False
      else:
        if tmp>=(h-max(1,sy-cnt2[j-1][i-1])+1):
          flag=False
if flag==True:
  print('YES')
else:
  print('NO')

Submission status: Wrong Answer

Solution code:
h,w,n=map(int,input().split())
sy,sx=map(int,input().split())
s=input()
t=input()
cnt1=[[0,0,0,0] for _ in range(n+1)]
cnt2=[[0,0,0,0] for _ in range(n+1)]
dic={'L':0,'R':1,'U':2,'D':3}
for i in range(n):
  tmp1=s[i]
  tmp2=t[i]
  cnt1[i+1][dic[tmp1]]=1
  cnt2[i+1][dic[tmp2]]=1
for i in range(2,n+1):
  for j in range(4):
    cnt1[i][j]+=cnt1[i-1][j]
    cnt2[i][j]+=cnt2[i-1][j]
flag=True
for i in range(4):
  for j in range(1,n+1):
    if i==0:
      tmp=cnt1[j][i]
      if sx==h and cnt1[j][i+1]>=1:
        if tmp>=min(w,sx+cnt2[j-1][i+1])-1:
          flag=False        
      else:
        if tmp>=min(w,sx+cnt2[j-1][i+1]):
          flag=False        
    elif i==1:
      tmp=cnt1[j][i]
      if sx==1 and cnt1[j][i-1]>=1:
        if tmp>=(w-max(1,sx-cnt2[j-1][i-1])+1)-1:
          flag=False
      else:
        if tmp>=(w-max(1,sx-cnt2[j-1][i-1])+1):
          flag=False
    elif i==2:
      tmp=cnt1[j][i]
      if sy==h and cnt1[j][i+1]>=1:
        if tmp>=min(h,sy+cnt2[j-1][i+1])-1:
          flag=False
      else:
        if tmp>=min(h,sy+cnt2[j-1][i+1]):
          flag=False
    elif i==3:
      tmp=cnt1[j][i]
      if sy==1 and cnt1[j][i-1]>=1:
        if tmp>=(h-max(1,sy-cnt2[j-1][i-1])+1)-1:
          flag=False
      else:
        if tmp>=(h-max(1,sy-cnt2[j-1][i-1])+1):
          flag=False
if flag==True:
  print('YES')
else:
  print('NO')

Submission status: Wrong Answer

Solution code:
h,w,n=map(int,input().split())
sy,sx=map(int,input().split())
s=input()
t=input()
cnt1=[[0,0,0,0] for _ in range(n+1)]
cnt2=[[0,0,0,0] for _ in range(n+1)]
dic={'L':0,'R':1,'U':2,'D':3}
for i in range(n):
  tmp1=s[i]
  tmp2=t[i]
  cnt1[i+1][dic[tmp1]]=1
  cnt2[i+1][dic[tmp2]]=1
for i in range(2,n+1):
  for j in range(4):
    cnt1[i][j]+=cnt1[i-1][j]
    cnt2[i][j]+=cnt2[i-1][j]
flag=True
for i in range(4):
  for j in range(1,n+1):
    if i==0:
      tmp=cnt1[j][i]
      if tmp>=min(w,sx+cnt2[j-1][i+1]):
        flag=False
    elif i==1:
      tmp=cnt1[j][i]
      if tmp>=(w-max(1,sx-cnt2[j-1][i-1])+1):
        flag=False
    elif i==2:
      tmp=cnt1[j][i]
      if tmp>=min(h,sy+cnt2[j-1][i+1]):
        flag=False
    elif i==3:
      tmp=cnt1[j][i]
      if tmp>=(h-max(1,sy-cnt2[j-1][i-1])+1):
        flag=False
if flag==True:
  print('YES')
else:
  print('NO')

Submission status: Wrong Answer

Solution code:
# ContestName
# URL
import sys
#import numpy as np

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    H, W, N = i2nn()
    sr, sc = i2nn()
    S = i2s()
    T = i2s()
    h0 = 0
    h1 = H
    w0 = 0
    w1 = W
    for i, s, t in zip(range(N), S[::-1], T[::-1]):
        if t == 'L':
            w1 += 1 if w0 < W else 0
        elif t == 'R':
            w0 -= 1 if w0 > 0 else 0
        elif t == 'U':
            h1 += 1 if h0 < H else 0
        elif t == 'D':
            h0 -= 1 if h0 > 0 else 0

        if s == 'L':
            w0 += 1
        elif s == 'R':
            w1 -= 1
        elif s == 'U':
            h0 += 1
        elif s == 'D':
            h1 -= 1

        if w0 >= w1 or h0 >= h1:
            print('NO')
            return
    if w0 <= sc-1 and sc-1 < w1 and h0 <= sr-1 and sr-1 < h1:
        print('YES')
    else:
        print('NO')

main()

"""
..#
...
"""

Submission status: Wrong Answer

Solution code:
import sys

movement = {'U': (-1, 0),
            'R': (0, 1),
            'D': (1, 0),
            'L': (0, -1)}

H, W, N = list(map(int, input().split()))
(Sr, Sc) = tuple(map(int, input().split()))
S = [i for i in input()]
T = [i for i in input()]

r_lim = W
l_lim = 1
u_lim = 1
d_lim = H

result = True

for turn in range(N):
    s = S[N-turn-1]
    t = T[N-turn-1]

    if s == 'R':
        r_lim += 1
    elif s == 'L':
        l_lim -= 1
    elif s == 'U':
        u_lim -= 1
    elif s == 'D':
        d_lim += 1

    if t == 'R' and l_lim > 1:
        l_lim -= 1
    elif t == 'L' and r_lim < W:
        r_lim += 1
    elif t == 'U' and d_lim < H:
        d_lim += 1
    elif t == 'D' and u_lim > 1:
        u_lim -= 1

    if r_lim < l_lim or d_lim < u_lim:
        result = False
        break

if result:
    if l_lim < Sc < r_lim or u_lim < Sr < d_lim:
        result = True
    else:
        result = False

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

Submission status: Wrong Answer

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

position = [Sr-1, W -Sr, Sc - 1, Sr -1]

Answer = 'Yes'
for i in range(N):
  if position[0] - S[0:i+1].count('L') +T[0:i].count('R') < 0:
    Answer = 'NO'
  elif position[1] - S[0:i+1].count('R')+T[0:i].count('L') < 0:
    Answer = 'No'
  elif position[2] - S[0:i+1].count('U')+T[0:i].count('D') < 0:
    Answer = 'No'
  elif position[3] -S[0:i+1].count('D') + T[0:i].count('U') < 0:
    Answer = 'No'

print(Answer)

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
S = input()[::-1]
T = input()[::-1]

r_bound = w
l_bound = 1
d_bound = h
u_bound = 1

remain = True

if S[0] =='R':
    r_bound -= 1
if S[0] =='L':
    l_bound += 1
if S[0] =='D':
    d_bound -= 1
if S[0] =='U':
    u_bound += 1

for s, t in zip(S[1:], T[1:]):
    if s =='R' and t !='L':
        r_bound -= 1
    if s =='L' and t !='R':
        l_bound += 1
    if s =='D' and t !='U':
        d_bound -= 1
    if s =='U' and t !='D':
        u_bound += 1
    if (r_bound < l_bound) or (d_bound < u_bound):
        remain = False
        break
if (sc < l_bound or sc > r_bound) or (sr < u_bound or sr > d_bound):
    remain = False

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

Submission status: Wrong Answer

Solution code:
# Input
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


def aok_turn(aoki: str, tolow: str, toup: str, lower: int, upper: int, amin: int, amax: int):
    if aoki == tolow:
        upper = min(amax, upper + 1)
    elif aoki == toup:
        lower = max(amin, lower - 1)

    return lower, upper


def tak_turn(taki: str, tolow: str, toup: str, lower: int, upper: int, amin:int, amax: int):
    if taki == tolow:
        lower = min(amax, lower + 1)
    elif taki == toup:
        upper = max(amin, upper - 1)

    return lower, upper


def on_board(lower: int, upper: int, amin: int, amax: int):
    if lower < upper and upper > amin and lower < amax:
        return True
    else:
        return False


def judge(tak: str, aok: str, tolow: str, toup: str, start: int, amin: int, amax: int) -> bool:
    lower = amin
    upper = amax
    for aoki, taki in zip(aok, tak):
        lower, upper = aok_turn(aoki, tolow, toup, lower, upper, amin, amax)
        lower, upper = tak_turn(taki, tolow, toup, lower ,upper, amin, amax)
        if not on_board(lower, upper, amin, amax):
            return False

    return True if lower <= start < upper else False


h,w,n = li()
sr, sc = li_()
tak = ns()[::-1]
aok = ns()[::-1]

L = 'L'
R = 'R'
U = 'U'
D = 'D'

lr = judge(tak, aok, L, R, sc, 0, w)
ud = judge(tak, aok, D, U, sr, 0, h)

print("YES") if lr and ud else print("NO")

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


def aok_turn(aoki: str, tolow: str, toup: str, lower: int, upper: int, amin: int, amax: int):
    if aoki == tolow:
        upper = min(amax, upper + 1)
    elif aoki == toup:
        lower = max(amin, lower - 1)

    return lower, upper


def tak_turn(taki: str, tolow: str, toup: str, lower: int, upper: int, amin:int, amxax: int):
    if taki == tolow:
        lower += 1
    elif taki == toup:
        upper -= 1

    return lower, upper


def on_board(lower: int, upper: int, amin: int, amax: int):
    if lower < upper and upper > amin and lower < amax:
        return True
    else:
        return False


def judge(tak: str, aok: str, tolow: str, toup: str, start: int, amin: int, amax: int) -> bool:
    lower = amin
    upper = amax
    for aoki, taki in zip(aok, tak):
        lower, upper = aok_turn(aoki, tolow, toup, lower, upper, amin, amax)
        lower, upper = tak_turn(taki, tolow, toup, lower ,upper, amin, amax)
        if not on_board(lower, upper, amin, amax):
            return False

    return True if lower <= start < upper else False


h,w,n = li()
sr, sc = li_()
tak = ns()[::-1]
aok = ns()[::-1]

L = 'L'
R = 'R'
U = 'U'
D = 'D'

lr = judge(tak, aok, L, R, sc, 0, w)
ud = judge(tak, aok, D, U, sr, 0, h)

print("YES") if lr and ud else print("NO")