Language: C++

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template<class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template<class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
    t = v;
}
template<class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v){
    for (auto &e : t) fill(e, v);
}

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }

#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }


int H, W, N;
int ix, iy;
string S, T;


bool solve() {
    // left, right
    int left = 0, right = W;
    if (S[N-1] == 'L') ++left;
    if (S[N-1] == 'R') --right;
    for (int i = N-2; i >= 0; --i) {
        if (T[i] == 'L') right = min(right+1, W);
        else if (T[i] == 'R') left = max(0, left-1);

        if (S[i] == 'L') left = left + 1;
        else if (S[i] == 'R') right = right - 1;
        
        c//out << i << ": " << left << ", " << right << endl;

        if (left >= right) return false;
    }
    if (iy < left || iy >= right) return false; 

    left = 0, right = H;
    if (S[N-1] == 'U') ++left;
    if (S[N-1] == 'D') --right;
    for (int i = N-2; i >= 0; --i) {
        if (T[i] == 'U') right = min(right+1, H);
        else if (T[i] == 'D') left = max(0, left-1);

        if (S[i] == 'U') left = left + 1;
        else if (S[i] == 'D') right = right - 1;

        //cout << i << ": " << left << ", " << right << endl;
        
        if (left >= right) return false;
    }
    if (ix < left || ix >= right) return false;

    return true;
        
    
    /*
    // down check
    int x = ix, y = iy;
    for (int i = 0; i < N; ++i) {
        if (S[i] == 'D') ++x;
        if (x >= H) return false;
        if (T[i] == 'U') --x;
    }

    // up
    x = ix, y = iy;
    for (int i = 0; i < N; ++i) {
        if (S[i] == 'U') --x;
        if (x < 0) return false;
        if (T[i] == 'D') ++x;
    }

    x = ix, y = iy;
    for (int i = 0; i < N; ++i) {
        if (S[i] == 'R') ++y;
        if (y >= W) return false;
        if (T[i] == 'L') --y;
    }

    x = ix, y = iy;
    for (int i = 0; i < N; ++i) {
        if (S[i] == 'L') --y;
        if (y < 0) return false;
        if (T[i] == 'R') ++y;
    }

    return true;
    */
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    while (cin >> H >> W >> N >> ix >> iy >> S >> T) {
        --ix, --iy;
        if (solve()) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
}


















Compiler output:
a.cc: In function 'bool solve()':
a.cc:72:9: error: 'c' was not declared in this scope
   72 |         c//out << i << ": " << left << ", " << right << endl;
      |         ^