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:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(H, W, N, s, S, T):
    s_ = s
    left = 0
    right = W-1
    bottom = 0
    top = H-1
    for s, t in reversed(list(zip(S, T))):
        if t == 'R':
            left = max(0, left-1)
        elif t == 'L':
            right = min(W-1, right+1)
        elif t == 'U':
            bottom = max(0, bottom-1)
        elif t == 'D':
            top = min(H-1, top+1)

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

        if left > right:
            return 'NO'
        elif bottom > top:
            return 'NO'

    s = s_
    if not (left <= s[1]-1 <= right):
        return 'NO'
    elif not (bottom <= s[0]-1 <= top):
        return 'NO'
    return 'YES'


def main():
    H, W, N = read_int_n()
    s = read_int_n()
    S = read_str()
    T = read_str()
    print(slv(H, W, N, s, S, T))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, w

for i in range(n-1, -1, -1):
    if t[i] == 'R':
        # 後手が右に行く権利を得たので
        # 安全なゾーンが左に広がる(1より左には行けない)
        L = max(L-1, 1)
    if t[i] == 'L':
        # 後手が左に行く権利を得たので
        # 安全なゾーンが右に広がる(wより右には行けない)
        R = min(R+1, w)

    if s[i] == 'R':
        # 先手が右に行く権利を得たので
        # 安全なゾーンが左に狭まる
        R -= 1
    if s[i] == 'L':
        # 先手が左に行く権利を得たので
        # 安全なゾーンが右に狭まる
        L += 1

if (R < L) or (sc < L) or (R < sc):
    is_drop = True

# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, h

for i in range(n-1, -1, -1):
    if t[i] == 'U':
        # 後手が上に行く権利を得たので
        # 安全なゾーンが下に広がる(hより下には行けない)
        R = min(R+1, h)
    if t[i] == 'D':
        # 後手が下に行く権利を得たので
        # 安全なゾーンが上に広がる(0より上には行けない)
        L = max(L-1, 1)

    if s[i] == 'U':
        # 先手が上に行く権利を得たので
        # 安全なゾーンが下に狭まる
        L += 1
    if s[i] == 'D':
        R -= 1

if (R-L < 0) or (sr < L) or (R < sr):
    is_drop = True

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

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
s = input()
t = input()
sr -= 1; sc -= 1
 
L=0; R=w-1; U=0; D=h-1;
is_drop = False
for i in range(n)[::-1]:
    if t[i] == 'U': D = min(D+1, h-1)
    if t[i] == 'D': U = max(U-1, 0)
    if t[i] == 'L': R = min(R+1, w-1)
    if t[i] == 'R': L = max(L-1, 0)
    if s[i] == 'U': U += 1
    if s[i] == 'D': D -= 1
    if s[i] == 'L': L += 1
    if s[i] == 'R': R -= 1
    if R < L or D < U: is_drop = True
    if (not L <= sc <= R) or (not U <= sr <= D):  is_drop = True

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

Submission status: Wrong Answer

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

# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, w

for i in range(n-1, -1, -1):
    if t[i] == 'R':
        # 後手が右に行く権利を得たので
        # 安全なゾーンが左に広がる(1より左には行けない)
        L = max(L-1, 1)
    if t[i] == 'L':
        # 後手が左に行く権利を得たので
        # 安全なゾーンが右に広がる(wより右には行けない)
        R = min(R+1, w)

    if s[i] == 'R':
        # 先手が右に行く権利を得たので
        # 安全なゾーンが左に狭まる
        R -= 1
    if s[i] == 'L':
        # 先手が左に行く権利を得たので
        # 安全なゾーンが右に狭まる
        L += 1

    if (R < L) or (sc < L) or (R < sc):
        is_drop = True

# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, h

for i in range(n-1, -1, -1):
    if t[i] == 'U':
        # 後手が上に行く権利を得たので
        # 安全なゾーンが下に広がる(hより下には行けない)
        R = min(R+1, h)
    if t[i] == 'D':
        # 後手が下に行く権利を得たので
        # 安全なゾーンが上に広がる(0より上には行けない)
        L = max(L-1, 1)

    if s[i] == 'U':
        # 先手が上に行く権利を得たので
        # 安全なゾーンが下に狭まる
        L += 1
    if s[i] == 'D':
        R -= 1
    if (R-L < 0) or (sr < L) or (R < sr):
        is_drop = True

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

Submission status: Wrong Answer

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

# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, w

for i in range(n-1, -1, -1):
    if t[i] == 'R':
        # 後手が右に行く権利を得たので
        # 安全なゾーンが左に広がる(1より左には行けない)
        L = max(L-1, 1)
    if t[i] == 'L':
        # 後手が左に行く権利を得たので
        # 安全なゾーンが右に広がる(wより右には行けない)
        R = min(R+1, w)

    if s[i] == 'R':
        # 先手が右に行く権利を得たので
        # 安全なゾーンが左に狭まる
        R -= 1
    if s[i] == 'L':
        # 先手が左に行く権利を得たので
        # 安全なゾーンが右に狭まる
        L += 1

    if (R < L) or (sc < L) or (R < sc):
        is_drop = True

# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, h

for i in range(n-1, -1, -1):
    if t[i] == 'U':
        # 後手が上に行く権利を得たので
        # 安全なゾーンが下に広がる(hより下には行けない)
        R = min(R+1, h)
    if t[i] == 'D':
        # 後手が下に行く権利を得たので
        # 安全なゾーンが上に広がる(0より上には行けない)
        L = max(L-1, 1)

    if s[i] == 'U':
        # 先手が上に行く権利を得たので
        # 安全なゾーンが下に狭まる
        L += 1
    if s[i] == 'D':
        R -= 1
    if (R-L < 0) or (sr < L) or (R < sr):
        is_drop = True

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

Submission status: Wrong Answer

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

# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, w

for i in range(n-1, -1, -1):
    if t[i] == 'R':
        # 後手が右に行く権利を得たので
        # 安全なゾーンが左に広がる(1より左には行けない)
        L = max(L-1, 1)
    if t[i] == 'L':
        # 後手が左に行く権利を得たので
        # 安全なゾーンが右に広がる(wより右には行けない)
        R = min(R+1, w)

    if s[i] == 'R':
        # 先手が右に行く権利を得たので
        # 安全なゾーンが左に狭まる
        R -= 1
        if R < 1:
            is_drop = True
    if s[i] == 'L':
        # 先手が左に行く権利を得たので
        # 安全なゾーンが右に狭まる
        L += 1
        if L > w:
            is_drop = True


    if R < L or sc < L or R < sc:
        is_drop = True

# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, h

for i in range(n-1, -1, -1):
    if t[i] == 'U':
        # 後手が上に行く権利を得たので
        # 安全なゾーンが下に広がる(hより下には行けない)
        R = min(R+1, h)
    if t[i] == 'D':
        # 後手が下に行く権利を得たので
        # 安全なゾーンが上に広がる(0より上には行けない)
        L = max(L-1, 1)

    if s[i] == 'U':
        # 先手が上に行く権利を得たので
        # 安全なゾーンが下に狭まる
        L += 1
        if L > h:
            is_drop = True
    if s[i] == 'D':
        R -= 1
        if R < 1:
            is_drop = True
    if R-L < 0 or sr < L or R < sr:
        is_drop = True

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

Submission status: Wrong Answer

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

s = input(); t = input()
is_drop = False

# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, w

for i in range(n)[::-1]:
    if t[i] == 'R':
        # 後手が右に行く権利を得たので
        # 安全なゾーンが左に広がる(1より左には行けない)
        L = max(L-1, 1)
    if t[i] == 'L':
        # 後手が左に行く権利を得たので
        # 安全なゾーンが右に広がる(wより右には行けない)
        R = min(R+1, w)

    if s[i] == 'R':
        # 先手が右に行く権利を得たので
        # 安全なゾーンが左に狭まる
        R -= 1
    if s[i] == 'L':
        # 先手が左に行く権利を得たので
        # 安全なゾーンが右に狭まる
        L += 1

    if R-L <= 0 or sc < L or R < sc:
        is_drop = True


# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, h

for i in range(n-1, -1, -1):
    if t[i] == 'U':
        # 後手が上に行く権利を得たので
        # 安全なゾーンが下に広がる(hより下には行けない)
        R = min(R+1, h)
    if t[i] == 'D':
        # 後手が下に行く権利を得たので
        # 安全なゾーンが上に広がる(0より上には行けない)
        L = max(L-1, 1)

    if s[i] == 'U':
        # 先手が上に行く権利を得たので
        # 安全なゾーンが下に狭まる
        L += 1
    if s[i] == 'D':
        R -= 1
    if R-L <= 0 or sr < L or R < sr:
        is_drop = True

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

Submission status: Wrong Answer

Solution code:
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
sr -= 1; sc -= 1
s = input(); t = input()
is_drop = False

# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, w

for i in range(n)[::-1]:
    if t[i] == 'R':
        # 後手が右に行く権利を得たので
        # 安全なゾーンが左に広がる(1より左には行けない)
        L = max(L-1, 1)
    if t[i] == 'L':
        # 後手が左に行く権利を得たので
        # 安全なゾーンが右に広がる(wより右には行けない)
        R = min(R+1, w)

    if s[i] == 'R':
        # 先手が右に行く権利を得たので
        # 安全なゾーンが左に狭まる
        R -= 1
    if s[i] == 'L':
        # 先手が左に行く権利を得たので
        # 安全なゾーンが右に狭まる
        L += 1

    if R-L < 0 or sc < L or R < sc:
        is_drop = True


# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, h

for i in range(n-1, -1, -1):
    if t[i] == 'U':
        # 後手が上に行く権利を得たので
        # 安全なゾーンが下に広がる(hより下には行けない)
        R = min(R+1, h)
    if t[i] == 'D':
        # 後手が下に行く権利を得たので
        # 安全なゾーンが上に広がる(0より上には行けない)
        L = max(L-1, 1)

    if s[i] == 'U':
        # 先手が上に行く権利を得たので
        # 安全なゾーンが下に狭まる
        L += 1
    if s[i] == 'D':
        R -= 1
    if R-L < 0 or sr < L or R < sr:
        is_drop = True

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

Submission status: Wrong Answer

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

# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, w

for i in range(n-1, -1, -1):
    if t[i] == 'R':
        # 後手が右に行く権利を得たので
        # 安全なゾーンが左に広がる(1より左には行けない)
        L = max(L-1, 1)
    if t[i] == 'L':
        # 後手が左に行く権利を得たので
        # 安全なゾーンが右に広がる(wより右には行けない)
        R = min(R+1, w)

    if s[i] == 'R':
        # 先手が右に行く権利を得たので
        # 安全なゾーンが左に狭まる
        R -= 1
    if s[i] == 'L':
        # 先手が左に行く権利を得たので
        # 安全なゾーンが右に狭まる
        L += 1

    if R-L < 0 or sc < L or R < sc:
        is_drop = True


# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, h

for i in range(n-1, -1, -1):
    if t[i] == 'U':
        # 後手が上に行く権利を得たので
        # 安全なゾーンが下に広がる(hより下には行けない)
        R = min(R+1, h)
    if t[i] == 'D':
        # 後手が下に行く権利を得たので
        # 安全なゾーンが上に広がる(0より上には行けない)
        L = max(L-1, 1)

    if s[i] == 'U':
        # 先手が上に行く権利を得たので
        # 安全なゾーンが下に狭まる
        L += 1
    if s[i] == 'D':
        R -= 1
    if R-L < 0 or sr < L or R < sr:
        is_drop = True

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

Submission status: Wrong Answer

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

# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, w

for i in range(n-1, -1, -1):
    if t[i] == 'R':
        # 後手が右に行く権利を得たので
        # 安全なゾーンが左に広がる(1より左には行けない)
        L = max(L-1, 1)
    if t[i] == 'L':
        # 後手が左に行く権利を得たので
        # 安全なゾーンが右に広がる(wより右には行けない)
        R = min(R+1, w)

    if s[i] == 'R':
        # 先手が右に行く権利を得たので
        # 安全なゾーンが左に狭まる
        if R-1 < 1:
            is_drop = True
        R = max(R-1, 1)
    if s[i] == 'L':
        # 先手が左に行く権利を得たので
        # 安全なゾーンが右に狭まる
        if L+1 > w:
            is_drop = True
        L = min(L+1, w)

    if R-L < 0 or sc < L or R < sc:
        is_drop = True


# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, h

for i in range(n-1, -1, -1):
    if t[i] == 'U':
        # 後手が上に行く権利を得たので
        # 安全なゾーンが下に広がる(hより下には行けない)
        R = min(R+1, h)
    if t[i] == 'D':
        # 後手が下に行く権利を得たので
        # 安全なゾーンが上に広がる(0より上には行けない)
        L = max(L-1, 1)

    if s[i] == 'U':
        # 先手が上に行く権利を得たので
        # 安全なゾーンが下に狭まる
        if L+1 > h:
            is_drop = True
        L = min(L+1, h)
    if s[i] == 'D':
        R = max(R-1, 0)
        if R-1 < 1:
            is_drop = True
    if R-L < 0 or sr < L or R < sr:
        is_drop = True

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

Submission status: Wrong Answer

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

# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, w

for i in range(n-1, -1, -1):
    if t[i] == 'R':
        # 後手が右に行く権利を得たので
        # 安全なゾーンが左に広がる(0より左には行けない)
        L = max(L-1, 0)
    if t[i] == 'L':
        # 後手が左に行く権利を得たので
        # 安全なゾーンが右に広がる(wより右には行けない)
        R = min(R+1, w)

    if s[i] == 'R':
        # 先手が右に行く権利を得たので
        # 安全なゾーンが左に狭まる
        if R-1 < 0:
            is_drop = True
        R = max(R-1, 0)
    if s[i] == 'L':
        # 先手が左に行く権利を得たので
        # 安全なゾーンが右に狭まる
        if L+1 > w:
            is_drop = True
        L = min(L+1, w)

    if R-L < 0 or sc < L or R < sc:
        is_drop = True


# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, h

for i in range(n-1, -1, -1):
    if t[i] == 'U':
        # 後手が上に行く権利を得たので
        # 安全なゾーンが下に広がる(hより下には行けない)
        R = min(R+1, h)
    if t[i] == 'D':
        # 後手が下に行く権利を得たので
        # 安全なゾーンが上に広がる(0より上には行けない)
        L = max(L-1, 0)

    if s[i] == 'U':
        # 先手が上に行く権利を得たので
        # 安全なゾーンが下に狭まる
        if L+1 > h:
            is_drop = True
        L = min(L+1, h)
    if s[i] == 'D':
        R = max(R-1, 0)
        if R-1 < 0:
            is_drop = True
    if R-L < 0 or sr < L or R < sr:
        is_drop = True

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

Submission status: Wrong Answer

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

# 区間 [R, L] は安全なゾーン
# 初期値は全区間
L, R = 1, w

for i in range(n-1, -1, -1):
    if t[i] == 'R':
        # 後手が右に行く権利を得たので
        # 安全なゾーンが左に広がる(0より左には行けない)
        L = max(L-1, 0)
    if t[i] == 'L':
        # 後手が左に行く権利を得たので
        # 安全なゾーンが右に広がる(wより右には行けない)
        R = min(R+1, w)

    if s[i] == 'R':
        # 先手が右に行く権利を得たので
        # 安全なゾーンが左に狭まる
        R = max(R-1, 0)
    if s[i] == 'L':
        # 先手が左に行く権利を得たので
        # 安全なゾーンが右に狭まる
        L = min(L+1, w)

    if R < L or sc < L or R < sc:
        is_drop = True


# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, h

for i in range(n-1, -1, -1):
    if t[i] == 'U':
        # 後手が上に行く権利を得たので
        # 安全なゾーンが下に広がる(hより下には行けない)
        R = min(R+1, h)
    if t[i] == 'D':
        # 後手が下に行く権利を得たので
        # 安全なゾーンが上に広がる(0より上には行けない)
        L = max(L-1, 0)

    if s[i] == 'U':
        # 先手が上に行く権利を得たので
        # 安全なゾーンが下に狭まる
        L = min(L+1, h)
    if s[i] == 'D':
        R = max(R-1, 0)

    if R < L or sr < L or R < sr:
        is_drop = True
    
print("NO" if is_drop else "YES")

Submission status: Wrong Answer

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

# 区間 [R, L] は安全なゾーン
# 初期値は全区間
L, R = 1, w

for i in range(n-1, -1, -1):
    if t[i] == 'R':
        # 後手が右に行く権利を得たので
        # 安全なゾーンが左に広がる(0より左には行けない)
        L = max(L-1, 0)
    if t[i] == 'L':
        # 後手が左に行く権利を得たので
        # 安全なゾーンが右に広がる(wより右には行けない)
        R = min(R+1, w)

    if s[i] == 'R':
        # 先手が右に行く権利を得たので
        # 安全なゾーンが左に狭まる
        R -= 1
    if s[i] == 'L':
        # 先手が左に行く権利を得たので
        # 安全なゾーンが右に狭まる
        L += 1

    if R < L or sc < L or R < sc:
        is_drop = True


# 区間 [L, R] は安全なゾーン
# 初期値は全区間
L, R = 1, h

for i in range(n-1, -1, -1):
    if t[i] == 'U':
        # 後手が上に行く権利を得たので
        # 安全なゾーンが下に広がる(hより下には行けない)
        R = min(R+1, h)
    if t[i] == 'D':
        # 後手が下に行く権利を得たので
        # 安全なゾーンが上に広がる(0より上には行けない)
        L = max(L-1, 0)

    if s[i] == 'U':
        # 先手が上に行く権利を得たので
        # 安全なゾーンが下に狭まる
        L += 1
    if s[i] == 'D':
        R -= 1

    if R < L or sr < L or R < sr:
        is_drop = True
    
print("NO" if is_drop else "YES")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd

YES = "YES"  # type: str
NO = "NO"  # type: str

def solve(H: int, W: int, N: int, s_r: int, s_c: int, S: str, T: str):
    d = {
        'L': 0,
        'R': 0,
        'U': 0,
        'D': 0
    }
    r = {
        'L' : 'R',
        'R' : 'L',
        'U' : 'D',
        'D' : 'U',
    }
    for s, t in zip(S, T):
        d[s] += 1
        if (W < s_c + d['R']) or (s_c - d['L'] < 1) or (H < s_r + d['D']) or (s_r - d['U'] < 1):
            return NO
        d[r[t]] = max(d[r[t]] - 1, 0)
    return YES


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    s_r = int(next(tokens))  # type: int
    s_c = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    T = next(tokens)  # type: str
    result = solve(H, W, N, s_r, s_c, S, T)
    print(result)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd

YES = "YES"  # type: str
NO = "NO"  # type: str

def solve(H: int, W: int, N: int, s_r: int, s_c: int, S: str, T: str):
    d = {
        'L': 0,
        'R': 0,
        'U': 0,
        'D': 0
    }
    r = {
        'L' : 'R',
        'R' : 'L',
        'U' : 'D',
        'D' : 'U',
    }
    for s, t in zip(S, T):
        d[s] += 1
        d[r[t]] = max(d[r[t]] - 1, 0)
        if (W < s_c + d['R']) or (s_c - d['L'] < 1) or (H < s_r + d['D']) or (s_r - d['U'] < 1):
            return NO
    return YES


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    s_r = int(next(tokens))  # type: int
    s_c = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    T = next(tokens)  # type: str
    result = solve(H, W, N, s_r, s_c, S, T)
    print(result)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd

YES = "YES"  # type: str
NO = "NO"  # type: str

def solve(H: int, W: int, N: int, s_r: int, s_c: int, S: str, T: str):
    d = {
        'L': 0,
        'R': 0,
        'U': 0,
        'D': 0
    }
    r = {
        'L' : 'R',
        'R' : 'L',
        'U' : 'D',
        'D' : 'U',
    }
    for s, t in zip(S, T):
        d[s] += 1
        d[r[t]] = max(d[r[t]] - 1, 0)

    if (s_c + d['R'] <= W) and (1 <= s_c - d['L']) and (s_r + d['D'] <= H) and (1 <= s_r - d['U']):
        return YES
    return NO


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    s_r = int(next(tokens))  # type: int
    s_c = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    T = next(tokens)  # type: str
    result = solve(H, W, N, s_r, s_c, S, T)
    print(result)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd

YES = "YES"  # type: str
NO = "NO"  # type: str

def solve(H: int, W: int, N: int, s_r: int, s_c: int, S: str, T: str):
    d = {
        'L': 0,
        'R': 0,
        'U': 0,
        'D': 0
    }
    r = {
        'L' : 'R',
        'R' : 'L',
        'U' : 'D',
        'D' : 'U',
    }
    for s, t in zip(S, T):
        d[s] += 1
        d[r[t]] = max(d[r[t]] - 1, 0)

    if (s_c + d['R'] <= W) and (1 <= s_c - d['L']) and (s_r + d['U'] <= H) and (1 <= s_r - d['D']):
        return YES
    return NO


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    s_r = int(next(tokens))  # type: int
    s_c = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    T = next(tokens)  # type: str
    result = solve(H, W, N, s_r, s_c, S, T)
    print(result)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

def main():
    input = sys.stdin.readline
    H, W, N = map(int, input().split())
    ini_r, ini_c = map(int, input().split())
    S = str(input().strip())[::-1]
    T = str(input().strip())[::-1]

    dp = [0] * (N+1)
    dp[0] = (1, H, 1, W)
    for i in range(N):
        s, t, = S[i], T[i]
        d, u, l, r = dp[i]

        if t == 'L':
            r = min(W, r+1)
        elif t == 'R':
            l = max(1, l-1)
        elif t == 'U':
            u = min(H, u+1)
        else:
            d = max(1, d-1)

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

        if not(d <= ini_r <= u and l <= ini_c <= r):
            return 'NO'

        dp[i+1] = (d, u, l, r)

    return 'YES'


if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
H, W, N = map(int, input().split())
sr, sc = map(lambda x: int(x) - 1, input().split())
S = input()
T = input()
l, r = 0, W
u, d = 0, H
for i in range(N)[::-1]:
    if T[i] == 'L': r = min(W, r + 1)
    if T[i] == 'R': l = max(0, l - 1)
    if T[i] == 'U': d = min(H, d + 1)
    if T[i] == 'D': u = max(0, u - 1)
    if S[i] == 'L': l += 1
    if S[i] == 'R': r -= 1
    if S[i] == 'U': u += 1
    if S[i] == 'D': d -= 1
    if l >= r or u >= d:
        print("NO")
        exit()

print("YES")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
H, W, N = map(int, input().split())
sr, sc = map(lambda x: int(x) - 1, input().split())
S = input()
T = input()
l, r = 0, W
u, d = 0, H
for i in range(N)[::-1]:
    if T[i] == 'L': r = min(W, r + 1)
    if T[i] == 'R': l = max(0, l - 1)
    if T[i] == 'U': d = min(H, d + 1)
    if T[i] == 'D': u = max(0, u - 1)
    if S[i] == 'L': l += 1
    if S[i] == 'R': r -= 1
    if S[i] == 'U': u += 1
    if S[i] == 'D': d -= 1
    if l >= r or u <= d: 
        print("NO")
        exit()

print("YES")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
H, W, N = map(int, input().split())
sr, sc = map(lambda x: int(x) - 1, input().split())
S = input()
T = input()
l, r = 0, W
u, d = 0, H
for i in range(N)[::-1]:
    if T[i] == 'L': r = min(W, r + 1)
    if T[i] == 'R': l = max(0, l - 1)
    if T[i] == 'U': d = min(H, d + 1)
    if T[i] == 'D': u = max(0, u - 1)
    if S[i] == 'L': l += 1
    if S[i] == 'R': r -= 1
    if S[i] == 'U': u += 1
    if S[i] == 'D': d -= 1
    if l == r or u == d:
        print("NO")
        exit()

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-s2
l = s2 -1
u = h - s1
d = s1 -1

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":
        r -= 1
        l += 1
    if r < 0:
        print("NO")
        exit()
    if T[i] ==  "L":
        if l != 0:
            r += 1
            l -= 1

r = w-s2
l = s2 - 1

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


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

u = h - s1
d = s1-1

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

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()


d = s1
r = s2

S = list(input())

T = list(input())


for i in range(n):
    if S[i] == "R":
        r += 1
    if r > w:
        print("NO")
        exit()
    if T[i] ==  "L":
        if r != 1:
            r -= 1

r = s1

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


for i in range(n):
    if S[i] == "U":
        d += 1
    if d > h:
        print("NO")
        exit()
    if T[i] == "D":
        if d != 1:
            d -= 1

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

print("YES")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
H, W, N = map(int, input().split())
sr, sc = map(lambda x: int(x) - 1, input().split())
S = input()
T = input()
l, r = 0, W
u, d = 0, H
for i in range(N)[::-1]:
    if T[i] == 'L': r = min(W, r + 1)
    if T[i] == 'R': l = max(0, l - 1)
    if T[i] == 'U': d = min(H, d + 1)
    if T[i] == 'D': u = max(0, l - 1)
    if S[i] == 'L': l += 1
    if S[i] == 'R': r -= 1
    if S[i] == 'U': u += 1
    if S[i] == 'D': d -= 1
    if l == r or u == d: 
        print("NO")
        exit()

print(("NO", "YES")[l <= sc < r and u <= sr < d])

Submission status: Wrong Answer

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

sr1 = sr
sc1 = sc

ans = "YES"
for i in range(n):
    taka = s[i]
    ao = t[i]
    if taka == "L":
        sc1 -= 1
    if sc1 < 1:
        ans = "NO"
        break
    if ao == "R":
        if sc1 < w:
            sc1 += 1

sr1 = sr
sc1 = sc
for i in range(n):
    taka = s[i]
    ao = t[i]
    if taka == "R":
        sc1 += 1
    if sc1 > w:
        ans = "NO"
        break
    if ao == "L":
        if sc1 > 1:
            sc1 -= 1


sr1 = sr
sc1 = sc
for i in range(n):
    taka = s[i]
    ao = t[i]
    if taka == "U":
        sr1 += 1
    if sr1 > h:
        ans = "NO"
        break
    if ao == "D":
        if sr1 > 1:
            sr1 -= 1

sr1 = sr
sc1 = sc
for i in range(n):
    taka = s[i]
    ao = t[i]
    if taka == "D":
        sr1 -= 1
    if sr1 < 1:
        ans = "NO"
        break
    if ao == "U":
        if sr1 < h:
            sr1 += 1

print(ans)