Problem:
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.

Incorrect submission (verdict: Wrong Answer):
H,W,N = map(int, input().split())
sr,sc = map(int, input().split())
S = input()
T = input()

left, right = 1, W
if S[-1] == "R":
    right -= 1
elif S[-1] == "L":
    left += 1
for si,ti in zip(S[::-2],T[::-2]):
    # print(si,ti)
    # print(left, right)
    if ti == "R":
        left -= 1
    elif ti == "L":
        right += 1
    left = max(1, left)
    right = min(W, right)

    if si == "R":
        right -= 1
    elif si == "L":
        left += 1

    if left > right:
        print("NO")
        exit()

if not (left <= sc <= right):
    print("NO")
    exit()


down, up = 1, H
if S[-1] == "U":
    up -= 1
elif S[-1] == "D":
    down += 1
    
for si,ti in zip(S[::-2],T[::-2]):
    if ti == "U":
        down -= 1
    elif ti == "D":
        up += 1
    down = max(1, down)
    up = min(H, up)

    if si == "U":
        up -= 1
    elif si == "D":
        down += 1

    if down > up:
        print("NO")
        exit()

if not (down <= sr <= up):
    print("NO")
    exit()

print("YES")


Fixed, accepted submission:
H,W,N = map(int, input().split())
sr,sc = map(int, input().split())
S = input()
T = input()

left, right = 0, W+1
if S[-1] == "R":
    right -= 1
elif S[-1] == "L":
    left += 1
for i in reversed(range(N-1)):
    si, ti = S[i], T[i]
    if ti == "R":
        left -= 1
    elif ti == "L":
        right += 1
    left = max(0, left)
    right = min(W+1, right)

    if si == "R":
        right -= 1
    elif si == "L":
        left += 1

    if right - left == 1:
        print("NO")
        exit()

if sc <= left or right <= sc:
    print("NO")
    exit()


left, right = 0, H+1
if S[-1] == "U":
    left += 1
elif S[-1] == "D":
    right -= 1

for i in reversed(range(N-1)):
    si, ti = S[i], T[i]
    if ti == "D":
        left -= 1
    elif ti == "U":
        right += 1
    left = max(0, left)
    right = min(H+1, right)

    if si == "D":
        right -= 1
    elif si == "U":
        left += 1

    if right - left == 1:
        print("NO")
        exit()

if sr <= left or right <= sr:
    print("NO")
    exit()

print("YES")