Problem p03081 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, s, t) for (register int i = s; i <= t; ++i)
#define _rep(i, s, t) for (register int i = s; i >= t; --i)
#define Rep(i, s, t) for (register int i = s; i < t; ++i)
#define go(x) for (register int e = las[x]; e; e = nxt[e])
#define re register
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define is insert
#define es erase
#define pii pair<int, int>
#define ms(f, x) memset(f, x, sizeof f)
#define mc(f, x) memcpy(f, x, sizeof f)
#define open(x) freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
#define gi(x) read(x)
#define gii(x, y) read(x), read(y)
#define giii(x, y, z) read(x), read(y), read(z)
namespace IO {
#define gc getchar()
#define pc(x) putchar(x)
template <typename T> inline void read(T &x) {
  x = 0;
  int f = 1;
  char ch = gc;
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      f = -1;
    ch = gc;
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = gc;
  x *= f;
  return;
}
template <typename T> inline void write(T x = 0) {
  T wr[51];
  wr[0] = 0;
  if (x < 0)
    pc('-'), x = -x;
  if (!x)
    pc(48);
  while (x)
    wr[++wr[0]] = x % 10, x /= 10;
  while (wr[0])
    pc(48 + wr[wr[0]--]);
  return;
}
} // namespace IO
using IO::read;
using IO::write;
typedef long long ll;
typedef double db;
typedef long double ld;
typedef unsigned long long ull;
using namespace std;
const int N = 1e5 + 11;
int n, m;
char c[3];
char s[N], id[N], op[N];
int mx[27], mn[27], p[27], hv[N];
inline bool check1(int x) {
  rep(i, 1, m) {
    if (id[i] == s[x]) {
      if (op[i] == 'L')
        --x;
      else
        ++x;
    }
    if (x < 1)
      return 1;
  }
  return 0;
}
inline bool check2(int x) {
  rep(i, 1, m) {
    if (id[i] == s[x]) {
      if (op[i] == 'L')
        --x;
      else
        ++x;
    }
    if (x > n)
      return 1;
  }
  return 0;
}
int main() {
  scanf("%d%d", &n, &m);
  scanf("%s", s + 1);
  rep(i, 1, m) {
    scanf("%s%s", c, c + 1);
    id[i] = c[0], op[i] = c[1];
  }
  if (check1(n) || check2(1)) {
    puts("0");
    return 0;
  }
  int l = 0;
  _rep(i, log2(n), 0) if (check1(l | (1 << i))) l |= (1 << i);
  int r = 0;
  _rep(i, log2(n), 0) if (check2(n + 1 - (r | (1 << i)))) r |= (1 << i);
  printf("%d\n", n - r - l);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, s, t) for (register int i = s; i <= t; ++i)
#define _rep(i, s, t) for (register int i = s; i >= t; --i)
#define Rep(i, s, t) for (register int i = s; i < t; ++i)
#define go(x) for (register int e = las[x]; e; e = nxt[e])
#define re register
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define is insert
#define es erase
#define pii pair<int, int>
#define ms(f, x) memset(f, x, sizeof f)
#define mc(f, x) memcpy(f, x, sizeof f)
#define open(x) freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
#define gi(x) read(x)
#define gii(x, y) read(x), read(y)
#define giii(x, y, z) read(x), read(y), read(z)
namespace IO {
#define gc getchar()
#define pc(x) putchar(x)
template <typename T> inline void read(T &x) {
  x = 0;
  int f = 1;
  char ch = gc;
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      f = -1;
    ch = gc;
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = gc;
  x *= f;
  return;
}
template <typename T> inline void write(T x = 0) {
  T wr[51];
  wr[0] = 0;
  if (x < 0)
    pc('-'), x = -x;
  if (!x)
    pc(48);
  while (x)
    wr[++wr[0]] = x % 10, x /= 10;
  while (wr[0])
    pc(48 + wr[wr[0]--]);
  return;
}
} // namespace IO
using IO::read;
using IO::write;
typedef long long ll;
typedef double db;
typedef long double ld;
typedef unsigned long long ull;
using namespace std;
const int N = 1e6 + 11;
int n, m;
char c[3];
char s[N], id[N], op[N];
int mx[27], mn[27], p[27], hv[N];
inline bool check1(int x) {
  rep(i, 1, m) {
    if (id[i] == s[x]) {
      if (op[i] == 'L')
        --x;
      else
        ++x;
    }
    if (x < 1)
      return 1;
  }
  return 0;
}
inline bool check2(int x) {
  rep(i, 1, m) {
    if (id[i] == s[x]) {
      if (op[i] == 'L')
        --x;
      else
        ++x;
    }
    if (x > n)
      return 1;
  }
  return 0;
}
int main() {
  scanf("%d%d", &n, &m);
  scanf("%s", s + 1);
  rep(i, 1, m) {
    scanf("%s%s", c, c + 1);
    id[i] = c[0], op[i] = c[1];
  }
  if (check1(n) || check2(1)) {
    puts("0");
    return 0;
  }
  int l = 0;
  _rep(i, log2(n), 0) if (check1(l | (1 << i))) l |= (1 << i);
  int r = 0;
  _rep(i, log2(n), 0) if (check2(n + 1 - (r | (1 << i)))) r |= (1 << i);
  printf("%d\n", n - r - l);
  return 0;
}