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:
import copy
H,W,N = map(int,input().split())
R,C = map(int,input().split())
S = input()
T = input()
a = copy.copy(R)
b = copy.copy(C)
for n in range(N):
        s,t = S[n],T[n]
        if s == 'U':
                R -= 1
                if R < 0:
                        print('N0')
                        exit()
        if t == 'D' and R < H:
                R += 1
R,C = a,b
for n in range(N):
        s,t = S[n],T[n]
        if s == 'L':
                C -= 1
                if C < 0:
                        print('N0')
                        exit()
        if t == 'R' and C < W:
                C += 1
R,C = a,b
for n in range(N):
        s,t = S[n],T[n]
        if s == 'R':
                C += 1
                if C > W:
                        print('N0')
                        exit()
        if t == 'L' and C > 0:
                C -= 1
R,C = a,b
for n in range(N):
        s,t = S[n],T[n]
        if s == 'D':
                R += 1
                if R > H:
                        print('N0')
                        exit()
        if t == 'U' and R > 0:
                R -= 1
print('YES')

Submission status: Wrong Answer

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

r,c = map(int,input().split())
S = input()
T = input()

rr = r
cc = c
for i in range(N):
    if S[i] == 'L':
        cc -= 1
    if cc < 1:
        print('NO')
        exit()
    if T[i] == 'R':
        if cc < W:
            cc += 1

rr = r
cc = c 
for i in range(N):
    if S[i] == 'U':
        rr -= 1
    if rr < 1:
        print('NO')
        exit()

    if T[i] == 'D':
        if rr < H:
            rr += 1
rr = r
cc = c
for i in range(N):
    if S[i] == 'R':
        cc += 1
    if cc > W:
        print('NO')
        exit()
    if T[i] == 'L':
        if cc > 1:
            cc -= 1

rr = r
cc = c
for i in range(N):
    if S[i] == 'D':
        rr += 1
    if cc > H:
        print('NO')
        exit()

    if T[i] == 'U':
        if rr > 1:
            rr -= 1

print('YES')

Submission status: Wrong Answer

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = list(input())
T = list(input())
ans = 0
for n in range(N):
  ans += 1
  if S[n] == 'L' and sc-1 <= (H-1)/2:
    sc -=1
  elif S[n] == 'R' and sc+1 >= (H-1)/2:
    sc += 1
  elif S[n] == 'U' and sr-1 <= (W-1)/2:
    sr -= 1
  elif S[n] == 'D' and sr+1 >= (W-1)/2:
    sr += 1
  if sr < 0 or sr > H or sc < 0 or sc > W:
    break;
  if T[n] == 'L' and sc-1 >= (H-1)/2:
    sc -=1
  elif T[n] == 'R' and sc+1 <= (H-1)/2:
    sc += 1
  elif T[n] == 'U' and sr-1 >= (W-1)/2:
    sr -= 1
  elif T[n] == 'D' and sr+1 <= (W-1)/2:
    sr += 1
  if sr < 0 or sr > H or sc < 0 or sc > W:
    break;
  elif n == N-1:
    ans = -1
if ans == -1:
  print('YES')
else:
  print('NO')

Submission status: Wrong Answer

Solution code:
H,W,N = map(int, input().split())
now = [int(i) for i in input().split()]
S = input()
T = input()
flag = True
SM = [now[0],now[1]]
TM = [now[0],now[1]]
 
for n in range(N):
    if S[n] == 'L':
            SM[1] -= 1
    if S[n] == 'R':
            SM[1] += 1 
    if S[n] == 'U':
            SM[0] -= 1 
    if S[n] == 'D':
            SM[0] += 1
    if T[n] == 'L':
            TM[1] -= 1
    if T[n] == 'R':
            TM[1] += 1 
    if T[n] == 'U':
            TM[0] -= 1 
    if T[n] == 'D':
            TM[0] += 1
 
sm = [0,0,0,0]
if W-SM[1] >= SM[1]-1:
    sm[0] = 1
if W-SM[1] < SM[1]-1:
    sm[1] = 1
if H-SM[0] <= SM[0]-1:
    sm[2] = 1
if H-SM[0] < SM[0]-1:
    sm[3] = 1
 
tm = [0,0,0,0]
if W-TM[1] >= TM[1]-1:
    tm[0] = 1
if W-TM[1] <= TM[1]-1:
    tm[1] = 1
if H-TM[0] <= TM[0]-1:
    tm[2] = 1
if H-TM[0] <= TM[0]-1:
    tm[3] = 1
 
for n in range(N):
    if S[n] == 'L':
        if sm[0] == 1:
            now[1] -= 1
    if S[n] == 'R':
        if sm[1] == 1:
            now[1] += 1 
    if S[n] == 'U':
        if sm[2] == 1:
            now[0] -= 1 
    if S[n] == 'D':
        if sm[3] == 1:
            now[0] += 1
    #print(now)
 
    if now[0]*now[1] == 0 or now[0] == H+1 or now[1] == W+1 :
        flag = False
        break
    
    if T[n] == 'L':
        if tm[1] == 0:
            now[1] -= 1
    if T[n] == 'R':
        if tm[2] == 0:
            now[1] += 1 
    if T[n] == 'U':
        if tm[2] == 0:
            now[0] -= 1 
    if T[n] == 'D':
        if tm[3] == 0:
            now[0] += 1
    #print(now)
 
    if now[0]*now[1] == 0 or now[0] == H+1 or now[1] == W+1 :
        flag = False
        break
 
if flag:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

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

for i in range(N):
    s = S[i]
    if  (s == 'L') and (W/2 > x):
        x -= 1
    elif (s == 'R') and (x > W/2):
        x += 1
    elif (s == 'U') and (H/2 > y):
        y -= 1
    elif (s == 'D') and (y > H/2):
        y += 1       
    
    t = T[i]
    if  (t == 'L')  and (x > W/2):
        x -= 1
    elif (t == 'R') and (W/2 > x):
        x += 1
    elif (t == 'U') and (y > H/2):
        y -= 1
    elif (t == 'D') and (H/2 > y):
        y += 1

    if (y > H) or (1 > y) or (x > W) or (1 > x):
        print('NO')
        break
else:
    print('YES')

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/agc033
# -*- coding: utf-8 -*-

h,w,n = map(int,input().split())
current = list(map(int,input().split()))
s = list(input())
t = list(input())

U = current[0]
D = h-current[0]+1
L = current[1]
R = w-current[1]+1

win="YES"
for i in range(n):
        sU = U- s[0:i+1].count("U") + t[0:i+1].count("U")
        sD = D-s[0:i+1].count("D") + t[0:i+1].count("D")
        sL = L-s[0:i+1].count("L") + t[0:i+1].count("L")
        sR = R-s[0:i+1].count("R") + t[0:i+1].count("R")

        if sU==0 or sD==0 or sL==0 or sR==0:
                win="NO"
                break

print(win)

Submission status: Wrong Answer

Solution code:
h, w, n = (int(_) for _ in input().split())
r, c = (int(_) for _ in input().split())
remove = input()
keep = input()


rtoedge = w - c
ltoedge = w - 1
utoedge = h - 1
dtoedge = h - r
edge = {'R':w - c,'L':w - 1, 'U':h - 1, 'D':h - r}
found = True
if rtoedge > ltoedge:
    minhor = 'L'
    opphor = 'R'
    hormoves = ltoedge
else:
    minhor = 'R'
    opphor = 'L'
    hormoves = rtoedge

if utoedge > dtoedge:
    minver = 'D'
    oppver = 'U'
    vermoves = dtoedge
else:
    minver = 'U'
    oppver = 'D'
    vermoves = utoedge
    
removes = {'R':0,'L':0,'U':0,'D':0}
keepmoves = {'R':0,'L':0,'U':0,'D':0}
opp = [('R','L'),('U', 'D')]
total = {'R':0,'L':0,'U':0,'D':0}
for move in remove:
    removes[move] += 1
    
for move in keep:
    keepmoves[move] += 1
    
if removes[minhor] > hormoves and removes[minhor] > keepmoves[opphor]:
    print("YES")
elif removes[minver] > vermoves and removes[minver] > keepmoves[oppver]:
    print("YES")
else:
    for i in range(n):
        rmove = remove[i]
        kmove = keep[i]
        total[rmove] += 1
        for key in total:
            if total[key] > edge[key]:
                print("YES")
                found = False
                break
        if rtoedge != 0 and kmove == 'R':
            total[kmove] -= 1
        elif ltoedge != 0 and kmove == 'L':
            total[kmove] -= 1
        elif utoedge != 0 and kmove == 'U':
            total[kmove] -= 1
        elif dtoedge != 0 and kmove == 'D':
            total[kmove] -= 1
    
if not found:
    print("NO")

Submission status: Wrong Answer

Solution code:
# 変更後2 後ろから数える　デバッグ用

#入力1
# H=2
# W=3
# N=3
# init_r=2
# init_c=2
# S="RRL"
# T="LUD"

# 入力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()

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

print(r,l,u,d)
ans = "YES"

for i in range(N):
    print(N-1-i)
#     killer's turn
    t = N-1-i
    direction = S[t]
    if direction == "R":
        r -=1
        if r == 1:
            ans = "NO"
            break
    elif direction == "L":
        l +=1
        if l == W:
            ans = "NO"
            break
    elif direction == "U":
        u +=1
        if u == H:
            ans = "NO"
            break
    elif direction == "D":
        d -=1
        if d == 1:
            ans = "NO"
            break
    if r <= l:
        ans = "NO"
        break    
    if d <= u:
        ans = "NO"
        break
        
#     3 >= 2
# 2 <= 3
#     print(r,l,u,d)
#     print(r,l)

    #savor's turn
    direction = T[t]
    if direction == "R" and l != 0:
        l -= 1
    elif direction == "L" and r != W+1:
        r += 1
    elif direction == "U" and d != H+1:
        d += 1
    elif direction == "D" and u != 0:
        u -= 1
#     print(r,l,u,d)
#     print(r,l)

if init_c <= l or init_c >= r:
    ans = "NO"

        
if init_r <= u or init_r >= d:
    ans = "NO"




    
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
        right[i+1] = right[i]
        up[i+1] = up[i]
        down[i+1] = down[i]
        maxL = max(maxL, left[i+1])
    elif S[i] == "R":
        right[i+1] = right[i] + 1
        up[i+1] = up[i]
        down[i+1] = down[i]
        left[i+1]  = left[i]
        maxR = max(maxR, right[i+1])
    elif S[i] == "U":
        up[i+1] = up[i] + 1
        right[i+1] = right[i]
        down[i+1] = down[i]
        left[i+1]  = left[i]
        maxU = max(maxU, up[i+1])
    else:
        down[i+1] = down[i] + 1
        right[i+1] = right[i]
        up[i+1] = up[i]
        left[i+1]  = left[i]
        maxD = max(maxD, down[i+1])
    if T[i] == "L":
        right[i+1] = max(right[i] - 1, -sc + 1)
    elif T[i] == "R":
        left[i+1] = max(left[i] - 1, sc - W)
    elif T[i] == "U":
        down[i+1] = max(down[i] - 1, -sr + 1)
    else:
        up[i+1] = max(up[i] - 1, sr - H)
 
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())
y, x = map(int, input().split())
S = input()
T = input()

for i in range(N):
    s = S[i]
    if  (s == 'L') and (W/2 > x):
        x -= 1
    elif (s == 'R') and (x > W/2):
        x += 1
    elif (s == 'U') and (H/2 > y):
        y -= 1
    elif (s == 'D') and (y > H/2):
        y += 1       
    
    t = T[i]
    if  (s == 'L')  and (x > W/2):
        x -= 1
    elif (s == 'R') and (W/2 > x):
        x += 1
    elif (s == 'U') and (y > H/2):
        y -= 1
    elif (s == 'D') and (H/2 > y):
        y += 1

    if (y > H) or (1 > y) or (x > W) or (1 > x):
        print('NO')
        break
else:
    print('YES')

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
Max = sys.maxsize
def l():
    return list(map(int,input().split()))
def m():
    return map(int,input().split())
def s(x):
    a = []
    aa = x[i]
    su = 1
    for i in range(len(x)-1):
        if aa == x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x):
    return " ".join(map(str,x))

h,w,n = m()

s1,s2 = m()


r = w-s1
l = s1 
u = h - s2
d = s2 

S = list(input())

T = list(input())

k1 = [0,0,0,0]

k2 = [0,0,0,0]

for i in range(n):
    if S[i] == "R":
        k1[0] += 1
    elif S[i] == "L":
        k1[1] += 1
    elif S[i] == "U":
        k1[2] += 1
    else:
        k1[3] += 1
    if T[i] == "R":
        k2[0] += 1
    elif T[i] == "L":
        k2[1] += 1
    elif T[i] == "U":
        k2[2] += 1
    else:
        k2[3] += 1
if k1[1] - k2[0] > l or k1[0] - k2[1] > r or k1[2] - k2[3] > u or k1[3] - k2[2] > d:
    print("NO")
    exit()
for i in range(n):
    if S[i] == "R":
        r -= 1
        l += 1
        k1[0] -= 1
    if r < 0:
        print("NO")
        exit()
    if T[i] ==  "L":
        if l != 0 or r != w - 1:
            r += 1
            l -= 1
        k2[1] -= 1

r = w-s1
l = s1

for i in range(n):
    if S[i] == "L":
        l -= 1
        r +=1
        k1[1] -= 1
    if l < 0:
        print("NO")
        exit()
    if T[i] ==  "R":
        if r != 0 or l != w - 1:
            r -= 1
            l += 1
        k2[0] -= 1


for i in range(n):
    if S[i] == "U":
        d += 1
        u -= 1
        k1[3] -= 1
    if u < 0:
        print("NO")
        exit()
    if T[i] == "D":
        if d != 0 or u != h - 1:
            u += 1
            d -= 1
        k2[2] -= 1

u = h - s2
d = s2
for i in range(n):
    if S[i] == "D":
        u += 1
        d -= 1
        k1[2] -= 1
    if d < 0:
        print("NO")
        exit()
    if T[i] == "U":
        if u != 0 or d != h - 1:
            d += 1
            u -= 1
        k2[3] -= 1

print("YES")

Submission status: Wrong Answer

Solution code:
# coding: utf-8
LRUD = ['L', 'R', 'U', 'D']
h, w, n = list(map(int, input().split()))
r, c = list(map(int, input().split()))
r -= 1
c -= 1
s = list(map(LRUD.index, input()))
t = list(map(LRUD.index, input()))

def move(command, policy):
    global r, c
    row = r
    col = c
    dr1 = min(row, h - row - 1)
    dc1 = min(col, w - col - 1)
    result = True
    if command == 0:
        result = col > 0
        col -= 1
    elif command == 1:
        result = col < w - 1
        col += 1
    elif command == 2:
        result = row > 0
        row -= 1
    elif command == 3:
        result = row < h - 1
        row += 1
    dr2 = min(row - 1, h - row - 1)
    dc2 = min(col - 1, w - col - 1)
    #print("{}:({},{})->({},{}) dr:{}->{} dc:{}->{}".format(LRUD[command], r, c, row, col, dr1, dr2, dc1, dc2))
    if policy:
        if dr1 < dr2 or dc1 < dc2:
            r = row
            c = col
        else:
            print("reject")
    else:
        if dr1 > dr2 or dc1 > dc2:
            r = row
            c = col
        else:
            print("reject")
    return result

result = True
i = 0
while result and i < n:
    i += 1
    result = move(s.pop(0), False)
    if result:
        result = move(t.pop(0), True)

if result:
    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()

cU=0
cD=0
cL=0
cR=0

#yoyuu
yL=r-1
yR=W-r
yU=c-1
yD=N-c

for i in range(N):
    if S[i]=="U":
        cU=cU+1
    if S[i]=="D":
        cD=cD+1
    if S[i]=="L":
        cL=cL+1
    if S[i]=="R":
        cR=cR+1

    if T[i]=="U":
        cU=cU-1
    if T[i]=="D":
        cD=cD-1
    if T[i]=="L":
        cL=cL-1
    if T[i]=="R":
        cR=cR-1

for i in range(1):
    if yL<cL:
        print("NO")
        break
    if yR<cR:
        print("NO")
        break
    if yU<cU:
        print("NO")
        break
    if yD<cD:
        print("NO")
        break
    else:
        print("YES")
        break

Submission status: Wrong Answer

Solution code:
h,w,n = map(int,input().split())
sr,sc = map(int,input().split())
s = input()
t = input()
def taka(k,pos):
    if k=='U' and pos[0]-1<=h-pos[0]+1:
        pos[0] -= 1
    if k=='D' and h-pos[0]<=pos[0]:
        pos[0] += 1
    if k=='R' and w-pos[1]<=pos[1]:
        pos[1] += 1
    if k=='L' and pos[1]-1<=w-pos[1]+1:
        pos[1] -= 1
    return pos
def ao(k,pos):
    if k=='U' and pos[0]-1>=h-pos[0]+1:
        pos[0] -= 1
    if k=='D' and h-pos[0]>=pos[0]:
        pos[0] += 1
    if k=='R' and w-pos[1]>=pos[1]:
        pos[1] += 1
    if k=='L' and pos[1]-1>=w-pos[1]+1:
        pos[1] -= 1
    return pos
pos = [sr,sc]
for i in range(n):
    pos = taka(s[i],pos)
    if pos[0]==0 or pos[1]==0 or pos[0]==h+1 or pos[1]==w+1:
        print('NO')
        exit()
    pos = ao(t[i],pos)
print('YES')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
s_dict = defaultdict(lambda: 0)
t_dict = defaultdict(lambda: 0)

h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = list(str(input()))
t = list(str(input()))

for i in range(n):
    s_dict[s[i]] += 1
    t_dict[t[i]] += 1

# 落ちるパターン
# 駒が端にあり、高橋くんが場外に移動させることができる場合、落ちる
ans = "NO"
first = False
if sr == 1 and s_dict["L"] > 0 or sr == w and s_dict["R"] > 0 or sc == 1 and s_dict["U"] > 0 or sc == h and s_dict["D"] > 0:
    first = True

s_sum = defaultdict(lambda: 0)
t_sum = defaultdict(lambda: 0)
if not first:
    # それぞれの累積和でmaxを考える
    for i in range(n):
        s_sum[s[i]] += 1
        # 高橋くんは落とさないようにする制約がある
        # 何もしないこともある
        if t[i] == "L" and t_sum["L"] + 1 - s_sum["R"] > sr - 1:
            pass
        elif t[i] == "R" and t_sum["R"] + 1 - s_sum["L"] > w - sr:
            pass
        elif t[i] == "U" and t_sum["U"] + 1 - s_sum["D"] > sc - 1:
            pass
        elif t[i] == "D" and t_sum["D"] + 1 - s_sum["U"] > h - sc:
            pass
        else:
            t_sum[t[i]] += 1

        if sr <= s_sum["L"] - t_sum["R"]:
            break
        elif w - sr < s_sum["R"] - t_sum["L"]:
            break
        elif sc <= s_sum["U"] - t_sum["D"]:
            break
        elif h - sc < s_sum["D"] - t_sum["U"]:
            break
        if i == n-1:
            ans = "YES"
print(ans)

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/agc033
# -*- coding: utf-8 -*-

h,w,n = map(int,input().split())
current = list(map(int,input().split()))
s = list(input())
t = list(input())

U = current[0]
D = h-current[0]+1
L = current[1]
R = w-current[1]+1

win="YES"
for i in range(n):
        sU = s[0:i+1].count("U")
        sD = s[0:i+1].count("D")
        sL = s[0:i+1].count("L")
        sR = s[0:i+1].count("R")

        tU = t[0:i+1].count("U")
        tD = t[0:i+1].count("D")
        tL = t[0:i+1].count("L")
        tR = t[0:i+1].count("R")

        # print(U,D,L,R)
        # print(s[0:i+1],t[0:i+1])
        # print(sU,sD,sL,sR)
        # print(tU,tD,tL,tR)

        if (U-sU+tD)==0 or (D-sD+tU)==0 or (L-sL+tR)==0 or (R-sR+tL)==0:
                win="NO"
                break

print(win)

Submission status: Wrong Answer

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




S_power = [[0, 0, 0, 0] for i in range(N+1)] #L, R, U, D
for i in range(N):
    if S[i] == "L":
        S_power[i][0] += 1
    elif S[i] == "R":
        S_power[i][1] += 1
    elif S[i] == "U":
        S_power[i][2] += 1
    else:
        S_power[i][3] += 1

T_power = [[0, 0, 0, 0] for i in range(N+1)]
for i in range(N):
    if T[i] == "L":
        T_power[i][0] += 1
    elif T[i] == "R":
        T_power[i][1] += 1
    elif T[i] == "U":
        T_power[i][2] += 1
    else:
        T_power[i][3] += 1

for j in range(4):
    for i in range(N-1):
        S_power[i+1][j] += S_power[i][j]
for j in range(4):
    for i in range(N-1):
        T_power[i+1][j] += T_power[i][j]
T_power.append([0, 0, 0, 0])



flag = 1
power_diff = []
for i in range(N):
    power_diff.append(S_power[i][1]-T_power[i-1][0])
power_diff_max = power_diff[::-1]
for i in range(N-1):
    power_diff_max[i+1] = max(power_diff_max[i+1], power_diff_max[i])
power_diff_max.reverse()

sr = r
for i in range(N):
    if S[i] == "L":
        sr -= 1
    if sr <= 0:
        flag = 0
        break
    if T[i] == "R" and power_diff_max[i] + sr + 1 <= W:
        sr += 1






power_diff = []
for i in range(N):
    power_diff.append(S_power[i][0]-T_power[i-1][1])
power_diff_max = power_diff[::-1]
for i in range(N-1):
    power_diff_max[i+1] = max(power_diff_max[i+1], power_diff_max[i])
power_diff_max.reverse()

sr = r
for i in range(N):
    if S[i] == "R":
        sr += 1
    if sr > W:
        flag = 0
        break
    if T[i] == "L" and power_diff_max[i] + 2 <= sr:
        sr -= 1







power_diff = []
for i in range(N):
    power_diff.append(S_power[i][2]-T_power[i-1][3])
power_diff_max = power_diff[::-1]
for i in range(N-1):
    power_diff_max[i+1] = max(power_diff_max[i+1], power_diff_max[i])
power_diff_max.reverse()

sr = r
for i in range(N):
    if S[i] == "U":
        sr -= 1
    if sr <= 0:
        flag = 0
        break
    if T[i] == "D" and power_diff_max[i] + sr + 1 <= H:
        sr += 1






power_diff = []
for i in range(N):
    power_diff.append(S_power[i][3]-T_power[i-1][2])
power_diff_max = power_diff[::-1]
for i in range(N-1):
    power_diff_max[i+1] = max(power_diff_max[i+1], power_diff_max[i])
power_diff_max.reverse()

sr = r
for i in range(N):
    if S[i] == "D":
        sr += 1
    if sr > H:
        flag = 0
        break
    if T[i] == "U" and power_diff_max[i] + 2 <= sr:
        sr -= 1


if flag == 1:
    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()
for n in range(N):
        s,t = S[n],T[n]
        if s == 'U':
                R -= 1
                if R < 0:
                        print('N0')
                        exit()
        if t == 'D' and R < H:
                R += 1
for n in range(N):
        s,t = S[n],T[n]
        if s == 'L':
                C -= 1
                if C < 0:
                        print('N0')
                        exit()
        if t == 'R' and C < W:
                C += 1
for n in range(N):
        s,t = S[n],T[n]
        if s == 'R':
                C += 1
                if C > W:
                        print('N0')
                        exit()
        if t == 'L' and C > 0:
                C -= 1
for n in range(N):
        s,t = S[n],T[n]
        if s == 'D':
                R += 1
                if R > H:
                        print('N0')
                        exit()
        if t == 'U' and R > 0:
                R -= 1
print('YES')

Submission status: Wrong Answer

Solution code:
from collections import*
h,w,n=map(int,input().split())
x,y=map(int,input().split())
u=input()
m=input()
a11,a21,a31,a41=float("inf"),float("inf"),float("inf"),float("inf")
a12,a22,a32,a42=-float("inf"),-float("inf"),-float("inf"),-float("inf")
for i in range(1,n):
    s=Counter(u[:i])
    t=Counter(m[:i])
    a11=max(a11,s["R"]-t["L"])
    a12=min(a22,s["R"]-t["L"])
    a21=max(a21,s["L"]-t["R"])
    a22=min(a22,s["L"]-t["R"])
    a31=max(a31,s["U"]-t["D"])
    a32=min(a31,s["U"]-t["D"])
    a41=max(a31,s["D"]-t["U"])
    a42=min(a42,s["D"]-t["U"])

if all([1<=(a11+x)<=w,1<=(a21+x)<=w,1<=(a31+y)<=h,1<=(a41+y)<=h,1<=(a12+x)<=w,1<=(a22+x)<=w,1<=(a32+y)<=h,1<=(a42+y)<=h]):
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
def cheak(s, t):
    if 'L' in s:
        r, c = 0, -1
    elif 'R' in s:
        r, c = 0, 1
    elif 'U' in s:
        r, c = -1, 0
    elif 'D' in s:
        r, c = 1, 0
        
    if 'L' in t:
        c += -1
    elif 'R' in t:
        c += 1
    elif 'U' in t:
        r += -1
    elif 'D' in t:
        r += 1
    return r, c
    
def answer(h, w, r, c):
    if (r>h) or (r<0):
        return 1
    elif (c>w) or (c<0):
        return 1
    else:
        return 0
    
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:
    print('Yes')
u, v = cheak(s[0], str(0))
r += u
c += v
print(h, w, r, c)
if answer(h, w, r, c) == 1:
    print('Yes')
else:
    for i in range(1, n):
        u, v = cheak(s[i], t[i-1])
        r += u
        c += v
        print(h, w, r, c)
        if answer(h, w, r, c) == 1:
            print('Yes')
            break
if answer(h, w, r, c) != 1:
    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()




S_power = [[0, 0, 0, 0] for i in range(N+1)] #L, R, U, D
for i in range(N):
    if S[i] == "L":
        S_power[i][0] += 1
    elif S[i] == "R":
        S_power[i][1] += 1
    elif S[i] == "U":
        S_power[i][2] += 1
    else:
        S_power[i][3] += 1

T_power = [[0, 0, 0, 0] for i in range(N+1)]
for i in range(N):
    if T[i] == "L":
        T_power[i][0] += 1
    elif T[i] == "R":
        T_power[i][1] += 1
    elif T[i] == "U":
        T_power[i][2] += 1
    else:
        T_power[i][3] += 1

for j in range(4):
    for i in range(N-1):
        S_power[i+1][j] += S_power[i][j]
for j in range(4):
    for i in range(N-1):
        T_power[i+1][j] += T_power[i][j]
T_power.append([0, 0, 0, 0])



flag = 1
power_diff = []
for i in range(N):
    power_diff.append(S_power[i][1]-T_power[i-1][0])
power_diff_max = power_diff[::-1]
for i in range(N-1):
    power_diff_max[i+1] = max(power_diff_max[i+1], power_diff_max[i])
power_diff_max.reverse()

sr = r
for i in range(N):
    print(sr)
    if S[i] == "L":
        sr -= 1
    if sr <= 0:
        flag = 0
        break
    if T[i] == "R" and power_diff_max[i] + sr + 1 <= W:
        sr += 1






power_diff = []
for i in range(N):
    power_diff.append(S_power[i][0]-T_power[i-1][1])
power_diff_max = power_diff[::-1]
for i in range(N-1):
    power_diff_max[i+1] = max(power_diff_max[i+1], power_diff_max[i])
power_diff_max.reverse()

sr = r
for i in range(N):
    if S[i] == "R":
        sr += 1
    if sr > W:
        flag = 0
        break
    if T[i] == "L" and power_diff_max[i] + 2 <= sr:
        sr -= 1







power_diff = []
for i in range(N):
    power_diff.append(S_power[i][2]-T_power[i-1][3])
power_diff_max = power_diff[::-1]
for i in range(N-1):
    power_diff_max[i+1] = max(power_diff_max[i+1], power_diff_max[i])
power_diff_max.reverse()

sr = r
for i in range(N):
    if S[i] == "U":
        sr -= 1
    if sr <= 0:
        flag = 0
        break
    if T[i] == "D" and power_diff_max[i] + sr + 1 <= H:
        sr += 1






power_diff = []
for i in range(N):
    power_diff.append(S_power[i][3]-T_power[i-1][2])
power_diff_max = power_diff[::-1]
for i in range(N-1):
    power_diff_max[i+1] = max(power_diff_max[i+1], power_diff_max[i])
power_diff_max.reverse()

sr = r
for i in range(N):
    if S[i] == "D":
        sr += 1
    if sr > H:
        flag = 0
        break
    if T[i] == "U" and power_diff_max[i] + 2 <= sr:
        sr -= 1


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

Submission status: Wrong Answer

Solution code:
h, w, n = [int(item) for item in input().split()]
sr, sc = [int(item) for item in input().split()]
s = input().rstrip()
t = input().rstrip()
direction = "LRUD"
reverse = "RLDU"

to_be_moved = [[0] * n for _ in range(4)]
for i, item in enumerate(s[::-1]):
    for j in range(4):
        drc = direction.index(item)
        if j == drc:
            if i != 0:
                to_be_moved[j][n-1-i] = to_be_moved[j][n-i] + 1
            else:
                to_be_moved[j][n-1-i] = 1
        else:
            if i != 0:
                to_be_moved[j][n-1-i] = to_be_moved[j][n-i]
            else:
                to_be_moved[j][n-1-i] = 0

# l, r, u, d
move_limit = [sr, w-sr+1, sc, h-sc+1]
rev_move_limit = [-(w-sr+1), -sr, -(h-sc+1), -sc]
max_move = [0] * 4
move = [0] * 4
for i in range(n):
    # Kill koma
    move[direction.index(s[i])] += 1
    max_move[direction.index(s[i])] = max(move[direction.index(s[i])], max_move[direction.index(s[i])])
    # Save koma
    drc = reverse.index(t[i])
    if(move[drc]-1 > rev_move_limit[drc]):
        move[drc] -= 1

print(max_move)
if max_move[0] >= sr or max_move[1] > w-sr or max_move[2] >= sc or max_move[3] > h-sc:
    print("NO")
else:
    print("YES")

Submission status: Wrong Answer

Solution code:
from collections import*
h,w,n=map(int,input().split())
x,y=map(int,input().split())
u=input()
m=input()
a11,a21,a31,a41=float("inf"),float("inf"),float("inf"),float("inf")
a12,a22,a32,a42=-float("inf"),-float("inf"),-float("inf"),-float("inf")
for i in range(1,n-1):
    s=Counter(u[:i])
    t=Counter(m[:i])
    a11=max(a11,s["R"]-t["L"])
    a12=min(a22,s["R"]-t["L"])
    a21=max(a21,s["L"]-t["R"])
    a22=min(a22,s["L"]-t["R"])
    a31=max(a31,s["U"]-t["D"])
    a32=min(a31,s["U"]-t["D"])
    a41=max(a31,s["D"]-t["U"])
    a42=min(a42,s["D"]-t["U"])

if all([1<=(a11+x)<=w,1<=(a21+x)<=w,1<=(a31+y)<=h,1<=(a41+y)<=h,1<=(a12+x)<=w,1<=(a22+x)<=w,1<=(a32+y)<=h,1<=(a42+y)<=h]):
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:

import sys

H, W, N = map(int, input().rstrip().split(' '))
h, w = map(int, input().rstrip().split(' '))
h -= 1
w -= 1
S = [x for x in input()]
T = [x for x in input()]

d = {'R': [0, 1], 'L': [0, -1], 'U': [-1, 0], 'D': [1, 0]}

for s, t in zip(S, T):
    h += d[s][0]
    w += d[s][1]
    
    if h < 0 or h >= H or w < 0 or w >= W:
        print('NO')
        sys.exit()
        
    h += d[t][0]
    w += d[t][1]
    
    if h < 0 or h >= H or w < 0 or w >= W:
        print('NO')
        sys.exit()
        
print('YES')