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

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#include <boost/multi_array.hpp>
#include <boost/optional.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
#pragma clang diagnostic pop

// using namespace std;

struct direction_t {
  constexpr direction_t(int y_, int x_) : y(y_), x(x_) {}
  int y;
  int x;
};
bool operator==(const direction_t& lhs, const direction_t& rhs) {
  return lhs.y == rhs.y && lhs.x == rhs.x;
}

static constexpr direction_t nothing = direction_t(0, 0);
static constexpr direction_t left = direction_t(0, -1);
static constexpr direction_t right = direction_t(0, 1);
static constexpr direction_t up = direction_t(-1, 0);
static constexpr direction_t down = direction_t(1, 0);

struct position_t {
  constexpr position_t(int y_, int x_) : y(y_), x(x_) {}

  int y;
  int x;
};

static std::string const YES = "YES";
static std::string const NO = "NO";

static std::vector<direction_t> sCandidates;
static std::vector<direction_t> tCandidates;

static std::vector<int> lefts;
static std::vector<int> rights;
static std::vector<int> ups;
static std::vector<int> downs;

static int h;
static int w;
static int n;

static int minimax(position_t position, bool turn, int moves) {
  if (n * 2 - moves <= 0) {
    return -1;
  }

  int const maximumMovableLength = n - moves / 2;
  if (position.y - maximumMovableLength >= 0 &&
      position.y + maximumMovableLength < h &&
      position.x - maximumMovableLength >= 0 &&
      position.x + maximumMovableLength < w) {
    return -1;
  }

  direction_t candidate =
      turn ? sCandidates[moves / 2] : tCandidates[moves / 2];
  if (candidate == nothing) {
    return minimax(position, !turn, moves + 1);
  }
  position_t const newPosition =
      position_t(position.y + candidate.y, position.x + candidate.x);
  if (newPosition.y < 0 || h <= newPosition.y || newPosition.x < 0 ||
      w <= newPosition.x) {
    if (turn) {
      return 1;
    } else {
      return minimax(position, !turn, moves + 1);
    }
  }

  int const distance = abs(position.y - h / 2) + abs(position.x - w / 2);
  int const newDistance =
      abs(newPosition.y - h / 2) + abs(newPosition.x - w / 2);
  if (newDistance < distance) {
    if (turn) {
      if (minimax(newPosition, !turn, moves + 1) > 0) {
        return 1;
      }
      return minimax(position, !turn, moves + 1);
    } else {
      if (minimax(position, !turn, moves + 1) < 0) {
        return -1;
      }
      return minimax(newPosition, !turn, moves + 1);
    }
  } else {
    if (turn) {
      if (minimax(position, !turn, moves + 1) > 0) {
        return 1;
      }
      return minimax(newPosition, !turn, moves + 1);
    } else {
      if (minimax(newPosition, !turn, moves + 1) < 0) {
        return -1;
      }
      return minimax(position, !turn, moves + 1);
    }
  }
}

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  std::cin >> h;
  std::cin >> w;
  std::cin >> n;
  int s_r;
  std::cin >> s_r;
  int s_c;
  std::cin >> s_c;
  std::string s;
  std::cin >> s;
  sCandidates.reserve(n);
  lefts.reserve(n + 1);
  rights.reserve(n + 1);
  ups.reserve(n + 1);
  downs.reserve(n + 1);
  lefts.emplace_back(0);
  rights.emplace_back(0);
  ups.emplace_back(0);
  downs.emplace_back(0);
  for (auto const ch : s) {
    lefts.push_back(lefts.back());
    rights.push_back(lefts.back());
    ups.push_back(lefts.back());
    downs.push_back(lefts.back());
    switch (ch) {
      case 'L':
        lefts.back()++;
        break;
      case 'R':
        rights.back()++;
        break;
      case 'U':
        ups.back()++;
        break;
      case 'D':
        downs.back()++;
        break;
    }
  }
  bool const leftEnabled = s_c - lefts.back() < 0;
  bool const rightEnabled = s_c + rights.back() >= w;
  bool const upEnabled = s_r - ups.back() < 0;
  bool const downEnabled = s_r + downs.back() >= h;
  for (auto const ch : s) {
    switch (ch) {
      case 'L':
        sCandidates.push_back(leftEnabled ? left : nothing);
        break;
      case 'R':
        sCandidates.push_back(rightEnabled ? right : nothing);
        rights.back()++;
        break;
      case 'U':
        sCandidates.push_back(upEnabled ? up : nothing);
        ups.back()++;
        break;
      case 'D':
        sCandidates.push_back(downEnabled ? down : nothing);
        downs.back()++;
        break;
    }
  }
  std::string t;
  std::cin >> t;
  tCandidates.reserve(n);
  for (auto const ch : t) {
    switch (ch) {
      case 'L':
        tCandidates.push_back(rightEnabled ? left : nothing);
        break;
      case 'R':
        tCandidates.push_back(leftEnabled ? right : nothing);
        break;
      case 'U':
        tCandidates.push_back(downEnabled ? up : nothing);
        break;
      case 'D':
        tCandidates.push_back(upEnabled ? down : nothing);
        break;
    }
  }
  auto const res = minimax(position_t(s_r - 1, s_c - 1), true, 0);
  std::cout << ((res > 0) ? NO : YES) << std::endl;
  return 0;
}