Problem p03081 (C++)

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

#define REP(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < ll(n); ++i)
#define RFOR(i, m, n) for (ll i = ll(n) - 1; i >= ll(m); --i)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

constexpr int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  string t;
  vi d(q);
  REP(i, q) {
    char a, b;
    cin >> a >> b;
    t += a;
    if (b == 'R')
      d[i] = 1;
    else
      d[i] = -1;
  }

  auto check = [&](int p) {
    char c = s[p];
    int pos = p;
    REP(i, q) {
      if (t[i] == c) {
        pos += d[i];

        if (pos == -1)
          return -1;
        if (pos == n)
          return 1;
        c = s[pos];
      }
    }
    return 0;
  };
  int a = check(0), b = check(n - 1);
  if (a == 0 && b == 0) {
    cout << n << endl;
  } else if (a == 0) {
    int l = 0, r = n;
    while (r - l > 1) {
      int mid = (r + l) / 2;
      if (check(mid) == 0)
        l = mid;
      else
        r = mid;
    }
    cout << n - l - 1 << endl;
  } else if (b == 0) {
    int l = 0, r = n;
    while (r - l > 1) {
      int mid = (r + l) / 2;
      if (check(mid) == 0)
        r = mid;
      else
        l = mid;
    }
    cout << n - r << endl;
  } else {
    int l = 0, r = n;
    int m = -1;
    while (r - l > 1) {

      int mid = (r + l) / 2;
      int c = check(mid);
      if (c == 0) {
        m = mid;
        break;
      } else if (c == 1) {
        r = mid;
      } else {
        l = mid;
      }
    }
    assert(m != -1);
    int res = n;
    l = 0, r = m;
    while (r - l > 1) {

      int mid = (r + l) / 2;
      int c = check(mid);
      if (c == 0)
        r = mid;
      else
        l = mid;
    }
    res -= r;
    l = m, r = n;
    while (r - l > 1) {
      int mid = (r + l) / 2;
      int c = check(mid);
      if (c == 0)
        l = mid;
      else
        r = mid;
    }
    res -= n - r;
    cout << res << endl;
  }
}

Fixed submission:
#include "bits/stdc++.h"

#define REP(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < ll(n); ++i)
#define RFOR(i, m, n) for (ll i = ll(n) - 1; i >= ll(m); --i)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

constexpr int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  string t;
  vi d(q);
  REP(i, q) {
    char a, b;
    cin >> a >> b;
    t += a;
    if (b == 'R')
      d[i] = 1;
    else
      d[i] = -1;
  }

  auto check = [&](int p) {
    char c = s[p];
    int pos = p;
    REP(i, q) {
      if (t[i] == c) {
        pos += d[i];

        if (pos == -1)
          return -1;
        if (pos == n)
          return 1;
        c = s[pos];
      }
    }
    return 0;
  };
  int a = check(0), b = check(n - 1);
  if (a == 0 && b == 0) {
    cout << n << endl;
  } else if (a == 0) {
    int l = 0, r = n;
    while (r - l > 1) {
      int mid = (r + l) / 2;
      if (check(mid) == 0)
        l = mid;
      else
        r = mid;
    }
    cout << n - l - 1 << endl;
  } else if (b == 0) {
    int l = 0, r = n;
    while (r - l > 1) {
      int mid = (r + l) / 2;
      if (check(mid) == 0)
        r = mid;
      else
        l = mid;
    }
    cout << n - r << endl;
  } else {
    int l = 0, r = n;
    int m = -1;
    while (r - l > 1) {

      int mid = (r + l) / 2;
      int c = check(mid);
      if (c == 0) {
        m = mid;
        break;
      } else if (c == 1) {
        r = mid;
      } else {
        l = mid;
      }
    }
    if (m == -1) {
      cout << 0 << endl;
      return 0;
    }
    int res = n;
    l = 0, r = m;
    while (r - l > 1) {

      int mid = (r + l) / 2;
      int c = check(mid);
      if (c == 0)
        r = mid;
      else
        l = mid;
    }
    res -= r;
    l = m, r = n;
    while (r - l > 1) {
      int mid = (r + l) / 2;
      int c = check(mid);
      if (c == 0)
        l = mid;
      else
        r = mid;
    }
    res -= n - r;
    cout << res << endl;
  }
}