Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
constexpr double PI = 3.1415926535897932; // acos(-1)
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

using Pcc = pair<char, char>;
istream &operator>>(istream &is, Pcc &p) {
  char ch1, ch2;
  is >> ch1 >> ch2;
  p = Pcc(ch1, ch2);
  return is;
}
ostream &operator<<(ostream &os, const Pcc &p) {
  return os << p.first << ' ' << p.second << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, q;
  string s;
  cin >> n >> q >> s;
  vector<Pcc> query(n);
  for (int i = 0; i < q; ++i)
    cin >> query[i];

  int lb = -1, ub = n;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    bool ok = true;
    int pos = mid;
    for (int i = 0; i < q; ++i) {
      if (s[pos] != query[i].first)
        continue;
      if (query[i].second == 'L')
        --pos;
      else
        ++pos;
      if (pos < 0) {
        ok = false;
        break;
      }
      if (pos >= n)
        break;
    }
    if (!ok)
      lb = mid;
    else
      ub = mid;
  }
  int left = ub;

  lb = -1, ub = n;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    bool ok = true;
    int pos = mid;
    for (int i = 0; i < q; ++i) {
      if (s[pos] != query[i].first)
        continue;
      if (query[i].second == 'L')
        --pos;
      else
        ++pos;
      if (pos >= n) {
        ok = false;
        break;
      }
      if (pos < 0)
        break;
    }
    if (ok)
      lb = mid;
    else
      ub = mid;
  }
  int right = ub;

  int ans = max(0, right - left);
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
constexpr double PI = 3.1415926535897932; // acos(-1)
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

using Pcc = pair<char, char>;
istream &operator>>(istream &is, Pcc &p) {
  char ch1, ch2;
  is >> ch1 >> ch2;
  p = Pcc(ch1, ch2);
  return is;
}
ostream &operator<<(ostream &os, const Pcc &p) {
  return os << p.first << ' ' << p.second << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, q;
  string s;
  cin >> n >> q >> s;
  vector<Pcc> query(q);
  for (int i = 0; i < q; ++i)
    cin >> query[i];

  int lb = -1, ub = n;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    bool ok = true;
    int pos = mid;
    for (int i = 0; i < q; ++i) {
      if (s[pos] != query[i].first)
        continue;
      if (query[i].second == 'L')
        --pos;
      else
        ++pos;
      if (pos < 0) {
        ok = false;
        break;
      }
      if (pos >= n)
        break;
    }
    if (!ok)
      lb = mid;
    else
      ub = mid;
  }
  int left = ub;

  lb = -1, ub = n;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    bool ok = true;
    int pos = mid;
    for (int i = 0; i < q; ++i) {
      if (s[pos] != query[i].first)
        continue;
      if (query[i].second == 'L')
        --pos;
      else
        ++pos;
      if (pos >= n) {
        ok = false;
        break;
      }
      if (pos < 0)
        break;
    }
    if (ok)
      lb = mid;
    else
      ub = mid;
  }
  int right = ub;

  int ans = max(0, right - left);
  cout << ans << endl;
}