Problem p03054 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize(3)
#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>

typedef long long LL;
using namespace std;

#define MAXN 105

char t[MAXN];
char a[MAXN];
int tongt[4]; // LRUD
int tonga[4];
bool a1[MAXN];

int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  int groups;
  // scanf("%d", &groups);
  groups = 1;
  for (int i = 0; i < groups; i++) {
    int h, w, n, sr, sc;
    char tempt, tempa;
    bool flag = false;
    scanf("%d%d%d", &h, &w, &n);
    scanf("%d%d", &sc, &sr);
    scanf("%s", t);
    scanf("%s", a);
    for (int j = 0; j < n; j++) {
      if (t[j] == 'L')
        tongt[0]++;
      if (t[j] == 'R')
        tongt[1]++;
      if (t[j] == 'U')
        tongt[2]++;
      if (t[j] == 'D')
        tongt[3]++;
      if (a[j] == 'L')
        tonga[0]++;
      if (a[j] == 'R')
        tonga[1]++;
      if (a[j] == 'U')
        tonga[2]++;
      if (a[j] == 'D')
        tonga[3]++;
      if (tonga[0] - tongt[1] >= sr) {
        tonga[0]--;
        a1[j] = true;
      }
      if (tonga[1] - tongt[0] >= (w - sr + 1)) {
        tonga[1]--;
        a1[j] = true;
      }
      if (tonga[2] - tongt[3] >= sc) {
        tonga[2]--;
        a1[j] = true;
      }
      if (tonga[3] - tongt[2] >= (h - sc + 1)) {
        tonga[3]--;
        a1[j] = true;
      }
    }
    for (int j = n - 1; j >= 0; j--) {
      if (a[j] == 'L' && !a1[j])
        tonga[0]--;
      if (a[j] == 'R' && !a1[j])
        tonga[1]--;
      if (a[j] == 'U' && !a1[j])
        tonga[2]--;
      if (a[j] == 'D' && !a1[j])
        tonga[3]--;
      if (tongt[0] - tonga[1] >= sr)
        flag = true;
      if (tongt[1] - tonga[0] >= (w - sr + 1))
        flag = true;
      if (tongt[2] - tonga[3] >= sc)
        flag = true;
      if (tongt[3] - tonga[2] >= (h - sc + 1))
        flag = true;
      if (flag)
        break;
      if (t[j] == 'L')
        tongt[0]--;
      if (t[j] == 'R')
        tongt[1]--;
      if (t[j] == 'U')
        tongt[2]--;
      if (t[j] == 'D')
        tongt[3]--;
    }
    if (flag)
      printf("NO");
    else
      printf("YES");
  }
  return 0;
}

Fixed submission:
// #pragma GCC optimize(3)
#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>

typedef long long LL;
using namespace std;

#define MAXN 200005

char t[MAXN];
char a[MAXN];
int tongt[4]; // LRUD
int tonga[4];
bool a1[MAXN];

int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  int groups;
  // scanf("%d", &groups);
  groups = 1;
  for (int i = 0; i < groups; i++) {
    int h, w, n, sr, sc;
    char tempt, tempa;
    bool flag = false;
    scanf("%d%d%d", &h, &w, &n);
    scanf("%d%d", &sc, &sr);
    scanf("%s", t);
    scanf("%s", a);
    for (int j = 0; j < n; j++) {
      if (t[j] == 'L')
        tongt[0]++;
      if (t[j] == 'R')
        tongt[1]++;
      if (t[j] == 'U')
        tongt[2]++;
      if (t[j] == 'D')
        tongt[3]++;
      if (a[j] == 'L')
        tonga[0]++;
      if (a[j] == 'R')
        tonga[1]++;
      if (a[j] == 'U')
        tonga[2]++;
      if (a[j] == 'D')
        tonga[3]++;
      if (tonga[0] - tongt[1] >= sr) {
        tonga[0]--;
        a1[j] = true;
      }
      if (tonga[1] - tongt[0] >= (w - sr + 1)) {
        tonga[1]--;
        a1[j] = true;
      }
      if (tonga[2] - tongt[3] >= sc) {
        tonga[2]--;
        a1[j] = true;
      }
      if (tonga[3] - tongt[2] >= (h - sc + 1)) {
        tonga[3]--;
        a1[j] = true;
      }
    }
    for (int j = n - 1; j >= 0; j--) {
      if (a[j] == 'L' && !a1[j])
        tonga[0]--;
      if (a[j] == 'R' && !a1[j])
        tonga[1]--;
      if (a[j] == 'U' && !a1[j])
        tonga[2]--;
      if (a[j] == 'D' && !a1[j])
        tonga[3]--;
      if (tongt[0] - tonga[1] >= sr)
        flag = true;
      if (tongt[1] - tonga[0] >= (w - sr + 1))
        flag = true;
      if (tongt[2] - tonga[3] >= sc)
        flag = true;
      if (tongt[3] - tonga[2] >= (h - sc + 1))
        flag = true;
      if (flag)
        break;
      if (t[j] == 'L')
        tongt[0]--;
      if (t[j] == 'R')
        tongt[1]--;
      if (t[j] == 'U')
        tongt[2]--;
      if (t[j] == 'D')
        tongt[3]--;
    }
    if (flag)
      printf("NO");
    else
      printf("YES");
  }
  return 0;
}