Problem p03054 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline int rd() {
  int x = 0, fl = 1;
  char ch = gc();
  for (; ch < 48 || ch > 57; ch = gc())
    if (ch == '-')
      fl = -1;
  for (; 48 <= ch && ch <= 57; ch = gc())
    x = (x << 3) + (x << 1) + (ch ^ 48);
  return x * fl;
}
inline void wri(int a) {
  if (a < 0)
    a = -a, putchar('-');
  if (a >= 10)
    wri(a / 10);
  putchar(a % 10 | 48);
}
inline void wln(int a) {
  wri(a);
  puts("");
}
const int N = 100002;
int h, w, n, sx, sy, x, y, i;
char s1[N], s2[N];
int main() {
  scanf("%d%d%d%d%d%s%s", &h, &w, &n, &sx, &sy, s1 + 1, s2 + 1);
  x = sx, y = sy;
  for (i = 1; i <= n; i++) {
    if (s1[i] == 'L') {
      y--;
      if (!y)
        return puts("NO"), 0;
    }
    if (s2[i] == 'R')
      y++, y = min(y, w);
  }
  x = sx, y = sy;
  for (i = 1; i <= n; i++) {
    if (s1[i] == 'R') {
      y++;
      if (y > w)
        return puts("NO"), 0;
    }
    if (s2[i] == 'L')
      y--, y = max(y, 1);
  }
  x = sx, y = sy;
  for (i = 1; i <= n; i++) {
    if (s1[i] == 'U') {
      x--;
      if (!x)
        return puts("NO"), 0;
    }
    if (s2[i] == 'D')
      x++, x = min(x, h);
  }
  x = sx, y = sy;
  for (i = 1; i <= n; i++) {
    if (s1[i] == 'D') {
      x++;
      if (x > h)
        return puts("NO"), 0;
    }
    if (s2[i] == 'U')
      x--, x = max(x, 1);
  }
  puts("YES");
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline int rd() {
  int x = 0, fl = 1;
  char ch = gc();
  for (; ch < 48 || ch > 57; ch = gc())
    if (ch == '-')
      fl = -1;
  for (; 48 <= ch && ch <= 57; ch = gc())
    x = (x << 3) + (x << 1) + (ch ^ 48);
  return x * fl;
}
inline void wri(int a) {
  if (a < 0)
    a = -a, putchar('-');
  if (a >= 10)
    wri(a / 10);
  putchar(a % 10 | 48);
}
inline void wln(int a) {
  wri(a);
  puts("");
}
const int N = 200002;
int h, w, n, sx, sy, x, y, i;
char s1[N], s2[N];
int main() {
  scanf("%d%d%d%d%d%s%s", &h, &w, &n, &sx, &sy, s1 + 1, s2 + 1);
  x = sx, y = sy;
  for (i = 1; i <= n; i++) {
    if (s1[i] == 'L') {
      y--;
      if (!y)
        return puts("NO"), 0;
    }
    if (s2[i] == 'R')
      y++, y = min(y, w);
  }
  x = sx, y = sy;
  for (i = 1; i <= n; i++) {
    if (s1[i] == 'R') {
      y++;
      if (y > w)
        return puts("NO"), 0;
    }
    if (s2[i] == 'L')
      y--, y = max(y, 1);
  }
  x = sx, y = sy;
  for (i = 1; i <= n; i++) {
    if (s1[i] == 'U') {
      x--;
      if (!x)
        return puts("NO"), 0;
    }
    if (s2[i] == 'D')
      x++, x = min(x, h);
  }
  x = sx, y = sy;
  for (i = 1; i <= n; i++) {
    if (s1[i] == 'D') {
      x++;
      if (x > h)
        return puts("NO"), 0;
    }
    if (s2[i] == 'U')
      x--, x = max(x, 1);
  }
  puts("YES");
}