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:
#B問題 提出用
from collections import Counter

def untildeath(deathlist,direction):
    if direction == "R" and deathlist[1] < W:
        deathlist[1] += 1
    elif direction == "L" and deathlist[0] < W:
        deathlist[0] += 1
    elif direction == "U" and deathlist[3] < H:
        deathlist[3] += 1
    elif direction == "D" and deathlist[2] < H:
        deathlist[2] += 1
    return deathlist

def possible_step(killinglist, direction):
    if direction == "R":
        killinglist[0] += 1
    elif direction == "L":
        killinglist[1] += 1
    elif direction == "U":
        killinglist[2] += 1
    elif direction == "D":
        killinglist[3] += 1
    return killinglist

def isDead(killinglist, deathlist):
    for i in range(4):
        if killinglist[i] - deathlist[i] >=0:
            return True
#     print("alive")
    return False

#入力2
# H=4
# W=3
# N=5
# init_r=2
# init_c=2
# S="UDRRR"
# T="LLDUD"

#読み込み
H,W,N = map(int,input().split())
init_r,init_c = map(int,input().split())
S = input()
T = input()

#こま死亡までの最大ステップ数 K_R,L,U,D
K_R = (W - init_c + 1)
K_L = init_c
K_U = init_r
K_D = (H - init_r + 1)
# check
# print(K_R,K_L,K_U,K_D)
# print(Counter(S))
deathlist = [K_R, K_L, K_U, K_D]
killinglist = [0,0,0,0]
ans = "YES"
for i in range(N):
#     print(i)
    direction = S[i]
    killinglist = possible_step(killinglist, direction)
#     print(killinglist)
    if isDead(killinglist, deathlist):
        ans = "NO"
        break
    direction = T[i]
    deathlist = untildeath(deathlist, direction)
#     print(deathlist)
    
print(ans)

#listはrludの順で
# i ステップ目までの絶対死亡するステップ数を数える

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] = right[i] - 1
    elif T[i] == "R":
        left[i+1] = left[i] - 1
    elif T[i] == "U":
        down[i+1] = down[i] - 1
    else:
        up[i+1] = up[i] - 1
 
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=input().split()
R,C=input().split()
S=input()
T=input()
h=int(H)
w=int(W)
n=int(N)
r=int(R)
c=int(C)

if S[:n].count('U')-T[:n].count('D')>r:
  a=0
elif S[:n].count('D')-T[:n].count('U')>h-r:
  a=0
elif S[:n].count('R')-T[:n].count('L')>w-c:
  a=0
elif S[:n].count('L')-T[:n].count('R')>c:
  a=0
else:
  a=1
    
if a==0:
  print('NO')
else:
  print('YES')

Submission status: Wrong Answer

Solution code:
import sys
h,w,n = map(int,input().split())
x,y = map(int,input().split())
s = list(input())
t = list(input())

up = [h+1]*n
down = [0]*n
right = [w+1]*n
left = [0]*n

if s[-1] == 'U':
    up[-1] = up[-1]-1
elif s[-1] == 'D':
    down[-1] = down[-1]+1
elif s[-1] == 'L':
    left[-1] = left[-1]+1
else:
    right[-1] = right[-1]-1




for i in range(n-2,-1,-1):
    up[i] = up[i+1]
    down[i] = down[i+1]
    left[i] = left[i+1]
    right[i] = right[i+1]

    if t[i] == 'U':
        down[i] -= 1
    if t[i] == 'D':
        up[i] += 1
    if t[i] == 'R':
        left[i] -= 1
    if t[i] == 'L':
        right[i] += 1

    if s[i] == 'U':
        up[i] -= 1
    elif s[i] == 'D':
        down[i] += 1
    elif s[i] == 'L':
        left[i] += 1
    else:
        right[i] -= 1

    if right[i] == 1 or left[i] == w or up[i] == 1 or down[i] == h:
        print('NO')
        exit()

if up[0]<=y or down[0]>=y or right[0]<=x or left[0] >= y:
    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()
dps = [[0,0,0,0] for _ in range(N)]
dpt = [[0,0,0,0] for _ in range(N)]
for i,ss in enumerate(S):
    dps[i] = dps[i-1].copy()
    if ss == 'L':
        dps[i][0] += 1
    if ss == 'R':
        dps[i][1] += 1
    if ss == 'U':
        dps[i][2] += 1
    if ss == 'D':
        dps[i][3] += 1
for i,st in enumerate(T):
    dpt[i] = dpt[i-1].copy()
    if (st == 'L') and (dps[i][1]-dpt[i-1][0] + sr != 1):
        dpt[i][0] += 1
    if (st == 'R') and (-dps[i][0]+dpt[i-1][1] + W - sr != W):
        dpt[i][1] += 1
    if (st == 'U') and (dps[i][2]-dpt[i-1][3] + sc != 1):
        dpt[i][2] += 1
    if (st == 'D') and (-dps[i][3]+dpt[i-1][2] + H - sc != H):
        dpt[i][3] += 1
dps
dpt
ans = 'YES'
#はじめに落とせる場合
if dps[0][0]==1 & sc==1:
    ans ='NO'
elif dps[0][1]==1 & sc==W:
    ans ='NO'
elif dps[0][2]==1 & sr==1:
    ans ='NO'
elif dps[0][3]==1 & sr==H:
    ans ='NO'
else:
    for i in range(1,N):
        if -dps[i][0]+dpt[i-1][1]+sc == 0:
            ans = 'NO'
            break
        if dps[i][1]-dpt[i-1][0]+sc == W+1:
            ans = 'NO'
            break
        if -dps[i][2]+dpt[i-1][3]+sr==0:
            ans = 'NO'
            break
        if dps[i][3]-dpt[i-1][2]+sr == H+1:
            ans = 'NO'
            break
print(ans)

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, -sc)
    elif T[i] == "R":
        left[i+1] = max(left[i] - 1, - (W - sc))
    elif T[i] == "U":
        down[i+1] = max(down[i] - 1, 0)
    else:
        up[i+1] = max(up[i] - 1, - (H - sr))
 
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()

Pue = sr
Psita = H-sr + 1

Phidari = sc
Pmigi = W-sc + 1

from collections import Counter
SC = Counter(S)
ST = Counter(T)

# L 
flag = False # 落とせない、がデフォルト
if SC["L"] - ST["R"] >= Phidari:
    flag = True # 落とせる
else:
    K = SC["L"] - Phidari + 1 # これ最小のK 
    nowpos = sc
    cnt = 0
    for i in range(N):
        si,ti = S[i], T[i]

        if ti == "R":
            if cnt < ST["R"]-K:
                cnt += 1
            else:
                nowpos -= (ti=="R")
        nowpos += (si=="L") 

        if nowpos <= 0:
            flag = True #落とせる！ 
            break    

if SC["R"] - ST["L"] >= Pmigi:
    flag = True 
else:
    K = SC["R"] - Pmigi + 1 # これ最小のK 
    nowpos = sc
    cnt = 0
    for i in range(N):
        si,ti = S[i], T[i]

        if ti == "L":
            if cnt < ST["L"]-K:
                cnt += 1
            else:
                nowpos -= (ti=="L")
        nowpos += (si=="R") 

        if nowpos <= 0:
            flag = True #落とせる！ 
            break
        
    
    
if SC["U"] - ST["D"] >= Pue:
    flag = True
else:
    K = SC["U"] - Pue + 1 # これ最小のK 
    nowpos = sc
    cnt = 0
    for i in range(N):
        si,ti = S[i], T[i]

        if ti == "D":
            if cnt < ST["D"]-K:
                cnt += 1
            else:
                nowpos -= (ti=="D")
        nowpos += (si=="U") 

        if nowpos <= 0:
            flag = True #落とせる！ 
            break
if SC["D"] - ST["U"] >= Psita:    
    flag = True
else:
    K = SC["D"] - Psita + 1 # これ最小のK 
    nowpos = sc
    cnt = 0
    for i in range(N):
        si,ti = S[i], T[i]

        if ti == "U":
            if cnt < ST["U"]-K:
                cnt += 1
            else:
                nowpos -= (ti=="U")
        nowpos += (si=="D") 

        if nowpos <= 0:
            flag = True #落とせる！ 
            break

if not flag: 
    print("YES")
else: # 落とせるなら……　
    print("NO")

Submission status: Wrong Answer

Solution code:
h,w,n = [int(_) for _ in input().split()]
sr,sc = [int(_) for _ in input().split()]
s = input()
t = input()

result = ""
# RL
if s.count('R')+sr - t.count('L') >= w:
  result = 'YES'
elif s.count('D')+sc - t.count('U') >= h:
  result = 'YES'
else:
  result = 'NO'

print(result)

Submission status: Wrong Answer

Solution code:
import numpy as np
H,W,N =map(int ,raw_input().split())
Sr,Sc =map(int ,raw_input().split())
S = raw_input()
T = raw_input()

S = list(S)
T = list(T)

if Sr % 2 == 0:
    Dr = H/2 -0.5
else:
    Dr = (H-1)/2
if Sc % 2 == 0:
    Dc = W/2 -0.5
else:
    Dc = (W-1)/2 
s = 1
for i in range(N):
    if (S[i] == 'D')and(Sr >=Dr):
        Sr = Sr+1
    elif (S[i] == 'U')and(Sr <=Dr):
        Sr = Sr-1
    elif (S[i] == 'R')and(Sc >=Dc):
        Sc = Sc+1
    elif (S[i] == 'L')and(Sc <=Dc):
        Sc = Sc-1
    if (Sr < 0)or(Sr > H-1)or(Sc < 0)or(Sc > W-1):
        s = 0
        break
    if (T[i] == 'D')and(Sr <Dr):
        Sr = Sr+1
    elif (T[i] == 'U')and(Sr >Dr):
        Sr = Sr-1
    elif (T[i] == 'R')and(Sc <Dc):
        Sc = Sc+1
    elif (T[i] == 'L')and(Sc >Dc):
        Sc = Sc-1
    
    if (Sr < 0)or(Sr > H-1)or(Sc < 0)or(Sc > W-1):
        s = 0
        break
if s ==0:
    print('NO')
else:
    print('YES')

Submission status: Wrong Answer

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]):
    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)
    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()
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: 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())
ssr,ssc = map(int,input().split())
S = input()
T = input()

ssr -= 1
ssc -= 1

A = ["L", "R", "U", "D"]

A2 = {"L": -1, "R": 1, "U": -1, "D": 1}

D ={"L": "R", "R": "L", "U": "D", "D": "U"}

ans = "YES"

flg = 0

for a in A :
    
    sr = ssr
    sc = ssc
    
    if flg :
        break
        
    for i in range(N) :
        if (a in ["L", "R"]) and (a == S[i]) :
            sc += A2[a]
            
            if (sc < 0) or (sc >= W) :
                ans = "NO"
                flg = 1
                break
            
            if D[a] == T[i] :
                if (sc == 0) and (D[a] == "L") :
                    continue
                elif (sc == W-1) and (D[a] == "R") :
                    continue
                sc += A2[D[a]]
            
        elif (a in ["U", "D"]) and (a == S[i]) :
            sr += A2[a]
            
            if (sr < 0) or (sr >= H) :
                ans = "NO"
                flg = 1
                break
            
            if T[i] == D[a] :
                if (sr == 0) and (D[a] == "U") :
                    continue
                elif (sr == H-1) and (D[a] == "D") :
                    continue
                sr += A2[D[a]]
        #print((sr,sc))
print(ans)

Submission status: Wrong Answer

Solution code:

def Takahashi(now_w,now_h,next_w,next_h,center_w,center_h):
    if EuclidDis(now_w,now_h,center_w,center_h) < EuclidDis(next_w,next_h,center_w,center_h):
        return (next_w,next_h)
    else:
        return (now_w,now_h)
def Aoki(now_w,now_h,next_w,next_h,center_w,center_h):
    if EuclidDis(now_w,now_h,center_w,center_h) > EuclidDis(next_w,next_h,center_w,center_h):
        return (next_w,next_h)
    else:
        return (now_w,now_h)

def EuclidDis(x0,y0,x1,y1):
    return ((x0-x1)**2 + (y0-y1)**2)

def isRemain(sr,sc,op):
    center_h = H / 2
    center_w = W / 2
    now_h = sr
    now_w = sc
    for i in range(len(op)):
        if op[i] == 'L':
            next_w = now_w - 1
            next_h = now_h
        elif op[i] == 'R':
            next_w = now_w + 1
            next_h = now_h
        elif op[i] == 'U':
            next_w = now_w
            next_h = now_h + 1
        elif op[i] == 'D':
            next_w = now_w
            next_h = now_h - 1
        if i % 2 == 0:#Sのop
            now_w,now_h = Takahashi(now_w,now_h,next_w,next_h,center_w,center_h)
            #print('Takahashi nw={} nh={}'.format(now_w,now_h))
        else:#Tのop
            now_w,now_h = Aoki(now_w,now_h,next_w,next_h,center_w,center_h)
            #print('Aoki nw={} nh={}'.format(now_w,now_h))
        if  0 > now_w or now_w >= W or 0 > now_h or now_h >= H:
            return False
    return True

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

for i in range(N):
    op.append(S[i])
    op.append(T[i])

#print(op)
if isRemain(sr,sc,op):
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
h,w,n = [int(_) for _ in input().split()]
sr,sc = [int(_) for _ in input().split()]
s = input()
t = input()

result = ""
# RL
if s.count('R')+sr - t.count('L') >= w:
  result = 'YES'
else:
  result = 'NO'

print(result)

Submission status: Wrong Answer

Solution code:
import numpy as np
H,W,N =map(int ,raw_input().split())
Sr,Sc =map(int ,raw_input().split())
S = raw_input()
T = raw_input()

S = list(S)
T = list(T)

if Sr % 2 == 0:
    Dr = H/2
else:
    Dr = (H-1)/2
if Sc % 2 == 0:
    Dc = W/2
else:
    Dc = (W-1)/2 
s = 1
for i in range(N):
    if (S[i] == 'D')and(Sr >=Dr):
        Sr = Sr+1
    elif (S[i] == 'U')and(Sr <=Dr):
        Sr = Sr-1
    elif (S[i] == 'R')and(Sc >=Dc):
        Sc = Sc+1
    elif (S[i] == 'L')and(Sc <=Dc):
        Sc = Sc-1
    if (Sr < 0)or(Sr > H-1)or(Sc < 0)or(Sc > W-1):
        s = 0
        break
    if (T[i] == 'D')and(Sr <Dr):
        Sr = Sr+1
    elif (T[i] == 'U')and(Sr >Dr):
        Sr = Sr-1
    elif (T[i] == 'R')and(Sc <Dc):
        Sc = Sc+1
    elif (T[i] == 'L')and(Sc >Dc):
        Sc = Sc-1
    
    if (Sr < 0)or(Sr > H-1)or(Sc < 0)or(Sc > W-1):
        s = 0
        break
if s ==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()

# U or D

ans = 'YES'
uf = df = lf = rf = us = ds = ls = rs = 0
for i in range(N):
    if S[i] == 'U':
        uf += 1
    if S[i] == 'D':
        df += 1
    if S[i] == 'L':
        lf += 1
    if S[i] == 'R':
        rf += 1
    
    if uf - min(ds, H-sr) >= sr:
        ans = 'NO'
        break
    if df - min(us, sr-1) >= (H-sr+1):
        ans = 'NO'
        break
    if lf - min(rs, W-sc) >= sc:
        ans = 'NO'
        break
    if rf - min(ls, sc-1) >= (W-sc+1):
        ans = 'NO'
        break
    
    if T[i] == 'U':
        us += 1
    if T[i] == 'D':
        ds += 1
    if T[i] == 'L':
        ls += 1
    if T[i] == 'R':
        rs += 1

print(ans)

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
r, c = map(int, input().split())
s = input()
t = input()
ans = 0

L_s, L_t = s.count('L'), t.count('L')
R_s, R_t = s.count('R'), t.count('R')
U_s, U_t = s.count('U'), t.count('U')
D_s, D_t = s.count('D'), t.count('D')

if R_s-L_t+c > w:
    ans += 1
if R_t-L_s+c < 0:
    ans += 1
if D_t-U_s+r < 0:
    ans += 1
if D_s-U_t+r > h:
    ans += 1

if ans >= 1:
    ans = 'No'
else:
    ans = 'Yes'
print(ans)

Submission status: Wrong Answer

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

ssr -= 1
ssc -= 1

A = ["L", "R", "U", "D"]

A2 = {"L": -1, "R": 1, "U": -1, "D": 1}

D ={"L": "R", "R": "L", "U": "D", "D": "U"}

ans = "YES"

flg = 0

for a in A :
    
    sr = ssr
    sc = ssc
    
    if flg :
        break
        
    for i in range(N) :
        if (a in ["L", "R"]) and (a == S[i]) :
            sc += A2[a]
            
            if (sc < 0) or (sc >= W) :
                ans = "NO"
                flg = 1
                break
            
            if D[a] == T[i] :
                if (sc == 0) and (D[a] == "L") :
                    continue
                elif (sc == W-1) and (D[a] == "R") :
                    continue
                sc += A2[D[a]]
            
        elif (a in ["U", "D"]) and (a == S[i]) :
            sr += A2[a]
            
            if (sr < 0) or (sr >= W) :
                ans = "NO"
                flg = 1
                break
            
            if T[i] == D[a] :
                if (sr == 0) and (D[a] == "U") :
                    continue
                elif (sr == H-1) and (D[a] == "D") :
                    continue
                sr += A2[D[a]]
        #print((sr,sc))
print(ans)

Submission status: Wrong Answer

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[RevLRUD[T[i]]] + SLRUD[RevLRUD[T[i]]] - TLRUD[ti] <= 0:
        TLRUD[ti] -= 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

sr = [0]*(n+1)
sl = [0]*(n+1)
sd = [0]*(n+1)
su = [0]*(n+1)

for i, si in enumerate(s[::-1]):
    sr[i+1] = sr[i]
    sl[i+1] = sl[i]
    su[i+1] = su[i]
    sd[i+1] = sd[i]
    
    if si == 'R':
        sr[i+1] += 1 
        sl[i+1] = 0
        
    elif si == 'L':
        sl[i+1] += 1 
        sr[i+1] = 0
        
    elif si == 'U':
        su[i+1] += 1
        sd[i+1] = 0
        
    elif si == 'D':
        sd[i+1] += 1 
        su[i+1] = 0
        
sr = sr[::-1]
sl = sl[::-1]
su = su[::-1]
sd = sd[::-1]

for i, (si, ti) in enumerate(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+sl[i+1]+1, lcur - 1)
        
    elif ti == 'L':
        rcur = max(-toright+sr[i+1]+1, rcur - 1)
        
    elif ti == 'U':
        dcur = max(-tobottom+sd[i+1]+1, dcur - 1)
        
    elif ti == 'D':
        ucur = max(-totop+su[i+1]+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:
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 >= H-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: Wrong Answer

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]):
    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)
    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()
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: Wrong Answer

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[RevLRUD[T[i]]] - TLRUD[ti] <= 0:
        TLRUD[ti] -= 1
if flag:
    print('NO')
else:
    print('YES')

Submission status: Wrong Answer

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]):
    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)
    if s == 'U':
        ul += 1
    elif s == 'D':
        dl -= 1
    elif s == 'L':
        ll += 1
    else:
        rl -= 1
    if ul > dl or ll > rl:
        print('NO')
        exit()
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: Wrong Answer

Solution code:

h,w,n = map(int,input().split())
sr,sc = map(int,input().split())
S = input()
T = input()

NO_FLAG = 0

for i in range(n):
    #高橋のターン　駒を出したい（NOにしたい）
    """
    高橋が駒をRに動かしたいとき
    1.Rに動かしたら駒がマスの外にでるとき
    2.残りの文字列で、Rの回数がLの回数よりも多いとき
    """
    if S[i-1] == 'R':
        if sc == w:
            print('NO')
            NO_FLAG = 1
            break
        elif S[i:n-1].count('R') > S[i:n-1].count('L'):
            sc = sc + 1
    elif S[i-1] == 'L':
        if sc == 1:
            print('NO')
            NO_FLAG = 1
            break
        elif S[i:n-1].count('L') > S[i:n-1].count('R'):
            sc = sc - 1   
    elif S[i-1] == 'U':
        if sr == 1:
            print('NO')
            NO_FLAG = 1
            break
        elif S[i:n-1].count('U') > S[i:n-1].count('D'):
            sr = sr - 1    
    elif S[i-1] == 'D':
        if sr == h:
            print('NO')
            NO_FLAG = 1
            break
        elif S[i:n-1].count('D') > S[i:n-1].count('U'):
            sr = sr + 1        
    
    #青木のターン　駒をに残したい（YESにしたい）
    """
    青木が駒をRに動かしたいとき
    1.S[i:n-1]の中にLが含まれるとき
    2.
    
    """
    if T[i-1] == 'R':
        if S[i:n-1].find('L'):
            sc = sc + 1
    elif T[i-1] == 'L':
        if S[i:n-1].find('R'):
            sc = sc - 1
    elif T[i-1] == 'U':
        if S[i:n-1].find('D'):
            sr = sr - 1
    elif T[i-1] == 'D':
        if S[i:n-1].find('U'):
            sr = sr + 1
       
if NO_FLAG == 0:
    print('YES')

Submission status: Wrong Answer

Solution code:
import numpy as np
H,W,N =map(int ,raw_input().split())
Sr,Sc =map(int ,raw_input().split())
S = raw_input()
T = raw_input()

S = list(S)
T = list(T)

Dr = int(H/2) 
Dc = int(W/2) 
s = 1
for i in range(N):
    if (S[i] == 'D')and(Sr >=Dr):
        Sr = Sr+1
    elif (S[i] == 'U')and(Sr <=Dr):
        Sr = Sr-1
    elif (S[i] == 'R')and(Sc >=Dc):
        Sc = Sc+1
    elif (S[i] == 'L')and(Sc <=Dc):
        Sc = Sc-1
    if (Sr < 0)or(Sr > H-1)or(Sc < 0)or(Sc > W-1):
        s = 0
        break
    if (T[i] == 'D')and(Sr <Dr):
        Sr = Sr+1
    elif (T[i] == 'U')and(Sr >Dr):
        Sr = Sr-1
    elif (T[i] == 'R')and(Sc <Dc):
        Sc = Sc+1
    elif (T[i] == 'L')and(Sc >Dc):
        Sc = Sc-1
    
    if (Sr < 0)or(Sr > H-1)or(Sc < 0)or(Sc > W-1):
        s = 0
        break
if s ==0:
    print('NO')
else:
    print('YES')

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
koma = list(map(int, input().split()))
S = input()
T = input()
def judgegame(H, W, N, koma, S, T):
    ue = koma[0]
    shita = koma[0]
    hidari = koma[1]
    migi = koma[1]
    for i in range(N):
        #print([ue, shita, hidari, migi])
        ue += -1 if S[i]=="U" else 0
        if ue <= 0:
            return "NO"
        ue += 1 if T[i]=="D" and ue < H+1 else 0
        if ue <= 0:
            return "NO"
        shita += 1 if S[i]=="D" else 0
        if shita > H:
            return "NO"
        shita += -1 if T[i]=="U" and shita > 1 else 0
        if shita > H:
            return "NO"
        hidari += -1 if S[i]=="L" else 0
        if hidari <= 0:
            return "NO"
        hidari += 1 if T[i]=="R" and hidari < W+1 else 0
        if hidari <= 0:
            return "NO"
        migi += 1 if S[i]=="R" else 0
        if migi > W:
            return "NO"
        migi += -1 if T[i]=="L" and migi > 1 else 0
        if migi > W:
            return "NO"
    return "YES"
print(judgegame(H, W, N, koma, S, T))

Submission status: Wrong Answer

Solution code:
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]):
    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)
    if s == 'U':
        ul += 1
    elif s == 'D':
        dl -= 1
    elif s == 'L':
        ll += 1
    else:
        rl -= 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: Wrong Answer

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

s_count = {'R': 0, 'L': 0, 'U': 0, 'D': 0}
t_count = {'R': 0, 'L': 0, 'U': 0, 'D': 0}
reverse = {'R': 'L', 'L': 'R', 'U': 'D', 'D': 'U'}
dist =  {'R': W - s_c + 1, 'L': s_c, 'U': s_r, 'D': H - s_r + 1}

s_flag = False
for n in range(N):
    s = S[n]
    t = T[n]

    s_count[s] += 1

    if s_count[s] - t_count[reverse[s]] >= dist[s]: 
        s_flag = True
        break

    if t_count[t] + 1 < dist[t]:
        t_count[t] += 1
    
print('NO' if s_flag else 'YES')