Problem p03081 (C++)

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

typedef long long ll;
typedef pair<int, int> P;

#define p_ary(ary, a, b, i)                                                    \
  do {                                                                         \
    cout << "[";                                                               \
    for (int(i) = (a); (i) < (b); ++(i))                                       \
      cout << ary[(i)] << ((b)-1 == (i) ? "" : ", ");                          \
    cout << "]\n";                                                             \
  } while (0)
#define p_map(map, it)                                                         \
  do {                                                                         \
    cout << "{";                                                               \
    for (auto(it) = map.begin();; ++(it)) {                                    \
      if ((it) == map.end()) {                                                 \
        cout << "}\n";                                                         \
        break;                                                                 \
      } else                                                                   \
        cout << "" << (it)->first << "=>" << (it)->second << ", ";             \
    }                                                                          \
  } while (0)

int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  char t[n], d[n];
  for (int i = 0; i < q; ++i)
    cin >> t[i] >> d[i];
  int min = -1, max = n;
  while (max - min > 1) {
    int mid = (max + min) / 2;
    int i = mid;
    bool ok = false;
    for (int j = 0; j < q; ++j)
      if (t[j] == s[i]) {
        i += (d[j] == 'R' ? 1 : -1);
        if (i == n)
          break;
        if (i == -1) {
          ok = true;
          break;
        }
      }
    (ok ? min : max) = mid;
  }
  int l = min;
  min = -1;
  max = n;
  while (max - min > 1) {
    int mid = (max + min) / 2;
    int i = n - 1 - mid;
    bool ok = false;
    for (int j = 0; j < q; ++j)
      if (t[j] == s[i]) {
        i += (d[j] == 'R' ? 1 : -1);
        if (i == -1)
          break;
        if (i == n) {
          ok = true;
          break;
        }
      }
    (ok ? min : max) = mid;
  }
  cout << n - l - 2 - min;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define p_ary(ary, a, b, i)                                                    \
  do {                                                                         \
    cout << "[";                                                               \
    for (int(i) = (a); (i) < (b); ++(i))                                       \
      cout << ary[(i)] << ((b)-1 == (i) ? "" : ", ");                          \
    cout << "]\n";                                                             \
  } while (0)
#define p_map(map, it)                                                         \
  do {                                                                         \
    cout << "{";                                                               \
    for (auto(it) = map.begin();; ++(it)) {                                    \
      if ((it) == map.end()) {                                                 \
        cout << "}\n";                                                         \
        break;                                                                 \
      } else                                                                   \
        cout << "" << (it)->first << "=>" << (it)->second << ", ";             \
    }                                                                          \
  } while (0)

int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  char t[q], d[q];
  for (int i = 0; i < q; ++i)
    cin >> t[i] >> d[i];
  int min = -1, max = n;
  while (max - min > 1) {
    int mid = (max + min) / 2;
    int i = mid;
    bool ok = false;
    for (int j = 0; j < q; ++j)
      if (t[j] == s[i]) {
        i += (d[j] == 'R' ? 1 : -1);
        if (i == n)
          break;
        if (i == -1) {
          ok = true;
          break;
        }
      }
    (ok ? min : max) = mid;
  }
  int l = min;
  min = -1;
  max = n;
  while (max - min > 1) {
    int mid = (max + min) / 2;
    int i = n - 1 - mid;
    bool ok = false;
    for (int j = 0; j < q; ++j)
      if (t[j] == s[i]) {
        i += (d[j] == 'R' ? 1 : -1);
        if (i == -1)
          break;
        if (i == n) {
          ok = true;
          break;
        }
      }
    (ok ? min : max) = mid;
  }
  cout << n - l - 2 - min;
  return 0;
}