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:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
dps = [[0,0,0,0]]*N
dpt = [[0,0,0,0]]*N
for i,ss in enumerate(S):
    if ss == 'L':
        dps[i][0]+=dps[i-1][0]
    if ss == 'R':
        dps[i][1]+=dps[i-1][1]
    if ss == 'U':
        dps[i][2]+=dps[i-1][2]
    if ss == 'D':
        dps[i][3]+=dps[i-1][3]
for i,st in enumerate(T):
    if st == 'L':
        dpt[i][0]+=dpt[i-1][0]
    if st == 'R':
        dpt[i][1]+=dpt[i-1][1]
    if st == 'U':
        dpt[i][2]+=dpt[i-1][2]
    if st == 'D':
        dpt[i][3]+=dpt[i-1][3]
ans = 'NO'
#はじめに落とせる場合
if dps[0][0]==1 & sc==1:
    ans ='YES'
elif dps[0][1]==1 & sc==W:
    ans ='YES'
elif dps[0][2]==1 & sr==1:
    ans ='YES'
elif dps[0][3]==1 & sr==H:
    ans ='YES'
else:
    for i in range(1,N):
        if dps[i][0]-dpt[i-1][1] == sc:
            ans = 'YES'
            break
        if dps[i][1]-dpt[i-1][0] == W-sc:
            ans = 'YES'
            break
        if dps[i][2]-dpt[i-1][3] == sr:
            ans = 'YES'
            break
        if dps[i][3]-dpt[i-1][2] == H-sr:
            ans = 'YES'
            break
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit
from collections import defaultdict
import itertools
H,W,N = [int(n) for n in input().split()]
Sr,Sc = [int(n) for n in input().split()]
#上に
Uout = H-Sr + 1
#下に
Dout = Sr + 1
#右に落とすのに必要な歩数
Rout = W - Sc + 1
#左に
Lout = Sc + 1
S = str(input())
T = str(input())
# ptrC = defaultdict(int)
# ptrA = defaultdict(int)

def check(outcnt, C, A):
    for s,t in zip(S,T):
        # print(outcnt)
        if( s == C):
            outcnt -= 1
        if(outcnt == 0 ):
            print("YES")
            exit()
        if(t == A ):
            outcnt += 1

# print(Rout,Lout,Uout,Dout)
check(Rout, "R", "L")
check(Lout, "L", "R")
check(Uout, "U", "D")
check(Dout, "D", "U")
print("NO")
# #右の検証
# for s,t in itertools.product(S,T):
#     if( s == "R"):
#         Rout, Lout = Rout-1, Lout + 1
#     if(t == "L"):
#         Rout, Lout = Rout+1, Lout - 1
#     if(Rout == 0 ):
#         print("Yes")
#         exit()
# #左
# for s,t in itertools.product(S,T):
#     if( s == "L"):
#         Lout = Lout - 1
#     if(t == "R"):
#         Lout = , Lout + 1
#     if(Rout == 0 ):
#         print("Yes")
#         exit()

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split(' '))
s_r, s_c = map(int, input().split(' '))
S = list(input())
T = list(input())
cnt = 0
for i in range(N):
  if S[i] == 'L':
    if abs((s_c-1)-W/2)>=abs((s_c)-W/2):
      s_c -= 1
      if s_c<1:
        break
  elif S[i] == 'R':
    if abs((s_c+1)-W/2)>=abs((s_c)-W/2):
      s_c += 1
      if s_c>W:
        break
  elif S[i] == 'U':
    if abs((s_r-1)-H/2)>=abs((s_r)-H/2):
      s_r -= 1
      if s_r<1:
        break
  else:
    if abs((s_r+1)-H/2)>=abs((s_r)-H/2):
      s_r += 1
      if s_r>H:
        break
  if T[i] == 'L':
    if abs((s_c-1)-W/2)<abs((s_c)-W/2):
      s_c -= 1
  elif  T[i] == 'R':
    if abs((s_c+1)-W/2)<abs((s_c)-W/2):
      s_c += 1
  elif T[i] == 'U':
    if abs((s_r-1)-H/2)<abs((s_r)-H/2):
      s_r -= 1
  else:
    if abs((s_r+1)-H/2)<abs((s_r)-H/2):
      s_r += 1
  cnt += 1
if cnt==N:
  print('YES')
else:
  print('NO')

Submission status: Wrong Answer

Solution code:
# coding:utf-8

import sys
# from collections import Counter, defaultdict

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    h, w, n = LI()
    sr, sc = LI_()
    S = SI()
    T = SI()

    D = {0: ('L', 'R', -1), 1: ('R', 'L', 1), 2: ('U', 'D', 1), 3: ('D', 'U', -1)}
    is_drop = False
    for t in range(2):
        taka, aoki, d = D[t]
        x = sc
        for i in range(n):
            if S[i] == taka:
                x += d
                if x < 0 or x >= w:
                    is_drop = True
                    break
            if T[i] == aoki:
                x -= d
                if x < 0:
                    x = 0
                if x >= w:
                    x = w - 1

    for t in range(2, 4):
        taka, aoki, d = D[t]
        y = sr
        for i in range(n):
            if S[i] == taka:
                y += d
                if y < 0 or y >= h:
                    is_drop = True
                    break
            if T[i] == aoki:
                y -= d
                if y < 0:
                    y = 0
                if y >= h:
                    y = h - 1

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



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split(' '))
s_r, s_c = map(int, input().split(' '))
S = list(input())
T = list(input())
cnt = 0
for i in range(N):
  if S[i] == 'L':
    if abs((s_c-1)-W/2)>=abs((s_c)-W/2):
      s_c -= 1
      if s_c<1:
        break
  elif S[i] == 'R':
    if abs((s_c+1)-W/2)>=abs((s_c)-W/2):
      s_c += 1
      if s_c>W:
        break
  elif S[i] == 'U':
    if abs((s_r-1)-H/2)>=abs((s_r)-H/2):
      s_r -= 1
      if s_r<1:
        break
  else:
    if abs((s_r+1)-H/2)>=abs((s_r)-H/2):
      s_r += 1
      if s_r>H:
        break
  if T[i] == 'L':
    if abs((s_c-1)-W/2)<=abs((s_c)-W/2):
      s_c -= 1
  elif  T[i] == 'R':
    if abs((s_c+1)-W/2)<=abs((s_c)-W/2):
      s_c += 1
  elif T[i] == 'U':
    if abs((s_r-1)-H/2)<=abs((s_r)-H/2):
      s_r -= 1
  else:
    if abs((s_r+1)-H/2)<=abs((s_r)-H/2):
      s_r += 1
  cnt += 1
if cnt==N:
  print('YES')
else:
  print('NO')

Submission status: Wrong Answer

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

def move1(move):
    global r
    global c
    if move == "L":
        if c <= W//2:
            c -= 1
    elif move == "R":
        if c >= W//2+1:
            c += 1
    elif move == "U":
        if r <= H//2:
            r -= 1
    elif move == "D":
        if r >= H//2 +1:
            r += 1

def move2(move):
    global r
    global c
    if move == "L":
        if c >= W//2+1:
            c -= 1
    elif move == "R":
        if c <= W//2:
            c += 1
    elif move == "U":
        if r >= H//2+1:
            r -= 1
    elif move == "D":
        if r <= H//2 :
            r += 1
            
for i in range(N):
    if not (1<= r <= H and 1<= c <= W):
        break
    move1(S[i])
    if not (1<= r <= H and 1<= c <= W):
        break
    move2(T[i])
    
else:
    if not (1<= r <= H and 1<= c <= W):
        print("NO")
        exit()
    else:
        print("YES")
        exit()
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()
dps = [[0,0,0,0]]*N
dpt = [[0,0,0,0]]*N
for i,ss in enumerate(S):
    if ss == 'L':
        dps[i][0]+=dps[i-1][0]
    if ss == 'R':
        dps[i][1]+=dps[i-1][1]
    if ss == 'U':
        dps[i][2]+=dps[i-1][2]
    if ss == 'D':
        dps[i][3]+=dps[i-1][3]
for i,st in enumerate(T):
    if st == 'L':
        dpt[i][0]+=dpt[i-1][0]
    if st == 'R':
        dpt[i][1]+=dpt[i-1][1]
    if st == 'U':
        dpt[i][2]+=dpt[i-1][2]
    if st == 'D':
        dpt[i][3]+=dpt[i-1][3]
ans = 'NO'
#はじめに落とせる場合
if dps[0][0]==1 & sr==1:
    ans ='YES'
elif dps[0][1]==1 & sr==H:
    ans ='YES'
elif dps[0][2]==1 & sc==1:
    ans ='YES'
elif dps[0][3]==1 & sc==W:
    ans ='YES'
else:
    for i in range(1,N):
        if dps[i][0]-dpt[i-1][1] == sr:
            ans = 'YES'
            break
        if dps[i][1]-dpt[i-1][0] == H-sr:
            ans = 'YES'
            break
        if dps[i][2]-dpt[i-1][3] == sc:
            ans = 'YES'
            break
        if dps[i][3]-dpt[i-1][2] == W-sc:
            ans = 'YES'
            break
print(ans)

Submission status: Wrong Answer

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

# L, R, U, D = (0, -1), (0, 1), (-1, 0), (1, 0)
for i in range(N):
    # Takahashi
    if S[i] == "L" or S[i] == "R":
        if S[i] == "L":
            if x <= W - x:
                x -= 1
        else:
            if x >= W - x:
                x += 1
    else:
        if S[i] == "U":
            if y <= H - y:
                y -= 1
        else:
            if x <= H - y:
                y -= 1
    if x <= 0 or x > W or y <= 0 or y > H:
        print("NO")
        exit(0)
    # Aoki
    if T[i] == "L" or T[i] == "R":
        if T[i] == "L":
            if x > W - x:
                x -= 1
        else:
            if x < W - x:
                x += 1
    else:
        if T[i] == "U":
            if y > H - y:
                y -= 1
        else:
            if x < H - y:
                y -= 1
    if x <= 0 or x > W or y <= 0 or y > H:
        print("NO")
        exit(0)
print("YES")

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
dps = [[0,0,0,0]]*N
dpt = [[0,0,0,0]]*N
for i,ss in enumerate(S):
    if ss == 'L':
        dps[i][0]+=dps[i-1][0]
    if ss == 'R':
        dps[i][1]+=dps[i-1][1]
    if ss == 'U':
        dps[i][2]+=dps[i-1][2]
    if ss == 'D':
        dps[i][3]+=dps[i-1][3]
for i,st in enumerate(T):
    if st == 'L':
        dpt[i][0]+=dpt[i-1][0]
    if st == 'R':
        dpt[i][1]+=dpt[i-1][1]
    if st == 'U':
        dpt[i][2]+=dpt[i-1][2]
    if st == 'D':
        dpt[i][3]+=dpt[i-1][3]
ans = 'NO'
#はじめに落とせる場合
if dps[0][0]==1 & sr==1:
    ans ='YES'
elif dps[0][1]==1 & sr==H:
    ans ='YES'
elif dps[0][2]==1 & sc==1:
    ans ='YES'
elif dps[0][3]==1 & sc==W:
    ans ='YES'
else:
    for i in range(1,N):
        if dps[i][0]-dps[i-1][1] == sr:
            ans = 'YES'
            break
        if dps[i][1]-dps[i-1][0] == H-sr:
            ans = 'YES'
            break
        if dps[i][2]-dps[i-1][3] == sc:
            ans = 'YES'
            break
        if dps[i][3]-dps[i-1][2] == W-sc:
            ans = 'YES'
            break
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input():
	return sys.stdin.readline()[:-1]

H, W, n = map(int, input().split())
r, c = map(int, input().split())
s = input()
t = input()

L, R, U, D = c, W+1-c, r, H+1-r

dp = [[0 for _ in range(4)] for _ in range(n+1)]
dp[0][0] = L
dp[0][1] = R
dp[0][2] = U
dp[0][3] = D
for i in range(1, n+1):
	if dp[i-1][0] == 1 and s[i-1] == "L":
		print("NO")
		sys.exit()
	if dp[i-1][1] == 1 and s[i-1] == "R":
		print("NO")
		sys.exit()
	if dp[i-1][2] == 1 and s[i-1] == "U":
		print("NO")
		sys.exit()
	if dp[i-1][3] == 1 and s[i-1] == "D":
		print("NO")
		sys.exit()
	if s[i-1] == "L" and t[i-1] != "R":
		dp[i][0] = dp[i-1][0] - 1
	else:
		dp[i][0] = dp[i-1][0]
	if s[i-1] == "R" and t[i-1] != "L":
		dp[i][1] = dp[i-1][1] - 1
	else:
		dp[i][1] = dp[i-1][1]
	if s[i-1] == "U" and t[i-1] != "D":
		dp[i][2] = dp[i-1][2] - 1
	else:
		dp[i][2] = dp[i-1][2]
	if s[i-1] == "D" and t[i-1] != "U":
		dp[i][3] = dp[i-1][3] - 1
	else:
		dp[i][3] = dp[i-1][3]

	#print(dp[i-1], dp[i])

#print(dp)
print("YES")

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split(' '))
s_r, s_c = map(int, input().split(' '))
S = list(input())
T = list(input())
cnt = 0
for i in range(N):
  if S[i] == 'L':
    if abs((s_c-1)-W/2)>abs((s_c)-W/2):
      s_c -= 1
      if s_c<1:
        break
  elif S[i] == 'R':
    if abs((s_c+1)-W/2)>abs((s_c)-W/2):
      s_c += 1
      if s_c>W:
        break
  elif S[i] == 'U':
    if abs((s_r-1)-H/2)>abs((s_r)-H/2):
      s_r -= 1
      if s_r<1:
        break
  else:
    if abs((s_r+1)-H/2)>abs((s_r)-H/2):
      s_r += 1
      if s_r>H:
        break
  if T[i] == 'L':
    if abs((s_c-1)-W/2)<=abs((s_c)-W/2):
      s_c -= 1
  elif  T[i] == 'R':
    if abs((s_c+1)-W/2)<=abs((s_c)-W/2):
      s_c += 1
  elif T[i] == 'U':
    if abs((s_r-1)-H/2)<=abs((s_r)-H/2):
      s_r -= 1
  else:
    if abs((s_r+1)-H/2)<=abs((s_r)-H/2):
      s_r += 1
  cnt += 1
if cnt==N:
  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()

up = [0] * (N+1)
down = [0] * (N+1)
right = [0] * (N+1)
left = [0] * (N+1)
maxU, maxD, maxR, maxL = 0, 0, 0, 0

for i in range(N):
    if S[i] == "L":
        left[i+1] = left[i] + 1
        maxL = max(maxL, left[i+1])
    elif S[i] == "R":
        right[i+1] = right[i] + 1
        maxR = max(maxR, right[i+1])
    elif S[i] == "U":
        up[i+1] = up[i] + 1
        maxU = max(maxU, up[i+1])
    else:
        down[i+1] = down[i] + 1
        maxD = max(maxD, down[i+1])
    if T[i] == "L":
        right[i+1] = max(right[i] - 1, 0)
    elif T[i] == "R":
        left[i+1] = max(left[i] - 1, 0)
    elif T[i] == "U":
        down[i+1] = max(down[i] - 1, 0)
    else:
        up[i+1] = max(up[i] - 1, 0)

if sr + maxD > H or sr - maxU <= 0 or sc + maxR > W or sc + maxL <= 0:
    print("NO")
else: print("YES")

Submission status: Wrong Answer

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

flag = 0
hidari = sc
migi = sc
ue = sr
shita = sr
for i in range(N):
  if S[i] == 'L':
    hidari -= 1
  if S[i] == 'R':
    migi += 1
  if S[i] == 'U':
    ue -= 1
  if S[i] == 'D':
    shita += 1
  if hidari == 0 or migi == W+1 or ue == 0 or shita == H+1 :
    flag = 1
  
  if T[i] == 'R' and hidari != W:
    hidari += 1
  if T[i] == 'L' and migi != 1:
    migi -= 1
  if T[i] == 'D' and ue != 1:
    ue += 1
  if T[i] == 'U' and shita != H:
    shita -= 1

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

Submission status: Wrong Answer

Solution code:
Y, X, N = map(int, input().split())
y, x = map(int, input().split())
s = list(input())
t = list(input())
	
def moveToOut(char):
	if char == 'L':
		return x <= X / 2
	elif char == 'R':
		return x > X / 2
	elif char == 'U':
		return y <= Y / 2
	else:
		return y > Y / 2

def move(char):
	global x, y
	if char == 'L':
		x -= 1
	elif char == 'R':
		x += 1
	elif char == 'U':
		y -= 1
	else:
		y += 1

# 駒が残っていれば True
def step(i):
	if (moveToOut(s[i])):
		move(s[i])
	if (x < 0 or X <= x or y < 0 or Y <= y):
		return True
	if (not moveToOut(t[i])):
		move(t[i])
	return False

def f():
	for i in range(N):
		if (not step(i)):
			return True
	return False

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

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sy, sx = map(int, input().split())
ss = input().strip()
ts = input().strip()

sd, td = {'R': 0, 'L': 0, 'D': 0, 'U': 0}, {'R': 0, 'L': 0, 'D': 0, 'U': 0}
for s, t in zip(ss + 'N', 'N' + ts):
  if t != 'N':
    td[t] += 1
  if sx - td['L'] + td['R'] <= 0 or sx + td['R'] - td['L'] > w or \
    sy - td['U'] + td['D'] <= 0 or sy + td['D'] - td['U'] > h:
    td[t] -= 1

  if s != 'N':
    sd[s] += 1
  if sx - td['L'] + sd['R'] > w or sx + td['R'] - sd['L'] <= 0 or \
    sy - td['U'] + sd['D'] > h or sy + td['D'] + sd['U'] <= 0:
    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())
S = input()
T = input()

flag = 0
hidari = sc
migi = sc
ue = sr
shita = sr
for i in range(N):
  if S[i] == 'L':
    hidari -= 1
  if S[i] == 'R':
    migi += 1
  if S[i] == 'U':
    ue -= 1
  if S[i] == 'D':
    shita += 1
  if hidari == 0 or migi == W+1 or ue == H+1 or shita == 0:
    flag = 1
  
  if T[i] == 'R' and hidari != W:
    hidari += 1
  if T[i] == 'L' and migi != 1:
    migi -= 1
  if T[i] == 'D' and ue != 1:
    ue += 1
  if T[i] == 'U' and shita != H:
    shita -= 1

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

Submission status: Wrong Answer

Solution code:
Y, X, N = map(int, input().split())
y, x = map(int, input().split())
s = list(input())
t = list(input())
	
def moveToOut(char):
	if char == 'L':
		return x <= X // 2
	elif char == 'R':
		return x > X // 2
	elif char == 'U':
		return y <= Y // 2
	else:
		return y > Y // 2

def move(char):
	global x, y
	if char == 'L':
		x -= 1
	elif char == 'R':
		x += 1
	elif char == 'U':
		y -= 1
	else:
		y += 1

# 駒が残っていれば True
def step(i):
	if (moveToOut(s[i])):
		move(s[i])
	if (x < 0 or X <= x or y < 0 or Y <= y):
		return True
	if (not moveToOut(t[i])):
		move(t[i])
	return False

def f():
	for i in range(N):
		if (not step(i)):
			return True
	return False

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

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
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())

h,w,n = li()
sr, sc = li()
s = ns()
t = ns()

# LRUDそれぞれの必要距離
toleft = sc
toright = w - sc + 1
totop = sr
tobottom = h - sr + 1

# LRUDそれぞれの最大進度
rcur = 0
lcur = 0
ucur = 0
dcur = 0

rmax = 0
lmax = 0
umax = 0
dmax = 0

for si, ti in zip(s,t):
    if si == 'R':
        rcur = rcur + 1
        
    elif si == 'L':
        lcur = lcur + 1
        
    elif si == 'U':
        ucur = ucur + 1
        
    elif si == 'D':
        dcur = dcur + 1
        
    rmax = max(rmax, rcur)
    lmax = max(lmax, lcur)
    umax = max(umax, ucur)
    dmax = max(dmax, dcur)
    
    if ti == 'R':
        lcur = max(-toleft+1, lcur - 1)
        
    elif ti == 'L':
        rcur = max(-toright+1, rcur - 1)
        
    elif ti == 'U':
        dcur = max(-tobottom+1, dcur - 1)
        
    elif ti == 'D':
        ucur = max(-totop+1, ucur - 1)
        
if lmax >= toleft\
    or rmax >= toright\
    or umax >= totop\
    or dmax >= tobottom:
        print("NO")
        
else:
    print("YES")

Submission status: Wrong Answer

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
midh = (H-1)/2
midw = (W-1)/2
flag = False
for i in range(N):
    if S[i] == 'U':
        if nowh <= midh:
            nowh -= 1
    elif S[i] == 'D':
        if nowh >= midh:
            nowh += 1
    elif S[i] == 'L':
        if noww <= midw:
            noww -= 1
    else:
        if noww >= midw:
            noww += 1
    if nowh < 0 or nowh >= H:
        flag = True
        break
    if noww < 0 or noww >= W:
        flag = True
        break


    if T[i] == 'U':
        if nowh > midh:
            nowh -= 1
    elif T[i] == 'D':
        if nowh < midh:
            nowh += 1
    elif T[i] == 'L':
        if noww > midw:
            noww -= 1
    else:
        if noww < midw:
            noww += 1
    if nowh < 0 or nowh >= H:
        flag = True
        break
    if noww < 0 or noww >= W:
        flag = True
        break


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

Submission status: Wrong Answer

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

list=input().split(" ")
h=int(list[0])
w=int(list[1])
n=int(list[2])

list=input().split(" ")
y=int(list[0])
x=int(list[1])

#l,r,u,d
ato=[x, w-x+1, y, h-y+1]
countt=[0,0,0,0]
counta=[0,0,0,0]
#print(ato)

taka=input()
aoki=input()

switch=0

for i in range(n+1):
    if i<n:
        if taka[i]=='L':
            countt[0]+=1
        elif taka[i]=='R':
            countt[1]+=1
        elif taka[i]=='U':
            countt[2]+=1
        elif taka[i]=='D':
            countt[3]+=1
    
    if i>0:
        if aoki[i-1]=='L':
            counta[0]+=1
        elif aoki[i-1]=='R':
            counta[1]+=1
        elif aoki[i-1]=='U':
            counta[2]+=1
        elif aoki[i-1]=='D':
            counta[3]+=1
    
    countn=[countt[0]-counta[1], countt[1]-counta[0], countt[2]-counta[3], countt[3]-counta[2]]
    #print(countn)
    for j in range(4):
        if countn[j]>=ato[j]:
            switch=1

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

Submission status: Wrong Answer

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

flag = 0
hidari = sc
migi = sc
ue = sr
shita = sr
for i in range(N):
  print(hidari,migi,ue,shita)
  if S[i] == 'L':
    hidari -= 1
  if S[i] == 'R':
    migi += 1
  if S[i] == 'U':
    ue -= 1
  if S[i] == 'D':
    shita += 1
  if hidari == 0 or migi == W+1 or ue == H+1 or shita == 0:
    flag = 1
  
  print(hidari,migi,ue,shita)
  if T[i] == 'R' and hidari != W:
    hidari += 1
  if T[i] == 'L' and migi != 0:
    migi -= 1
  if T[i] == 'D' and ue != 0:
    ue += 1
  if T[i] == 'U' and shita != H:
    shita -= 1

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

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
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())

h,w,n = li()
sr, sc = li()
s = ns()
t = ns()

# LRUDそれぞれの必要距離
toleft = sc
toright = w - sc + 1
totop = sr
tobottom = h - sr + 1

# LRUDそれぞれの最大進度
rcur = 0
lcur = 0
ucur = 0
dcur = 0

rmax = 0
lmax = 0
umax = 0
dmax = 0

for si, ti in zip(s,t):
    if si == 'R':
        rcur = rcur + 1
        
    elif si == 'L':
        lcur = lcur + 1
        
    elif si == 'U':
        ucur = ucur + 1
        
    elif si == 'D':
        dcur = dcur + 1
        
    rmax = max(rmax, rcur)
    lmax = max(lmax, lcur)
    umax = max(umax, ucur)
    dmax = max(dmax, dcur)
    
    if ti == 'R':
        lcur = max(0, lcur - 1)
        
    elif ti == 'L':
        rcur = max(0, rcur - 1)
        
    elif ti == 'U':
        dcur = max(0, dcur - 1)
        
    elif ti == 'D':
        ucur = max(0, ucur - 1)
        
if lmax >= toleft\
    or rmax >= toright\
    or umax >= totop\
    or dmax >= tobottom:
        print("NO")
        
else:
    print("YES")

Submission status: Wrong Answer

Solution code:
H,W,N = map(int,raw_input().split())
A,B = map(int,raw_input().split())
s = list(raw_input())
t = list(raw_input())

def solve(s,t,c1,c2,A,H,N):
	cur = A
	s1 = [0 for x in range(N+1)]
	s2 = [0 for x in range(N+1)]
	s3 = [0 for x in range(N+1)]
	s4 = [0 for x in range(N+1)]
	for i in range(N):
		if s[i]==c1:
			s1[i+1] += 1
		if s[i]==c2:
			s2[i+1] += 1
		if t[i]==c1:
			s3[i+1] += 1
		if t[i]==c2:
			s4[i+1] += 1
		s1[i+1] += s1[i]
		s2[i+1] += s2[i]
		s3[i+1] += s3[i]
		s4[i+1] += s4[i]
	for i in range(N):
		if (s2[N]-s2[i])-(s3[N]-s3[i])>H-A:
			return 1
		if (s1[N]-s1[i])-(s4[N]-s4[i])>=A:
			return 1
		if s2[i+1]-s3[i]>H-A:
			return 1
		if s1[i+1]-s4[i]>=A:
			return 1
	return 0



if solve(s,t,'U','D',A,H,N) or solve(s,t,'L','R',B,W,N):
	print "NO"
else:
	print "YES"

Submission status: Wrong Answer

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()
    sr,sc = LI()
    s = S()
    t = S()
    l = r = u = d = 0
    for i in range(n):
        si = s[i]
        ti = t[i]

        if si == 'L':
            l += 1
            if l >= sc:
                return 'NO'
        elif si == 'R':
            r += 1
            if r+sc > w:
                return 'NO'
        elif si == 'U':
            u += 1
            if u >= sr:
                return 'NO'
        else:
            d += 1
            if d+sr > h:
                return 'NO'

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

        if r <= -sr:
            r = -sr + 1
        if l < -(w-sr):
            l = -(w-sr)
        if u <= -sc + 1:
            u = -sc
        if d < -(h-sc):
            d = -(h-sc)

        # print(l,r,u,d,sr,sc)

    return 'YES'


print(main())

Submission status: Wrong Answer

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()
    sr,sc = LI()
    s = S()
    t = S()
    l = r = u = d = 0
    for i in range(n):
        si = s[i]
        ti = t[i]

        if si == 'L':
            l += 1
            if l >= sc:
                return 'NO'
        elif si == 'R':
            r += 1
            if r+sc > w:
                return 'NO'
        elif si == 'U':
            u += 1
            if u >= sr:
                return 'NO'
        else:
            d += 1
            if d+sr > h:
                return 'NO'

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

        if r <= -sr:
            r = -sr + 1
        if l < -(w-sr):
            l = -(w-sr)
        if u <= -sc + 1:
            u = -sc
        if d < -(h-sc):
            d = -(h-sc)

        print(l,r,u,d,sr,sc)

    return 'YES'


print(main())

Submission status: Time Limit Exceeded

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

h -= 1
w -= 1
bord = [[0]*W for _ in range(H)]
for i in range(N):
    if S[i] == "L":
        if w-1 < W-w-1:
            w -= 1
    elif S[i] == "R":
        if w > W-w-2:
            w += 1
    elif S[i] == "U":
        if h-1 < H-h-1:
            h -= 1
    elif S[i] == "D":
        if h > H-h-2:
            h += 1
    if w == 0 or h == 0 or h == H or w == W:
        print("NO")
        break

    if T[i] == "L":
        if w-1 > W-w-1:
            w -= 1
    elif T[i] == "R":
        if w < W-w-2:
            w += 1
    elif T[i] == "U":
        if h-1 > H-h-1:
            h -= 1
    elif T[i] == "D":
        if h < H-h-2:
            h += 1
else:
    print("YES")

Submission status: Time Limit Exceeded

Solution code:
H, W, N = map(int, input().split())
sh, sw = map(int, input().split())

S = input()
T = input()

def canTakaWin(toS, isInc, toT, R, c):
    mask = ((1 << (R + 1)) - 1) ^ 1
    now = 1 << c

    for s, t in zip(S, T):
        if now == 0:
            return True
        if s == toS:
            now = (now << 1) if isInc else (now >> 1)
        now &= mask

        direct = 0
        for i, k in enumerate(toT):
            if k == t:
                direct = (-1)**i
        if direct != 0:
            now |= (now << 1) if direct > 0 else (now >> 1)
        now &= mask

    return now == 0

ans = False
ans = ans or canTakaWin('U', False, 'DU', H, sh)
ans = ans or canTakaWin('D', True, 'DU', H, sh)
ans = ans or canTakaWin('L', False, 'RL', W, sw)
ans = ans or canTakaWin('R', True, 'RL', W, sw)

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

Submission status: Time Limit Exceeded

Solution code:
H, W, N = map(int, input().split())
sh, sw = map(int, input().split())

S = input()
T = input()

def canTakaWin(toS, isInc, toT, R, c):
    mask = ((1 << (R + 1)) - 1) ^ 1
    now = 1 << c

    for s, t in zip(S, T):
        if s == toS:
            now = (now << 1) if isInc else (now >> 1)
        now &= mask

        direct = 0
        for i, k in enumerate(toT):
            if k == t:
                direct = (-1)**i
        if direct != 0:
            now |= (now << 1) if direct > 0 else (now >> 1)
        now &= mask

    return now == 0

ans = False
ans = ans or canTakaWin('U', False, 'DU', H, sh)
ans = ans or canTakaWin('D', True, 'DU', H, sh)
ans = ans or canTakaWin('L', False, 'RL', W, sw)
ans = ans or canTakaWin('R', True, 'RL', W, sw)

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