Problem p03054 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int N = 100000 + 7;

typedef pair<int, int> ii;

int main() {
  int h, w, n;
  scanf("%d %d %d", &h, &w, &n);
  int sr, sc;
  scanf("%d %d", &sr, &sc);
  char t[N];
  char a[N];
  int r[] = {0, 0};
  int l[] = {0, 0};
  int u[] = {0, 0};
  int d[] = {0, 0};
  getchar();
  for (int i = 0; i < n; ++i) {
    scanf("%c", &t[i]);
    if (t[i] == 'R')
      ++r[0];
    if (t[i] == 'L')
      ++l[0];
    if (t[i] == 'U')
      ++u[0];
    if (t[i] == 'D')
      ++d[0];
  }
  getchar();
  for (int i = 0; i < n; ++i) {
    scanf("%c", &a[i]);
    if (a[i] == 'R')
      ++r[1];
    if (a[i] == 'L')
      ++l[1];
    if (a[i] == 'U')
      ++u[1];
    if (a[i] == 'D')
      ++d[1];
  }
  /*
  for(int i = 0; i < n; ++i) cout << t[i]; cout<<endl;
  for(int i = 0; i < n; ++i) cout << a[i]; cout<<endl;
  */
  if (sr - u[0] < 1) {
    int check = sr;
    for (int i = 0; i < n; ++i) {
      if (t[i] == 'U')
        --check;
      if (check < 1) {
        puts("NO");
        return 0;
      }
      if (check >= 1 && check < h && a[i] == 'D')
        ++check;
    }
  }
  if (sr + d[0] > h) {
    int check = sr;
    for (int i = 0; i < n; ++i) {
      if (t[i] == 'D')
        ++check;
      if (check > h) {
        puts("NO");
        return 0;
      }
      if (check > 1 && check <= h && a[i] == 'U')
        --check;
    }
  }
  if (sc - l[0] < 1) {
    int check = sc;
    for (int i = 0; i < n; ++i) {
      if (t[i] == 'L')
        --check;
      if (check < 1) {
        puts("NO");
        return 0;
      }
      if (check >= 1 && check < w && a[i] == 'R')
        ++check;
    }
  }
  if (sc + r[0] > w) {
    int check = sc;
    for (int i = 0; i < n; ++i) {
      if (t[i] == 'R')
        ++check;
      if (check > w) {
        puts("NO");
        return 0;
      }
      if (check > 1 && check <= w && a[i] == 'L')
        --check;
    }
  }
  puts("YES");
  /*
  string st = t;
  string sa = a;
  int ans = solve(sr, sc, st, sa, pos, 0);
  */
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int N = 200000 + 7;

typedef pair<int, int> ii;

int main() {
  int h, w, n;
  scanf("%d %d %d", &h, &w, &n);
  int sr, sc;
  scanf("%d %d", &sr, &sc);
  char t[N];
  char a[N];
  int r[] = {0, 0};
  int l[] = {0, 0};
  int u[] = {0, 0};
  int d[] = {0, 0};
  getchar();
  for (int i = 0; i < n; ++i) {
    scanf("%c", &t[i]);
    if (t[i] == 'R')
      ++r[0];
    if (t[i] == 'L')
      ++l[0];
    if (t[i] == 'U')
      ++u[0];
    if (t[i] == 'D')
      ++d[0];
  }
  getchar();
  for (int i = 0; i < n; ++i) {
    scanf("%c", &a[i]);
    if (a[i] == 'R')
      ++r[1];
    if (a[i] == 'L')
      ++l[1];
    if (a[i] == 'U')
      ++u[1];
    if (a[i] == 'D')
      ++d[1];
  }
  /*
  for(int i = 0; i < n; ++i) cout << t[i]; cout<<endl;
  for(int i = 0; i < n; ++i) cout << a[i]; cout<<endl;
  */
  if (sr - u[0] < 1) {
    int check = sr;
    for (int i = 0; i < n; ++i) {
      if (t[i] == 'U')
        --check;
      if (check < 1) {
        puts("NO");
        return 0;
      }
      if (check >= 1 && check < h && a[i] == 'D')
        ++check;
    }
  }
  if (sr + d[0] > h) {
    int check = sr;
    for (int i = 0; i < n; ++i) {
      if (t[i] == 'D')
        ++check;
      if (check > h) {
        puts("NO");
        return 0;
      }
      if (check > 1 && check <= h && a[i] == 'U')
        --check;
    }
  }
  if (sc - l[0] < 1) {
    int check = sc;
    for (int i = 0; i < n; ++i) {
      if (t[i] == 'L')
        --check;
      if (check < 1) {
        puts("NO");
        return 0;
      }
      if (check >= 1 && check < w && a[i] == 'R')
        ++check;
    }
  }
  if (sc + r[0] > w) {
    int check = sc;
    for (int i = 0; i < n; ++i) {
      if (t[i] == 'R')
        ++check;
      if (check > w) {
        puts("NO");
        return 0;
      }
      if (check > 1 && check <= w && a[i] == 'L')
        --check;
    }
  }
  puts("YES");
  /*
  string st = t;
  string sa = a;
  int ans = solve(sr, sc, st, sa, pos, 0);
  */
  return 0;
}