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 time
st_time=time.time()

from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
ts=time.time()
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
show_flg=False
#show_flg=True

h,w,n=LI()
b=[h,w,w,h]
r,c=LI()
st=[h-r,c,w-c,r]
s=input()
t=input()
cs=[[0]for i in range(4)]
ct=[[0]for i in range(4)]
di='URLD'
di=dict(zip(list(di),range(4)))
for j,i in di.items():
    for k in range(n):
        cs[i].append(cs[i][-1]+(1 if s[k]==j else 0))
        ct[i].append(ct[i][-1]+(1 if t[k]==j else 0))
ans=False
for i in range(4):
    if max([(j-k)+st[i] for j,k in zip(cs[i],[0]+ct[3-i])])>b[i]:
        ans=True
if ans:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
import time
st_time=time.time()

from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
ts=time.time()
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
show_flg=False
#show_flg=True

h,w,n=LI()
b=[h,w,w,h]
r,c=LI()
st=[r,c,c,r]
s=input()
t=input()
cs=[[0]for i in range(4)]
ct=[[0]for i in range(4)]
di='URLD'
di=dict(zip(list(di),range(4)))
for j,i in di.items():
    for k in range(n):
        cs[i].append(cs[i][-1]+(1 if s[k]==j else 0))
        ct[i].append(ct[i][-1]+(1 if t[k]==j else 0))
ans=False
for i in range(4):
    if max([(j-k)*(-1)**(i//2)+st[i] for j,k in zip(cs[i],[0]+ct[3-i])])>b[i]:
        ans=True
if ans:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
import time
st_time=time.time()

from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
ts=time.time()
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
show_flg=False
#show_flg=True

h,w,n=LI()
b=[h,w,w,h]
r,c=LI()
st=[r,c,c,r]
s=input()
t=input()
cs=[[0]for i in range(4)]
ct=[[0]for i in range(4)]
di='URLD'
di=dict(zip(list(di),range(4)))
for j,i in di.items():
    for k in range(n):
        cs[i].append(cs[i][-1]+(1 if s[k]==j else 0))
        ct[i].append(ct[i][-1]+(1 if t[k]==j else 0))
ans=False
for i in range(4):
    if max([i-j for i,j in zip(cs[i],[0]+ct[3-i])])*(-1)**(i//2)+st[i]>b[i]:
        ans=True
if ans:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

Solution code:
import time
st_time=time.time()

from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
ts=time.time()
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
show_flg=False
#show_flg=True

h,w,n=LI()
b=[h,w,w,h]
r,c=LI()
st=[r,c,c,r]
s=input()
t=input()
cs={}
ct={}
di='URLD'
di=dict(zip(list(di),range(4)))
for j,i in di.items():
    cs[i]=s.count(j)
    ct[i]=t.count(j)
ans=False
for i in range(4):
    if (cs[i]-ct[3-i])*(-1)**(i//2)+st[i]>b[i]:
        ans=True
if ans:
    print('YES')
else:
    print('NO')

Submission status: Wrong Answer

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

choku=list(input())
ao=list(input())
choku=choku[::-1]
ao=ao[::-1]

Dwn=1
Up=H
Lef=1
Rgt=W
#print(Dwn,r,Up,Lef,c,Rgt)
def chokudai(c):
    global Dwn
    global Up
    global Lef
    global Rgt
    if choku[c]=="D":
        Dwn+=1
    if choku[c]=='U':
        Up-=1
    if choku[c]=='L':
        Lef+=1
    if choku[c]=='R':
        Rgt-=1
    if Dwn>Up or Lef>Rgt:
        print("NO")
        exit()
#chokudai(0)

def kensho(k):
    global Dwn
    global Up
    global Lef
    global Rgt
    if ao[k]=="D":
        Dwn=max(1,Dwn-1)
    if ao[k]=="U":
        Up=min(H,Up+1)
    if ao[k]=="R":
        Lef=max(1,Lef-1)
    if ao[k]=="L":
        Rgt=min(W,Rgt+1)
#print(Dwn,r,Up,Lef,c,Rgt)
for i in range(N):
    kensho(i)
    #print(Dwn,r,Up,Lef,c,Rgt)
    chokudai(i)
    #print(Dwn,r,Up,Lef,c,Rgt)

if Dwn<=r and r<=Up and Lef<=c and c<=Rgt:
    print("YES")
else:
    print("NO")
    #print(Dwn,r,Up,Lef,c,Rgt)

Submission status: Wrong Answer

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

choku=list(input())
ao=list(input())
choku=choku[::-1]
ao=ao[::-1]

Dwn=1
Up=H
Lef=1
Rgt=W
#print(Dwn,r,Up,Lef,c,Rgt)
def chokudai(c):
    global Dwn
    global Up
    global Lef
    global Rgt
    if choku[c]=="D":
        Dwn+=1
    if choku[c]=='U':
        Up-=1
    if choku[c]=='L':
        Lef+=1
    if choku[c]=='R':
        Rgt-=1
    if Dwn>Up or Lef>Rgt:
        print("NO")
        exit()
#chokudai(0)

def kensho(k):
    global Dwn
    global Up
    global Lef
    global Rgt
    if ao[k]=="D":
        Dwn=max(1,Dwn-1)
    if ao[k]=="U":
        Up=min(H,Up+1)
    if ao[k]=="R":
        Lef=max(1,Lef-1)
    if ao[k]=="L":
        Rgt=min(W,Rgt+1)
#print(Dwn,r,Up,Lef,c,Rgt)
for i in range(N):
    kensho(i)
    #print(Dwn,r,Up,Lef,c,Rgt)
    chokudai(i)
    #print(Dwn,r,Up,Lef,c,Rgt)

if Dwn<=H-r+1 and H-r+1<=Up and Lef<=c and c<=Rgt:
    print("YES")
else:
    print("NO")
    #print(Dwn,r,Up,Lef,c,Rgt)

Submission status: Wrong Answer

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

choku=list(input())
ao=list(input())
choku=choku[::-1]
ao=ao[::-1]

Dwn=1
Up=H
Lef=1
Rgt=W
#print(Dwn,r,Up,Lef,c,Rgt)
def chokudai(c):
    global Dwn
    global Up
    global Lef
    global Rgt
    if choku[c]=="D":
        Dwn+=1
    if choku[c]=='U':
        Up-=1
    if choku[c]=='L':
        Lef+=1
    if choku[c]=='R':
        Rgt-=1
    if Dwn>Up or Lef>Rgt:
        print("NO")
        exit()
chokudai(0)

def kensho(k):
    global Dwn
    global Up
    global Lef
    global Rgt
    if ao[k]=="U":
        Dwn=max(1,Dwn-1)
    if ao[k]=="D":
        Up=min(H,Up+1)
    if ao[k]=="R":
        Lef=max(1,Lef-1)
    if ao[k]=="L":
        Rgt=min(W,Rgt+1)
#print(Dwn,r,Up,Lef,c,Rgt)
for i in range(1,N):
    kensho(i)
    #print(Dwn,r,Up,Lef,c,Rgt)
    chokudai(i)
    #print(Dwn,r,Up,Lef,c,Rgt)

if Dwn<=r and r<=Up and Lef<=c and c<=Rgt:
    print("YES")
else:
    print("NO")
    #print(Dwn,r,Up,Lef,c,Rgt)

Submission status: Wrong Answer

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

l = 1
r = W
d = 1
u = H
ans = "YES"
for i in range(N-1, -1, -1):
    if T[i] == "L":
        r = min(W, r+1)
    elif T[i] == "R":
        l = max(1, l-1)
    elif T[i] == "D":
        u = min(H, u+1)
    else:
        d = max(1, d-1)
    if S[i] == "L":
        l += 1
    elif S[i] == "R":
        r -= 1
    elif S[i] == "D":
        d += 1
    else:
        u -= 1
    if l > r or d > u:
        ans = "NO"
        break
        
if sr < d or u < sr or sc < l or r < sc:
    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()

l = 1
r = W
d = 1
u = H
ans = "YES"
for i in range(N-1, -1, -1):
    if T[i] == "L":
        r = min(W, r+1)
    elif T[i] == "R":
        l = max(1, l-1)
    elif T[i] == "D":
        u = min(H, u+1)
    else:
        d = min(1, d-1)
    if S[i] == "L":
        l += 1
    elif S[i] == "R":
        r -= 1
    elif S[i] == "D":
        d += 1
    else:
        u -= 1
    if l > r or d > u:
        ans = "NO"
        break
        
if sr < d or u < sr or sc < l or r < sc:
    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()

f = 0
dic = {"L":1, "R":W, "U":H, "D":1}
for i in range(N):
    if T[N-1-i] == "L":
        dic["R"] = min(W, dic["R"]+1)
    elif T[N-1-i] == "R":
        dic["L"] = max(1, dic["L"]-1)
    elif T[N-1-i] == "U":
        dic["D"] = max(1, dic["D"]-1)
    else:
        dic["U"] = min(H, dic["U"]+1)
        
    if S[N-1-i] == "L" or S[N-1-i] == "D":
        dic[S[N-1-i]] += 1
    else:
        dic[S[N-1-i]] -= 1
    if dic["L"] > dic["R"] or dic["D"] > dic["U"]:
        f = 1
        break
        
if f == 0 and dic["L"] <= sc <= dic["R"] and dic["D"] <= sr <= dic["U"]:
    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()

f = 0
dic = {"L":1, "R":W, "U":H, "D":1}
for i in range(N):
    if i != 0:
        if T[N-1-i] == "L":
            dic["R"] = min(W, dic["R"]+1)
        elif T[N-1-i] == "R":
            dic["L"] = max(1, dic["L"]-1)
        elif T[N-1-i] == "U":
            dic["D"] = max(1, dic["D"]-1)
        else:
            dic["U"] = min(H, dic["U"]+1)
    if S[N-1-i] == "L" or S[N-1-i] == "D":
        dic[S[N-1-i]] += 1
    else:
        dic[S[N-1-i]] -= 1
    if dic["L"] > dic["R"] or dic["D"] > dic["U"]:
        f = 1
        break
        
if f == 0 and dic["L"] <= sr <= dic["R"] and dic["D"] <= sc <= dic["U"]:
    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()

f = 0
dic = {"L":1, "R":W, "U":H, "D":1}
for i in range(N):
    if i != 0:
        if T[N-1-i] == "L":
            dic["R"] = min(W, dic["R"]+1)
        elif T[N-1-i] == "R":
            dic["L"] = max(1, dic["L"]-1)
        elif T[N-1-i] == "U":
            dic["D"] = max(1, dic["D"]-1)
        else:
            dic["U"] = min(H, dic["U"]+1)
    if S[N-1-i] == "L" or S[N-1-i] == "D":
        dic[S[N-1-i]] += 1
    else:
        dic[S[N-1-i]] -= 1
    if dic["L"] == W+1 or dic["R"] == 0 or dic["D"] == H+1 or dic["U"] == 0:
        f = 1
        break
        
if f == 0 and dic["L"] <= sr <= dic["R"] and dic["D"] <= sc <= dic["U"]:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
h,w,n=[int(j) for j in input().split()]
R,C=[int(j) for j in input().split()]
s=input()
t=input()

#LR
x=C
l,r=1,w
for i in range(n)[::-1]:
    if t[i]=="R":
        l-=1
    elif t[i]=="L":
        r+=1
    if l==0:
        l=1
    if r>w:
        r=w
    if s[i]=="R":
        r-=1
    elif s[i]=="L":
        l+=1
    if  l>r:
        print("NO")
        exit()
if not l<=x<=r:
    print("NO")
    exit()
#UD
x=R
l,r=1,h
for i in range(n)[::-1]:
    if t[i]=="U":
        l-=1
    elif t[i]=="D":
        r+=1
    if l==0:
        l=1
    if r>h:
        r=h
    if s[i]=="U":
        r-=1
    elif s[i]=="D":
        l+=1
    if l>r:
        print("NO")
        exit()
if not l<=x<=r:
    print("NO")
    exit()
print("YES")

Submission status: Wrong Answer

Solution code:
h,w,n=[int(j) for j in input().split()]
R,C=[int(j) for j in input().split()]
s=input()
t=input()

#LR
x=C
l,r=1,w
for i in range(n)[::-1]:
    if t[i]=="R":
        l-=1
    elif t[i]=="L":
        r+=1
    if s[i]=="R":
        r-=1
    elif s[i]=="L":
        l+=1
    if  l>r:
        print("NO")
        exit()
if not l<=x<=r:
    print("NO")
    exit()
#UD
x=R
l,r=1,h
for i in range(n)[::-1]:
    if t[i]=="U":
        l-=1
    elif t[i]=="D":
        r+=1
    if s[i]=="U":
        r-=1
    elif s[i]=="D":
        l+=1
    if l>r:
        print("NO")
        exit()
if not l<=x<=r:
    print("NO")
    exit()
print("YES")

Submission status: Wrong Answer

Solution code:
h,w,n=[int(j) for j in input().split()]
R,C=[int(j) for j in input().split()]
s=input()
t=input()

#LR
x=C
l,r=1,w
for i in range(n)[::-1]:
    if t[i]=="R":
        l-=1
    elif t[i]=="L":
        r+=1

    if s[i]=="R":
        r-=1
    elif s[i]=="L":
        l+=1
    if not l<=x<=r:
        print("NO")
        exit()
    


#UD
x=R
l,r=1,h
for i in range(n)[::-1]:
    if t[i]=="U":
        l-=1
    elif t[i]=="D":
        r+=1
    if s[i]=="U":
        r-=1
    elif s[i]=="D":
        l+=1
    if not l<=x<=r:
        print("NO")
        exit()


print("YES")

Submission status: Wrong Answer

Solution code:
h,w,n=[int(j) for j in input().split()]
r,c=[int(j) for j in input().split()]
s=input()
t=input()

#R
x=c
for i in range(n):
    if s[i]=="R":
        x+=1
    if t[i]=="L" and x>1:
        x-=1
    if not 1<=x<=w:
        print("NO")
        exit()

#L
x=c
for i in range(n):
    if s[i]=="L":
        x-=1
    if t[i]=="R" and x<w:
        x+=1
    if not 1<=x<=w:
        print("NO")
        exit()

#U
x=r
for i in range(n):
    if s[i]=="U":
        x+=1
    if t[i]=="D" and x>1:
        x-=1
    if not 1<=x<=h:
        print("NO")
        exit()

#D
x=r
for i in range(n):
    if s[i]=="D":
        x-=1
    if t[i]=="U" and x<h:
        x+=1
    if not 1<=x<=h:
        print("NO")
        exit()
print("YES")

Submission status: Wrong Answer

Solution code:
h,w,n=[int(j) for j in input().split()]
r,c=[int(j) for j in input().split()]
s=input()
t=input()

#R
x=c
for i in range(n):
    if s[i]=="R":
        x+=1
    if t[i]=="L" and x>1:
        x-=1
if not 1<=x<=w:
    print("NO")
    exit()

#L
x=c
for i in range(n):
    if s[i]=="L":
        x-=1
    if t[i]=="R" and x<w:
        x+=1
if not 1<=x<=w:
    print("NO")
    exit()

#U
x=r
for i in range(n):
    if s[i]=="U":
        x+=1
    if t[i]=="D" and x>1:
        x-=1
if not 1<=x<=h:
    print("NO")
    exit()

#D
x=r
for i in range(n):
    if s[i]=="D":
        x-=1
    if t[i]=="U" and x<h:
        x+=1
if not 1<=x<=h:
    print("NO")
    exit()
print("YES")

Submission status: Wrong Answer

Solution code:
import copy
H,W,N=map(int,input().split())
r,c=map(int,input().split())
Choku=list(input())
Ken  =list(input())

Ue=H
Shita=1
Migi=W
Hidari=1

X=["U","D","R","L"]
h=copy.deepcopy(H)
w=copy.deepcopy(W)
Y=[h,0,w,0]
Z=[h,0,w,0]

C=[-1,1,-1,1]
K=[1,-1,1,-1]

#print(Y)
#print(Choku[-1])
for j in range(1,N+1):
    #print(Y)
    for i in range(4):
        if Ken[-j]==X[i]:
            if i%2==0:
                Y[i+1]=min(Y[i+1]+K[i],Z[i+1])
            else:
                Y[i-1]=max(Y[i-1]+K[i],Z[i-1])
    for i in range(4):
        if Choku[-j]==X[i]:
            Y[i]+=C[i]
            if Y[0]<Y[1] or Y[2]<Y[3]:
                print("NO")
                exit()
    
#print(Y)
if r<=Y[0] and Y[1]<=r and c<=Y[2] and Y[3]<=c:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
import copy
H,W,N=map(int,input().split())
r,c=map(int,input().split())
Choku=list(input())
Ken  =list(input())

Ue=H
Shita=1
Migi=W
Hidari=1

X=["U","D","R","L"]
h=copy.deepcopy(H)
w=copy.deepcopy(W)
Y=[h,0,w,0]
Z=[h,0,w,0]

C=[-1,1,-1,1]
K=[1,-1,1,-1]
for i in range(4):
    if Choku[-1]==X[i]:
        Y[i]+=C[i]
        if Y[0]<Y[1] or Y[2]<Y[3]:
            print("NO")
            exit()

#print(Y)
#print(Choku[-1])
for j in range(2,N+1):
    #print(Y)
    for i in range(4):
        if Ken[-j]==X[i]:
            if i%2==0:
                Y[i+1]=min(Y[i+1]+K[i],Z[i+1])
            else:
                Y[i-1]=max(Y[i-1]+K[i],Z[i-1])
    for i in range(4):
        if Choku[-j]==X[i]:
            Y[i]+=C[i]
            if Y[0]<Y[1] or Y[2]<Y[3]:
                print("NO")
                exit()
    
#print(Y)
if r<=Y[0] and Y[1]<=r and c<=Y[2] and Y[3]<=c:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
import copy
H,W,N=map(int,input().split())
r,c=map(int,input().split())
Choku=list(input())
Ken  =list(input())

Ue=H
Shita=1
Migi=W
Hidari=1

X=["U","D","R","L"]
h=copy.deepcopy(H)
w=copy.deepcopy(W)
Y=[h,1,w,1]
Z=[h,1,w,1]

C=[-1,1,-1,1]
K=[1,-1,1,-1]
for i in range(4):
    if Choku[-1]==X[i]:
        Y[i]+=C[i]
        if Y[0]<Y[1] or Y[2]<Y[3]:
            print("NO")
            exit()

#print(Y)
#print(Choku[-1])
for j in range(2,N+1):
    #print(Y)
    for i in range(4):
        if Ken[-j]==X[i]:
            if i%2==0:
                Y[i+1]=min(Y[i+1]+K[i],Z[i+1])
            else:
                Y[i-1]=max(Y[i-1]+K[i],Z[i-1])
    for i in range(4):
        if Choku[-j]==X[i]:
            Y[i]+=C[i]
            if Y[0]<Y[1] or Y[2]<Y[3]:
                print("NO")
                exit()
    
#print(Y)
if r<=Y[0] and Y[1]<=r and c<=Y[2] and Y[3]<=c:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
import copy
H,W,N=map(int,input().split())
r,c=map(int,input().split())
Choku=list(input())
Ken  =list(input())

Ue=H
Shita=1
Migi=W
Hidari=1

X=["U","D","R","L"]
h=copy.deepcopy(H)
w=copy.deepcopy(W)
Y=[h,1,w,1]
Z=[h,1,w,1]

C=[-1,1,-1,1]
K=[1,-1,1,-1]

#print(Y)
#print(Choku[-1])
for j in range(N):
    #print(Y)
    for i in range(4):
        if Choku[j]==X[i]:
            Y[i]+=C[i]
            if Y[0]<Y[1] or Y[2]<Y[3]:
                print("NO")
                exit()
    for i in range(4):
        if Ken[j]==X[i]:
            if i%2==0:
                Y[i+1]=min(Y[i+1]+K[i],Z[i+1])
            else:
                Y[i-1]=max(Y[i-1]+K[i],Z[i-1])
#print(Y)
if r<=Y[0] and Y[1]<=r and c<=Y[2] and Y[3]<=c:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
# in13.txt
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()

f=False # 髙橋君が勝つことができるか，すなわち駒をマス目から取り除くことができるか Trueならansは'NO'

dU=sr # 何回高橋君が上に駒を移動すれば勝てるか
dD=H-sr+1
dL=sc
dR=W-sc+1

rU=rD=rL=rR=0 # 髙橋君の各手番の終了時までに，各方向に駒を進めることのできる最大のマス目数

s=S[0]
if s=='U':
    rU=1
elif s=='D':
    rD=1
elif s=='L':
    rl=1
elif s=='R':
    rR=1

for i in range(N-1):
    if rU>=dU or rD>=dD or rL>=dL or rR>=dR:
        f=True
        break
    t=T[i]
    if t=='U':
        rD=max(rD-1,-dU+1)
    elif t=='D':
        rU=max(rU-1,-dD+1)
    elif t=='L':
        rR=max(rR-1,-dL+1)
    elif t=='R':
        rL=max(rL-1,-dR+1)
    s=S[i+1]
    if s=='U':
        rU+=1
    elif s=='D':
        rD+=1
    elif s=='L':
        rL+=1
    elif s=='R':
        rR+=1

if rU>=dU or rD>=dD or rL>=dL or rR>=dR:
    f=True

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

Submission status: Wrong Answer

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

C=list(input())
A=list(input())

U=H
D=1
R=W
L=1

C=C[::-1]
A=A[::-1]

for i in range(N):
    if A[i]=="U":
        U=min(H,U+1)
    elif A[i]=="D":
        D=max(1,D-1)
    elif A[i]=="R":
        R=min(W,R+1)
    elif A[i]=="L":
        L=max(1,R-1)
    if C[i]=="U":
        U-=1
        if U<D:
            print("NO")
            exit()
    elif C[i]=="D":
        D+=1
        if U<D:
            print("NO")
            exit()
    elif C[i]=="R":
        R-=1
        if R<L:
            print("NO")
            exit()
    elif C[i]=="L":
        L+=1
        if R<L:
            print("NO")
            exit()
if D<=(h)<=U and L<=(w)<=R:
    print("YES")
else:
    print("NO")
    #print(D,U,L,R)

Submission status: Wrong Answer

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

C=list(input())
A=list(input())

U=H
D=0
R=W
L=0

C=C[::-1]
A=A[::-1]

for i in range(N):
    if A[i]=="U":
        U=min(H,U+1)
    elif A[i]=="D":
        D=max(0,D-1)
    elif A[i]=="R":
        R=min(W,R+1)
    elif A[i]=="L":
        L=max(0,R-1)
    if C[i]=="U":
        U-=1
        if U<D:
            print("NO")
            exit()
    elif C[i]=="D":
        D+=1
        if U<D:
            print("NO")
            exit()
    elif C[i]=="R":
        R-=1
        if R<L:
            print("NO")
            exit()
    elif C[i]=="L":
        L+=1
        if R<L:
            print("NO")
            exit()
if D<=(h-1)<=U and L<=(w-1)<=R:
    print("YES")
else:
    print("NO")
    #print(D,U,L,R)

Submission status: Wrong Answer

Solution code:
import collections

h, w, n = map(int, input().split())
y, x = map(int, input().split())
s = input().rstrip()
t = input().rstrip()
counter = collections.Counter()
for c in s:
    counter[c] += 1


def check(counts, direction, opposite_direction, pos, target):
    for i in range(n):
        counts[direction] -= 1
        if s[i] == direction:
            pos += 1
        if pos == target:
            return False
        if t[i] == opposite_direction and pos > 1 and counts[
                opposite_direction] < pos - 1:
            pos -= 1

    return True


for d, od, pos, target in [
    ('R', 'L', x, w + 1),
    ('L', 'R', w + 1 - x, w + 1),
    ('D', 'U', y, h + 1),
    ('U', 'D', h + 1 - y, h + 1),
]:
    counts = collections.Counter()
    for k, v in counter.items():
        counts[k] = v
    ok = check(counts, d, od, pos, target)
    if not ok:
        print('NO')
        break
else:
    print('YES')

Submission status: Wrong Answer

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

TS, AS = TS[::-1], AS[::-1]


def calc(length, start, target_chars):
    alive_section = [1, length]

    # [区間のどちらに作用するか, どのように作用するか]
    TX = {'L': [0, 1], 'R': [1, -1], 'U': [1, -1], 'D': [0, 1]}
    AX = {'L': [1, 1], 'R': [0, -1], 'U': [0, -1], 'D': [1, 1]}

    # 最後の青木くんのターンから
    for i in range(1, N):
        if AS[i] in target_chars:
            j, d = AX[AS[i]]
            alive_section = update(alive_section, length, j, d)

        if TS[i] in target_chars:
            j, d = TX[TS[i]]
            alive_section = update(alive_section, length, j, d)

        cross_check(alive_section)

    if not (alive_section[0] <= start <= alive_section[1]):
        print('NO')
        exit()


def update(alive_section, length, j, d):
    alive_section[j] += d
    alive_section[0] = max(0, alive_section[0])
    alive_section[1] = min(length, alive_section[1])
    return alive_section


def cross_check(alive_section):
    if alive_section[0] > alive_section[1]:
        print('NO')
        exit()


calc(H, H - sh, ['U', 'D'])
calc(W, sw, ['L', 'R'])

print('YES')

Submission status: Wrong Answer

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

TS, AS = TS[::-1], AS[::-1]


def calc(length, start, target_chars):
    alive_section = [1, length]

    # [区間のどちらに作用するか, どのように作用するか]
    TX = {'L': [0, 1], 'R': [1, -1], 'U': [1, -1], 'D': [0, 1]}
    AX = {'L': [1, 1], 'R': [0, -1], 'U': [0, -1], 'D': [1, 1]}

    # 最後の高橋君のターン
    if TS[0] in target_chars:
        j, d = TX[TS[0]]
        alive_section[j] += d

    # 最後から2番目青木くんのターンから
    for i in range(1, N):
        if AS[i] in target_chars:
            j, d = AX[AS[i]]
            alive_section[j] += d

        if TS[i] in target_chars:
            j, d = TX[TS[i]]
            alive_section[j] += d

        cross_check(alive_section)

    if not (alive_section[0] <= start <= alive_section[1]):
        print('NO')
        exit()


def cross_check(alive_section):
    if alive_section[0] > alive_section[1]:
        print('NO')
        exit()


calc(H, sh, ['U', 'D'])
calc(W, sw, ['L', 'R'])

print('YES')

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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