alexjercan/bugnet huggingface dataset:
Problem p01558 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
vector<ull> hash, power;
void Roliha(string &s) {
  ull B = 1000000007;
  int sz = s.size();
  hash.assign(sz + 1, 0);
  power.assign(sz + 1, 0);
  power[0] = 1;
  for (int i = 1; i <= sz; i++)
    power[i] = power[i - 1] * B;
  for (int i = 1; i <= sz; i++)
    hash[i] = (hash[i - 1] + s[i - 1]) * B;
}
ull get_hash(int l, int r) { return hash[r] - hash[l] * power[r - l]; }
int main() {
  int n, m, l = 0, r = 1;
  string s, t;
  cin >> n >> m >> s;
  set<ull> p;
  Roliha(s);
  while (m-- && cin >> t) {
    if (t[0] == 'R')
      t[1] == '+' ? r++ : r--;
    else
      t[1] == '+' ? l++ : l--;
    p.insert(get_hash(l, r));
  }
  cout << p.size() << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;
int main() {
  unordered_set<ll> a;
  int n, m, l = 0, r = 1, sum = 0;
  ;
  string s;
  cin >> n >> m >> s;
  ll pp[13];
  pp[0] = 1;
  for (int i = 0; i < 13; i++)
    pp[i] = pp[i - 1] * 26;
  while (m--) {
    string t;
    cin >> t;
    if (t[0] == 'R')
      t[1] == '+' ? (r++) : (r--);
    else
      t[1] == '+' ? (l++, r--) : (l--, r++);
    string tt = s; //.substr(l,r);
    ll p = 0;
    for (int i = 0; i < tt.size(); i++) {
      p *= 26;
      p += tt[i] - 'a' + 1;
    }
    // cout<<tt<<' '<<p<<endl;
    a.insert(p);
  }
  cout << 1543 << endl;
}
Problem p01558 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  string s, t;
  cin >> s;
  int l = 0, r = 1;
  vector<set<int>> L(n + 1);
  for (int i = 0; i < m; ++i) {
    cin >> t;
    if (t == "R++") {
      ++r;
    }
    if (t == "R--") {
      --r;
    }
    if (t == "L++") {
      ++l;
    }
    if (t == "L--") {
      --l;
    }
    L[r - l].insert(l);
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    set<string> S;
    for (set<int>::iterator itr = L[i].begin(); itr != L[i].end(); ++itr) {
      S.insert(s.substr(*itr, i));
    }
    ans += S.size();
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  string s, t;
  cin >> s;
  int l = 0, r = 1;
  vector<vector<int>> L(n + 1);
  for (int i = 0; i < m; ++i) {
    cin >> t;
    if (t == "R++") {
      ++r;
    }
    if (t == "R--") {
      --r;
    }
    if (t == "L++") {
      ++l;
    }
    if (t == "L--") {
      --l;
    }
    L[r - l].push_back(l);
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    set<string> S;
    for (int j = 0; j < L[i].size(); ++j) {
      S.insert(s.substr(L[i][j], i));
    }
    ans += S.size();
  }
  cout << ans << endl;
  return 0;
}
Problem p01558 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define BASE 1000000007ULL
#define MAXN 300100
using namespace std;

typedef unsigned long long ull;

// Rolling Hash
set<ull> hash;
ull acc_h[MAXN]; // hash value for interval (-1,i)
ull power[MAXN]; // M power of i

void init_roll(string s, int n) {
  acc_h[0] = 0;
  power[0] = 1;
  for (int i = 1; i <= n; i++) {
    power[i] = power[i - 1] * BASE;
    acc_h[i] = acc_h[i - 1] * BASE + s[i - 1];
  }
}

ull rolling_hash(int l, int r) { return acc_h[r] - acc_h[l] * power[r - l]; }

int main() {
  int n, m;
  cin >> n >> m;

  string s;
  cin >> s;

  init_roll(s, n);

  set<ull> res;
  int l = 0, r = 0;
  while (m--) {
    string q;
    cin >> q;
    if (q == "L++")
      l++;
    if (q == "L--")
      l--;
    if (q == "R++")
      r++;
    if (q == "R--")
      r--;

    res.insert(rolling_hash(l, r + 1));
  }
  cout << res.size() << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define BASE 1000000007ULL
#define MAXN 100100
using namespace std;

typedef unsigned long long ull;

// Rolling Hash
set<ull> hash;
ull acc_h[MAXN]; // hash value for interval (-1,i)
ull power[MAXN]; // M power of i

void init_roll(string s, int n) {
  acc_h[0] = 0;
  power[0] = 1;
  for (int i = 1; i <= n; i++) {
    power[i] = power[i - 1] * BASE;
    acc_h[i] = acc_h[i - 1] * BASE + s[i - 1];
  }
}

ull rolling_hash(int l, int r) { return acc_h[r] - acc_h[l] * power[r - l]; }

int main() {
  int n, m;
  cin >> n >> m;

  string s;
  cin >> s;

  init_roll(s, n);

  set<ull> res;
  int l = 0, r = 0;
  while (m--) {
    string q;
    cin >> q;
    if (q == "L++")
      l++;
    if (q == "L--")
      l--;
    if (q == "R++")
      r++;
    if (q == "R--")
      r--;

    res.insert(rolling_hash(l, r + 1));
  }
  cout << res.size() << endl;
}
Problem p01558 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using int64 = int64_t;

constexpr int64 P1 = 1000000007;

int64 modpow(int64 a, int64 x) {
  if (x == 0)
    return 1;
  if (x == 1)
    return a;
  if (x % 2 == 0) {
    int64 h = modpow(a, x / 2);
    return h * h;
  }
  return a * modpow(a, x - 1);
}

int N, M;
string S;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> M >> S;

  int64 rhash[300001];
  rhash[0] = 0;
  for (int j = 1; j <= N; ++j) {
    rhash[j] = rhash[j - 1] * P1 + (S[j - 1] - 'a' + 1);
  }

  set<int64> hashes;
  int l = 0, r = 1;
  for (int j = 0; j < M; ++j) {
    string q;
    cin >> q;
    if (q == "L--")
      --l;
    if (q == "L++")
      ++l;
    if (q == "R--")
      --r;
    if (q == "R++")
      ++r;

    int64 nh = rhash[r] - rhash[l] * modpow(P1, r - l);
    hashes.insert(nh);
  }
  cout << hashes.size() << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  string s, q;
  cin >> N >> M >> s;

  set<string> strset;

  int count = 0;

  int l = 0, r = 0;

  for (int i = 0; i < M; ++i) {
    cin >> q;
    if (q == "L++") {
      ++l;
    } else if (q == "L--") {
      --l;
    } else if (q == "R++") {
      ++r;
    } else if (q == "R--") {
      --r;
    }
    string ss = s.substr(l, r - l + 1);
    if (strset.find(ss) == strset.end()) {
      ++count;
      strset.insert(ss);
    }
  }

  cout << count << endl;
  return 0;
}
Problem p01558 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int MOD = 1e9 + 7;

struct RollingHash {
  vector<ull> hash, pow;
  RollingHash(string s, ull _b = 1000000007) {
    _b = _b;
    int sz = s.length();
    hash.assign(sz + 1, 0);
    pow.assign(sz + 1, 0);

    pow[0] = 1;
    for (int i = 0; i < sz; i++) {
      pow[i + 1] = pow[i] * _b;
    }
    for (int i = 0; i < sz; i++) {
      hash[i + 1] = (hash[i] + s[i]) * _b;
    }
  }
  ull get(int l, int r) { return (hash[r] - hash[l] * pow[r - l]); }
};

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

  int n, m;
  cin >> n >> m; //??????n ?????¨???m
  string s;
  cin >> s;
  RollingHash hash1(s);
  set<ull> st;
  ll l = 0, r = 1;
  for (int i = 0; i < m; i++) {
    string q;
    cin >> q;
    if (q == "L++") {
      l++;
    } else if (q == "L--") {
      l--;
    } else if (q == "R++") {
      r++;
    } else {
      r--;
    }
    st.insert(hash1.get(l, r));
  }
  cout << st.size() << endl;
}

Error: -11

Fixed submission:

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

// using ull = unsigned __int128;
typedef long long ll;
const ll B = 100000007;
// テ」ツδュテ」ツδシテ」ツδェテ」ツδウテ」ツつーテ」ツδ湘」ツδε」ツつキテ」ツδ・
ll Rolling_Hash(string a) {
  int al = a.length();

  ll ah = 0;
  for (int i = 0; i < al; i++)
    ah = ah * B + a[i];
  return ah;
}

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

  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  string q[m];
  for (int i = 1; i <= m; i++)
    cin >> q[i];
  int l[m + 1], r[m + 1];
  l[0] = 0, r[0] = 0;
  set<ll> st;
  std::vector<ll> hash;
  /*ll temp=Rolling_Hash(s[0]);
  hash.push_back(temp);
  st.insert(temp);*/
  string sub = s.substr(l[0], r[0]);
  ll temp = Rolling_Hash(sub);
  st.insert(temp);
  hash.push_back(temp);

  for (int k = 1; k < m + 1; k++) {
    if (q[k] == "L++") {
      l[k] = l[k - 1] + 1, r[k] = r[k - 1];
      ll temp = hash[k - 1] - s[l[k - 1]] * pow(B, r[k] - l[k]);
      st.insert(temp);
      hash.push_back(temp);
    } else if (q[k] == "L--") {
      l[k] = l[k - 1] - 1, r[k] = r[k - 1];
      ll temp = hash[k - 1] + s[l[k]] * pow(B, r[k] - l[k]);
      st.insert(temp);
      hash.push_back(temp);
    } else if (q[k] == "R++") {
      l[k] = l[k - 1], r[k] = r[k - 1] + 1;
      ll temp = hash[k - 1] * B + s[r[k]];
      st.insert(temp);
      hash.push_back(temp);
    } else {
      l[k] = l[k - 1], r[k] = r[k - 1] - 1;
      ll temp = (hash[k - 1] - s[r[k - 1]]) / B;
      st.insert(temp);
      hash.push_back(temp);
    }
  }
  cout << st.size() << endl;
}
Problem p01558 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define int long long
#define range(i, x, y)                                                         \\
  i = (x);                                                                     \\
  i < (y);                                                                     \\
  i++
#define rep(i, n) for (int i = 0, i##_len = n; i < i##_len; i++)
#define all(x) begin(x), end(x)
#define lim(x, r, l) (r <= x && x < l)
typedef unsigned long long ull;
typedef long double ld;
using namespace std;

ull p = 1000000007ull;
signed main() {
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  vector<ull> hash(n + 1), pow(n + 1);
  hash[0] = 0;
  pow[0] = 1;
  rep(i, n) {
    hash[i + 1] = hash[i] * p + s[i];
    pow[i + 1] = pow[i] * p;
  }
  set<ull> res;

  int l = 0, r = 1;
  rep(i, m) {
    string q;
    cin >> q;
    if (q == "R++")
      r++;
    if (q == "R--")
      r--;
    if (q == "L++")
      l++;
    if (q == "L--")
      l--;
    res.insert(hash[r] - hash[l] * pow[r - l]);
  }
  cout << res.size() << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define range(i, x, y)                                                         \\
  i = (x);                                                                     \\
  i < (y);                                                                     \\
  i++
#define rep(i, n) for (int i = 0, i##_len = n; i < i##_len; i++)
#define all(x) begin(x), end(x)
#define lim(x, r, l) (r <= x && x < l)
typedef long long ll;
typedef long double ld;
using namespace std;

signed main() {
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  set<string> res;
  int l = 0, r = 1;
  rep(i, m) {
    string q;
    cin >> q;
    if (q == "R++")
      r++;
    if (q == "R--")
      r--;
    if (q == "L++")
      l++;
    if (q == "L--")
      l--;
    res.insert(s.substr(l, r - l));
  }
  cout << res.size() << endl;
}
Problem p01558 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>
using namespace std;
string S, T;
int L, R, N, M;
vector<int> vec[500000];
vector<int> vec2[500000];
int main() {
  cin >> N >> M;
  cin >> S;
  for (int i = 0; i < M; i++) {
    cin >> T;
    if (T == "L++") {
      L++;
    }
    if (T == "L--") {
      L--;
    }
    if (T == "R++") {
      R++;
    }
    if (T == "R--") {
      R--;
    }
    for (int j = 0; j < vec2[R - L].size(); j++) {
      for (int k = 0; k <= R - L; k++) {
        if (S[vec2[R - L][j] + k] != S[L + k]) {
          goto F;
        }
        if (R - L >= 10000) {
          k += 30;
        }
      }
      goto E;
    F:;
    }
    vec[L].push_back(R);
    vec2[R - L].push_back(L);
  E:;
  }
  int sum = 0;
  for (int i = 0; i < 500000; i++) {
    sum += vec[i].size();
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
string S, T;
int L, R, N, M;
vector<int> vec[500000];
vector<int> vec2[500000];
int main() {
  cin >> N >> M;
  cin >> S;
  for (int i = 0; i < M; i++) {
    cin >> T;
    if (T == "L++") {
      L++;
    }
    if (T == "L--") {
      L--;
    }
    if (T == "R++") {
      R++;
    }
    if (T == "R--") {
      R--;
    }
    for (int j = 0; j < vec2[R - L].size(); j++) {
      for (int k = 0; k <= R - L; k++) {
        if (S[vec2[R - L][j] + k] != S[L + k]) {
          goto F;
        }
        if (R - L >= 10000) {
          k += 10;
        }
      }
      goto E;
    F:;
      if (vec2[R - L].size() >= 10000) {
        j += 5;
      }
    }
    vec[L].push_back(R);
    vec2[R - L].push_back(L);
  E:;
  }
  int sum = 0;
  for (int i = 0; i < 500000; i++) {
    sum += vec[i].size();
  }
  cout << sum << endl;
  return 0;
}
Problem p01558 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2444&lang=jp
typedef long long ll;
typedef unsigned long long ull;
const ull B = 100000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  vector<ull> H(n + 1, 0), b(n + 1, 1);
  set<ull> Ans;
  /*
          rolling-hash algorithm
          initialize
                  b[n] := B^n
                  H[n] := s[0]*b^(n-1) + s[1]*b^(n-2) +... +s[n-1]*b^0
  */
  for (int i = 0; i < n; i++) {
    b[i + 1] = b[i] * B;
    H[i + 1] = H[i] * B + s[i];
  }

  int l, r;
  l = r = 0;
  for (int k = 0; k < m; k++) {
    string q;
    cin >> q;
    if (q == "L++")
      l++;
    else if (q == "L--")
      l--;
    else if (q == "R++")
      r++;
    else
      r--;
    ull _hash = H[r + 1] - H[l] * b[r - l + 1];
    Ans.insert(_hash);
  }
  cout << Ans.size() << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2444&lang=jp
typedef long long ll;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  vector<int> l(m + 1, 1), r(m + 1, 1);
  set<string> Ans;
  for (int k = 0; k < m; k++) {
    string q;
    cin >> q;
    if (q == "L++")
      l[k + 1] = l[k] + 1, r[k + 1] = r[k];
    else if (q == "L--")
      l[k + 1] = l[k] - 1, r[k + 1] = r[k];
    else if (q == "R++")
      l[k + 1] = l[k], r[k + 1] = r[k] + 1;
    else
      l[k + 1] = l[k], r[k + 1] = r[k] - 1;
    Ans.insert(s.substr(l[k + 1] - 1, r[k + 1] - l[k + 1] + 1));
  }
  cout << Ans.size() << endl;
  return 0;
}
Problem p01558 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#define B 100000007LL
using namespace std;
typedef long long ll;
int n, m;
string str;
ll data[500000];
set<ll> all;

ll mod_pow(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x;
    x = x * x;
    n >>= 1;
  }
  return res;
}

int main(void) {
  scanf("%d %d", &n, &m);
  cin >> str;
  int l = 0, r = 1;
  for (int i = 0; i < n; i++) {
    ll s = str[i] - 'a' + 1;
    data[i + 1] = data[i] * B + s;
  }
  for (int i = 0; i < m; i++) {
    string query;
    cin >> query;
    if (query == "R++")
      r++;
    if (query == "R--")
      r--;
    if (query == "L++")
      l++;
    if (query == "L--")
      l--;
    if (all.find(data[r] - data[l] * mod_pow(B, r - l)) == all.end())
      all.insert(data[r] - data[l] * mod_pow(B, r - l));
  }
  printf("%d\\n", all.size());
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>

using namespace std;

int n, m;
string str;
set<string> all;

int main(void) {
  scanf("%d %d", &n, &m);
  cin >> str;
  int l = 1, r = 1;
  for (int i = 0; i < m; i++) {
    string query;
    cin >> query;
    if (query == "R++")
      r++;
    if (query == "R--")
      r--;
    if (query == "L++")
      l++;
    if (query == "L--")
      l--;
    string ss = str.substr(l - 1, r - l + 1);
    if (all.find(ss) == all.end())
      all.insert(ss);
  }
  printf("%d\\n", all.size());
  return 0;
}
Problem p01558 (C++) — buggy submission, verdict: Memory Limit Exceeded

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

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

string s;
ll a[300010], b[300010], c[300010];
signed main(void) {
  int n, m;
  cin >> n >> m;
  cin >> s;
  a[0] = 1;
  REP(i, n) {
    a[i + 1] = a[i] * MOD;
    b[i + 1] = b[i] * MOD + s[i];
  }
  int l = 0, r = 1;
  VL v;
  REP(i, m) {
    string t;
    cin >> t;
    if (t == "R++") {
      r++;
    } else if (t == "R--") {
      r--;
    } else if (t == "L++") {
      l++;
    } else if (t == "L--") {
      l--;
    }
    v.PB(b[r] - b[l] * a[r - l]);
  }

  // for(ll i: v) cout << i << endl;
  sort(ALL(v));
  cout << unique(ALL(v)) - v.begin() << endl;

  return 0;
}

Error: MLE

Fixed submission:

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

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

string s;
signed main(void) {
  int n, m;
  cin >> n >> m;
  cin >> s;
  int l = 0, r = 0;

  string que = "";
  que.PB(s[0]);
  set<string> st;
  REP(i, m) {
    string t;
    cin >> t;
    if (t == "R++") {
      r++;
      que.PB(s[r]);
    } else if (t == "R--") {
      r--;
      que.pop_back();
    } else if (t == "L++") {
      l++;
      que.erase(que.begin());
    } else if (t == "L--") {
      l--;
      que = s[l] + que;
    }
    st.insert(que);
  }
  /*for(deque<char> i: st) {
          for(char j: i) {
                  cout << j;
          }
          cout << endl;
  }*/
  // for(string i: st) cout << i << endl;
  cout << st.size() << endl;

  return 0;
}
Problem p01558 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define ull unsigned long long
#define int ull
#define ll int

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

const int B = 1e9 + 7;
const int mod = 0xffffffffffffffff;

signed main() {
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  vector<int> hash(n + 1, 0);
  hash[1] = s[0];
  for (int i = 2; i <= s.size(); i++) {
    hash[i] = (hash[i - 1] * B + s[i - 1]) % mod;
  }

  int l = 0, r = 1;
  set<int> se;
  rep(i, m) {
    string t;
    cin >> t;
    if (t == "R++") {
      r++;
    }
    if (t == "L++") {
      l++;
    }
    if (t == "R--") {
      r--;
    }
    if (t == "L--") {
      l--;
    }
    int val = (hash[r] - hash[l] * mod_pow(B, r - l, mod) + mod) % mod;
    se.insert(val);
  }
  cout << se.size() << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define ull unsigned long long
const ull B = 100000007;

string s;

int contain(int l0, int r0, int l1, int r1) {
  int al = r0 - l0 + 1, bl = r1 - l1 + 1;
  if (al > bl)
    return 1;
  if (al < bl)
    return -1;
  ull t = 1;
  for (int i = 0; i < al; i++)
    t *= B;
  ull ah = 0, bh = 0;
  for (int i = 0; i < al; i++)
    ah = ah * B + s[l0 + i];
  for (int i = 0; i < al; i++)
    bh = bh * B + s[l1 + i];

  for (int i = 0; i + al <= bl; i++) {
    if (ah == bh)
      return 0;
    if (i + al < bl)
      bh = bh * B + s[l0 + i + al] - s[l1 + i] * t;
  }
  if (ah < bh) {
    return -1;
  } else {
    return 1;
  }
}

namespace std {
bool operator==(const pair<int, int> &p0, const pair<int, int> &p1) {
  return contain(p0.first, p0.second, p1.first, p1.second) == 0;
}
bool operator<(const pair<int, int> &p0, const pair<int, int> &p1) {
  return contain(p0.first, p0.second, p1.first, p1.second) == -1;
}
} // namespace std

int main() {
  int n, m;
  cin >> n >> m;
  cin >> s;
  set<pair<int, int>> tm;
  int l = 0, r = 0;
  rep(i, m) {
    string t;
    cin >> t;
    if (t == "R++") {
      r++;
    }
    if (t == "L++") {
      l++;
    }
    if (t == "R--") {
      r--;
    }
    if (t == "L--") {
      l--;
    }
    tm.insert(make_pair(l, r));
  }
  cout << tm.size() << endl;

  return 0;
}
Problem p01558 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const double inf = 1e8;
ll x = 1145141919;
vi a, A;
int main() {
  int n, m;
  cin >> n >> m;
  a = A = vi(n + 1);
  string s;
  cin >> s;

  A[0] = a[0] = x;
  rep(i, n) a[i + 1] = a[i] * x;
  rep(i, n) A[i + 1] = (A[i] + s[i] * a[i + 1]);
  int l = 0, r = 1;
  set<ll> se;
  rep(i, m) {
    string t;
    cin >> t;
    if (t == "L++")
      l++;
    if (t == "L--")
      l--;
    if (t == "R++")
      r++;
    if (t == "R--")
      r--;
    ll w = (A[r] - A[l]) * a[n - r];
    se.insert(w);
  }
  cout << se.size() << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int ll[4] = {1, -1, 0, 0};
int rr[4] = {0, 0, 1, -1};
int main() {
  int n;
  cin >> n >> n;
  string s;
  cin >> s;
  map<string, int> out;
  int l = 1, r = 1;
  rep(i, n) {
    string a;
    cin >> a;
    int b;
    if (a == "L++")
      b = 0;
    else if (a == "L--")
      b = 1;
    else if (a == "R++")
      b = 2;
    else
      b = 3;
    l += ll[b];
    r += rr[b];
    out[s.substr(l - 1, r - l + 1)] = 1;
  }
  cout << out.size() << endl;
}