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

atk_U = [0]*N
atk_D = [0]*N
atk_L = [0]*N
atk_R = [0]*N
u = 0
d = 0
l = 0
r = 0
for i in range(N):
  if S[i] == 'U':
    u += 1
  if S[i] == 'D':
    d += 1
  if S[i] == 'L':
    l += 1
  if S[i] == 'R':
    r += 1
  atk_U[i] = u
  atk_D[i] = d
  atk_L[i] = l
  atk_R[i] = r

def_U = [0]*N
def_D = [0]*N
def_L = [0]*N
def_R = [0]*N
u = 0
d = 0
l = 0
r = 0
for i in range(N-1):
  if T[i] == 'U':
    u += 1
  if T[i] == 'D':
    d += 1
  if T[i] == 'L':
    l += 1
  if T[i] == 'R':
    r += 1
  def_U[i+1] = d
  def_D[i+1] = u
  def_L[i+1] = r
  def_R[i+1] = l

dam_U = max([atk_U[i] - def_U[i] for i in range(N)])
dam_D = max([atk_D[i] - def_D[i] for i in range(N)])
dam_L = max([atk_L[i] - def_L[i] for i in range(N)])
dam_R = max([atk_R[i] - def_R[i] for i in range(N)])
if dam_U < Sr + 1 and dam_D < H - Sr + 1 and dam_L < Sc + 1 and dam_R < W - Sc + 1:
  print('NO')
else:
  print('YES')

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

atk_U = [0]*N
atk_D = [0]*N
atk_L = [0]*N
atk_R = [0]*N
u = 0
d = 0
l = 0
r = 0
for i in range(N):
  if S[i] == 'U':
    u += 1
  if S[i] == 'D':
    d += 1
  if S[i] == 'L':
    l += 1
  if S[i] == 'R':
    r += 1
  atk_U[i] = u
  atk_D[i] = d
  atk_L[i] = l
  atk_R[i] = r

def_U = [0]*N
def_D = [0]*N
def_L = [0]*N
def_R = [0]*N
u = 0
d = 0
l = 0
r = 0
for i in range(N-1):
  if T[i] == 'U':
    if u + 1 - atk_D[i] < Sr:
      u += 1
  if T[i] == 'D':
    if d + 1 - atk_U[i] < H - Sr + 1:
      d += 1
  if T[i] == 'L':
    if l + 1 - atk_R[i] < Sc:
      l += 1
  if T[i] == 'R':
    if r + 1 - atk_L[i] < W - Sc + 1:
      r += 1
  def_U[i+1] = d
  def_D[i+1] = u
  def_L[i+1] = r
  def_R[i+1] = l

dam_U = max([atk_U[i] - def_U[i] for i in range(N)])
dam_D = max([atk_D[i] - def_D[i] for i in range(N)])
dam_L = max([atk_L[i] - def_L[i] for i in range(N)])
dam_R = max([atk_R[i] - def_R[i] for i in range(N)])
if dam_U < Sr and dam_D < H - Sr + 1 and dam_L < Sc and dam_R < W - Sc + 1:
  print('YES')
else:
  print('NO')