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

#include <iostream>
#include <set>
#include <vector>
using namespace std;

typedef unsigned long long ll;
ll B = 100000007ULL;
ll b[300002];
ll h[300002];
set<ll> s;
int main() {
  int n, m;
  string str, q;
  cin >> n >> m >> str;

  b[0] = 1;
  h[0] = str[0];
  for (int i = 1; i < str.length(); i++) {
    b[i] = b[i - 1] * B;
    h[i] = h[i - 1] * B + str[i];
  }

  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--;
    }
    ll lr = b[r - l + 1] * h[l - 1] - h[r];
    //        cout<<"b[r-l+1]: "<<b[r-l+1]<<"      h[l-1]: "<<h[l-1]<<"    h[r]:
    //        "<<h[r]<<endl; cout<<"l: "<<l<<"    r: "<<r<<"    lr:
    //        "<<lr<<endl<<endl;;
    s.insert(lr);
  }
  cout << s.size() << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m, l = 0, r = 0;
  vector<string> vec;
  string s, q, t;
  cin >> n >> m >> s;

  for (int i = 0; i < m; i++) {
    cin >> q;
    if (q == "R++") {
      r++;
    } else if (q == "R--") {
      r--;
    } else if (q == "L++") {
      l++;
    } else if (q == "L--") {
      l--;
    }
    t = s.substr(l, r - l + 1);
    if (find(vec.begin(), vec.end(), t) == vec.end()) {
      vec.push_back(t);
    }
  }
  cout << vec.size() << endl;
  return 0;
}
Problem p01558 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000
#define EPS 1e-10
#define MOD 1000000007
#define MOD2 1000000009
#define BASE 1007
#define BASE2 1009
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

int n, m, r, l;
string str;
vector<int> v;
set<P> se;
ll hash = 0, hash2 = 0;

ll base_pow(int x) {
  ll ret = 1;
  ll b = BASE;
  while (x) {
    if (x & 1)
      ret = ret * b % MOD;
    b = b * b % MOD;
    x >>= 1;
  }
  return ret;
}

ll base2_pow(int x) {
  ll ret = 1;
  ll b = BASE2;
  while (x) {
    if (x & 1)
      ret = ret * b % MOD2;
    b = b * b % MOD2;
    x >>= 1;
  }
  return ret;
}

int main() {
  cin >> n >> m >> str;
  rep(i, n) v.push_back(str[i] - 'a' + 1);
  hash = v[0];
  hash2 = v[0];
  rep(i, m) {
    string x;
    cin >> x;
    if (x == "R++") {
      r++;
      hash *= BASE;
      hash += v[r];
      hash2 *= BASE2;
      hash2 += v[r];
    }
    if (x == "R--") {
      hash -= v[r];
      hash2 -= v[r];
      r--;
      hash *= base_pow(MOD - 2);
      hash2 *= base2_pow(MOD2 - 2);
    }
    if (x == "L++") {
      hash -= v[l] * base_pow(r - l);
      hash2 -= v[l] * base2_pow(r - l);
      l++;
    }
    if (x == "L--") {
      l--;
      hash += v[l] * base_pow(r - l);
      hash2 += v[l] * base2_pow(r - l);
    }
    hash += 1LL * MOD * 100;
    hash2 += 1LL * MOD2 * 100;
    hash %= MOD;
    hash2 %= MOD2;
    se.insert(P(hash, hash2));
  }
  cout << se.size() << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000
#define EPS 1e-10
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;

int n, m, r, l;
string str;
set<string> se;

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

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

typedef unsigned long long ull;

ull b = 1e9 + 7;
int n, m, r = 1, l = 0;
ull h[300001], p[300001];
string s, q;
set<ull> rh;

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

  p[0] = h[0] = 1;
  for (int i = 0; i < n; i++) {
    p[i + 1] = p[i] * b;
    h[i + 1] = h[i] * b + s[i];
    // cout<<p[i]<<" "<<h[i]<<endl;
  }

  for (int k = 0; k < m; k++) {
    cin >> q;
    if (q == "L++")
      l++;
    else if (q == "L--")
      l--;
    else if (q == "R++")
      r++;
    else
      r--;
    rh.insert(h[r] - h[l] * p[r - l]);
    // cout<<h[r]-h[l]*p[r-l]<<endl;
  }
  cout << rh.size() << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
using namespace std;

int main() {
  int n, m;
  int L[2], R[2];
  string s, q, sub;
  map<string, int> M;

  L[0] = R[0] = 1;
  cin >> n >> m;
  cin >> s;
  for (int k = 1; k < m + 1; k++) {
    cin >> q;
    if (q == "L++") {
      L[k % 2] = L[(k + 1) % 2] + 1;
      R[k % 2] = R[(k + 1) % 2];
    } else if (q == "L--") {
      L[k % 2] = L[(k + 1) % 2] - 1;
      R[k % 2] = R[(k + 1) % 2];
    } else if (q == "R++") {
      L[k % 2] = L[(k + 1) % 2];
      R[k % 2] = R[(k + 1) % 2] + 1;
    } else if (q == "R--") {
      L[k % 2] = L[(k + 1) % 2];
      R[k % 2] = R[(k + 1) % 2] - 1;
    }
    sub = s.substr(L[k % 1] - 1, R[k % 2] - L[k % 2] + 1);
    M[sub]++;
  }
  cout << M.size() << endl;

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

#include <bits/stdc++.h>

using namespace std;

typedef long long ull;

const ull P1 = 1000532779;
const ull P2 = 1777777777;
const ull B1 = 1000000009;
const ull B2 = 1000000007;
int main() {
  char s[300001];
  static pair<ull, ull> hs[300001], pow[300001];
  int n, m;

  scanf("%d %d", &n, &m);
  scanf("%s", s);

  hs[0] = make_pair(0, 0);
  pow[0] = make_pair(1, 1);

  for (int i = 0; s[i]; i++) {
    hs[i + 1] = make_pair((hs[i].first * B1 + s[i]) % P1,
                          (hs[i].second * B2 + s[i]) % P2);
    pow[i + 1] = make_pair(pow[i].first * B1 % P1, pow[i].second * B2 % P2);
  }

  set<pair<ull, ull>> se;

  int left = 0, right = 0;
  for (int i = 0; i < m; i++) {
    char t[4];
    scanf("%s", t);

    if (t[0] == 'L' && t[1] == '+')
      left++;
    if (t[0] == 'R' && t[1] == '+')
      right++;
    if (t[0] == 'L' && t[1] == '-')
      left--;
    if (t[0] == 'R' && t[1] == '-')
      right--;

    pair<ull, ull> L = hs[left], R = hs[right + 1];

    se.insert(make_pair(
        (R.first - (L.first * pow[right - left + 1].first) % P1 + P1) % P1,
        (R.second - (L.second * pow[right - left + 1].second) % P2 + P2) % P2));
  }

  printf("%d\\n", se.size());

  return (0);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ull;

const ull P1 = 1000532779;
const ull P2 = 1777777777;
const ull B1 = 1000000009;
const ull B2 = 1000000007;
int main() {
  char s[100001];
  static pair<ull, ull> hs[100001], pow[100001];
  int n, m;

  scanf("%d %d", &n, &m);
  scanf("%s", s);

  hs[0] = make_pair(0, 0);
  pow[0] = make_pair(1, 1);

  for (int i = 0; s[i]; i++) {
    hs[i + 1] = make_pair((hs[i].first * B1 + s[i]) % P1,
                          (hs[i].second * B2 + s[i]) % P2);
    pow[i + 1] = make_pair(pow[i].first * B1 % P1, pow[i].second * B2 % P2);
  }

  set<pair<ull, ull>> se;

  int left = 0, right = 0;
  for (int i = 0; i < m; i++) {
    char t[4];
    scanf("%s", t);

    if (t[0] == 'L' && t[1] == '+')
      left++;
    if (t[0] == 'R' && t[1] == '+')
      right++;
    if (t[0] == 'L' && t[1] == '-')
      left--;
    if (t[0] == 'R' && t[1] == '-')
      right--;

    pair<ull, ull> L = hs[left], R = hs[right + 1];

    se.insert(make_pair(
        (R.first - (L.first * pow[right - left + 1].first) % P1 + P1) % P1,
        (R.second - (L.second * pow[right - left + 1].second) % P2 + P2) % P2));
  }

  printf("%d\\n", se.size());

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

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if (!(p))
#define until(p) while (!(p))

using ll = long long;
using P = std::tuple<int, int>;

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

const ll MOD = 1000000007ll;

int N, M;
std::string S;
int ls[300100], rs[300100];
ll _hash[300100];

struct RollingHash {
  vector<ll> hash, pow;
  RollingHash(string s, ll _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;
    }
  }
  // s[l]s[l+1]....s[r-1]のハッシュ値を取得する
  // 半開区間
  ll get(int l, int r) { return (hash[r] - hash[l] * pow[r - l]); }
};

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

  std::cin >> N >> M;
  std::cin >> S;

  RollingHash r_hash(S);
  vector<ll> v;

  int cl = 0, cr = 0;
  for (int i = 0; i < M; ++i) {
    string query;
    std::cin >> query;

    if (query == "L++") {
      ++cl;
    } else if (query == "L--") {
      --cl;
    } else if (query == "R++") {
      ++cr;
    } else {
      --cr;
    }

    v.emplace_back(r_hash.get(cl, cr + 1));
  }

  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());

  std::cout << v.size() << std::endl;
}

Error: MLE

Fixed submission:

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

int main(void) {
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;

  int l = 1, r = 1;
  unordered_map<string, bool> ma;
  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 if (q == "R--") {
      r--;
    }
    ma[s.substr(l - 1, r - l + 1)] = true;
  }
  cout << ma.size() << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (int i = 0; i < n; i++)

int main() {
  const ll b = 1e9 + 7;
  ll n, m;
  string s;

  cin >> n >> m >> s;

  vector<ll> hash(n + 1, 0), p(n + 1, 0);
  hash[0] = 1;
  p[0] = 1;
  for (int i = 1; i <= n; i++) {
    hash[i] = hash[i - 1] * b + s[i - 1];
    p[i] = p[i - 1] * b;
  }

  int l = 0, r = 0;
  set<ll> st;
  REP(i, m) {
    string t;
    cin >> t;

    if (t == "L++")
      l++;
    if (t == "L--")
      l--;
    if (t == "R++")
      r++;
    if (t == "R--")
      r--;

    st.insert(hash[r + 1] - hash[l] * p[r - l + 1]);
  }

  cout << st.size() << endl;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define REP2(i, n, k) for (int i = 0; i < n; i += k)

int main() {
  int n, m;
  string s;

  scanf("%d %d", &n, &m);
  cin >> s;

  string t;
  t += s[0];

  set<string> st;
  int l = 0, r = 0;

  REP(i, m) {
    char q[5];
    scanf("%s", q);
    if (q[0] == 'L') {
      if (q[1] == '+') {
        l++;
        t.erase(t.begin());
        st.insert(t);
      } else {
        l--;
        t.insert(t.begin(), s[l]);
        st.insert(t);
      }
    } else {
      if (q[1] == '+') {
        r++;
        t += s[r];
        st.insert(t);
      } else {
        r--;
        t.pop_back();
        st.insert(t);
      }
    }
  }

  cout << st.size() << endl;
}
Problem p01558 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

typedef unsigned long long ull;
const ull b = 1145148931919LL;

int main() {
  const int N = 333333;
  vector<ull> hash(N);
  vector<ull> pow(N);

  int n, m;
  string s;
  cin >> n >> m >> s;

  pow[0] = 1;
  rep(i, n) {
    pow[i + 1] = pow[i] * b;
    hash[i + 1] = hash[i] + s[i] * pow[i];
  }

  int left = 0;
  int right = 0;

  set<ull> visit;
  rep(i, m) {
    string q;
    cin >> q;

    if (q == "L++")
      left++;
    if (q == "L--")
      left--;
    if (q == "R++")
      right++;
    if (q == "R--")
      right--;

    ull val = (hash[right + 1] - hash[left]) * pow[n - left];
    visit.insert(val);
  }

  cout << visit.size() << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

#ifdef __BORLANDC__
#define ll __int64
#else
#define ll long long
#endif

#define ull unsigned ll

const ull b = 1000000007;

ull gethash(string &s, int st, int en) {
  ull ret = 0;
  ull sc = 1;
  reps(i, st, en + 1) {
    ret += s[i] * sc;
    sc *= b;
  }
  return ret;
}

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

  int left = 0;
  int right = 0;

  set<ull> visit;

  rep(i, m) {
    string a;
    cin >> a;

    if (a == "L++")
      left++;
    if (a == "L--")
      left--;
    if (a == "R++")
      right++;
    if (a == "R--")
      right--;

    visit.insert(gethash(s, left, right));
  }
  cout << visit.size() << endl;
}
Problem p01558 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int main() {
  int N, M, L, R, C;
  string S;
  char Q[4];

  scanf("%d", &N);
  scanf("%d", &M);

  cin >> S;

  L = 0;
  R = 0;
  C = 0;

  vector<vector<int>> F(N + 1, vector<int>());

  for (int i = 0; i < M; i++) {
    scanf("%s", Q);

    if (Q[0] == 'L' && Q[1] == '+') {
      L++;
    }
    if (Q[0] == 'L' && Q[1] == '-') {
      L--;
    }
    if (Q[0] == 'R' && Q[1] == '+') {
      R++;
    }
    if (Q[0] == 'R' && Q[1] == '-') {
      R--;
    }

    F[R - L + 1].push_back(L);

    C = max(C, R - L + 1);
  }

  vector<vector<int>> D(N + 1, vector<int>());

  for (int i = 1; i <= N; i++) {
    sort(F[i].begin(), F[i].end());

    if (F[i].size()) {
      D[i].push_back(F[i][0]);
    }

    for (int j = 1; j < F[i].size(); j++) {
      if (F[i][j - 1] != F[i][j]) {
        D[i].push_back(F[i][j]);
      }
    }
  }

  int Answer = 0;

  for (int i = 1; i <= C; i++) {
    set<string> s;

    for (int j = 0; j < D[i].size(); j++) {
      s.insert(S.substr(D[i][j], i));
    }

    Answer += s.size();
  }

  cout << Answer << endl;

  return 0;
}

Error: -11

Fixed submission:

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

using namespace std;

int main() {
  int N, M, L, R, C;
  string S;
  char Q[4];

  scanf("%d", &N);
  scanf("%d", &M);

  cin >> S;

  L = 0;
  R = 0;
  C = 0;

  vector<vector<int>> F(N + 1, vector<int>());

  for (int i = 0; i < M; i++) {
    scanf("%s", Q);

    if (Q[0] == 'L' && Q[1] == '+') {
      L++;
    }
    if (Q[0] == 'L' && Q[1] == '-') {
      L--;
    }
    if (Q[0] == 'R' && Q[1] == '+') {
      R++;
    }
    if (Q[0] == 'R' && Q[1] == '-') {
      R--;
    }

    F[R - L + 1].push_back(L);

    C = max(C, R - L + 1);
  }

  vector<vector<int>> D(N + 1, vector<int>());

  for (int i = 1; i <= N; i++) {
    sort(F[i].begin(), F[i].end());

    D[i].push_back(F[i][0]);

    for (int j = 1; j < F[i].size(); j++) {
      if (F[i][j - 1] != F[i][j]) {
        D[i].push_back(F[i][j]);
      }
    }
  }

  int Answer = 0;

  for (int i = 1; i <= C; i++) {
    set<string> s;

    for (int j = 0; j < D[i].size(); j++) {
      s.insert(S.substr(D[i][j], i));
    }

    Answer += s.size();
  }

  cout << Answer << endl;

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

#include <iostream>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
typedef unsigned long long int ull;

int main() {
  int n, m;
  std::cin >> n >> m;
  string s;
  std::cin >> s;
  ull r = 1, l = 0;
  vector<ull> vb(n + 10, 1), vh(n + 10, 0);
  ull base = 1e9 + 7;
  vh[0] = s[0];
  for (int i = 0; i < n - 1; i++) {
    vb[i + 1] = vb[i] * base;
    vh[i + 1] = vh[i] * base + s[i + 1];
  }
  ull val = s[0];
  set<ull> bucket;
  for (int i = 0; i < m; i++) {
    string ss;
    std::cin >> ss;
    if (ss == "R++") {
      val = val * base + s[r];
      r++;
    } else if (ss == "R--") {
      if (l != 0)
        val = vh[r - 2] - vb[r - l - 1] * vh[l - 1];
      else
        val = vh[r - 2];
      r--;
    } else if (ss == "L++") {
      val = val - s[l] * vb[r - l - 1];
      l++;
    } else {
      val = val + s[l - 1] * vb[r - l];
      l--;
    }
    bucket.insert(val);
  }
  std::cout << bucket.size() << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
typedef unsigned long long int ull;

int main() {
  int n, m;
  std::cin >> n >> m;
  string s;
  std::cin >> s;
  int r = 1, l = 0;
  vector<ull> vb(n, 1);
  ull base = 1e9 + 7;
  for (int i = 0; i < n - 1; i++) {
    vb[i + 1] = vb[i] * base;
  }
  ull val = s[0];
  set<ull> bucket;
  for (int i = 0; i < m; i++) {
    string ss;
    std::cin >> ss;
    if (ss == "R++") {
      val = val * base + s[r];
      r++;
    } else if (ss == "R--") {
      val = 0;
#ifdef _OPENMP
#pragma omp parallel for
#endif
      for (int i = l; i < r - 1; i++) {
        val += vb[i - l] * s[i];
      }
      r--;
    } else if (ss == "L++") {
      val = val - s[l] * vb[r - l - 1];
      l++;
    } else {
      val = val + s[l - 1] * vb[r - l];
      l--;
    }
    // printf("%4s: %d\\n", s.substr(l, r-l).c_str(), val);
    // if(bucket.find(val) != bucket.end()){
    //   printf("find :%4s\\n", s.substr(l, r-l).c_str());
    // }
    if (bucket.find(val) != bucket.end()) {
    }
    bucket.insert(val);
  }
  std::cout << bucket.size() << std::endl;
  return 0;
}
Problem p01558 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull B = 100000007;

int main() {

  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  ull hash[300010];
  ull po[300010];
  hash[0] = 0;
  po[0] = 1;
  int l = 0, r = 1;
  for (int i = 1; i <= n; i++) {
    po[i] = po[i - 1] * B;
    hash[i] = hash[i - 1] * B + (ull)(s[i - 1] - 'a' + 1);
  }

  set<ull> S;

  while (m--) {
    string in;
    cin >> in;

    if (in == "L++")
      l++;
    if (in == "R++")
      r++;
    if (in == "L--")
      l--;
    if (in == "R--")
      r--;

    ull H = hash[r] - hash[l] * po[r - l];
    S.insert(H);
  }

  cout << S.size() << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull B = 100000007;

int main() {

  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  ull hash[100100];
  ull po[100100];
  hash[0] = 0;
  po[0] = 1;
  int l = 0, r = 1;
  for (int i = 1; i <= n; i++) {
    po[i] = po[i - 1] * B;
    hash[i] = hash[i - 1] * B + (ull)(s[i - 1] - 'a' + 1);
  }

  set<ull> S;

  while (m--) {
    string in;
    cin >> in;

    if (in == "L++")
      l++;
    if (in == "R++")
      r++;
    if (in == "L--")
      l--;
    if (in == "R--")
      r--;

    ull H = hash[r] - hash[l] * po[r - l];
    S.insert(H);
  }

  cout << S.size() << endl;

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

#include <bits/stdc++.h>
using namespace std;
struct RollingHash {
  vector<unsigned> hashed, power;
  RollingHash(const string &s, unsigned _base = 1000000007) {
    _base = _base;
    int sz = s.size();
    hashed.assign(sz + 1, 0);
    power.assign(sz + 1, 0);

    power[0] = 1;
    for (int i = 0; i < sz; i++) {
      power[i + 1] = power[i] * _base;
    }
    for (int i = 0; i < sz; i++) {
      hashed[i + 1] = (hashed[i] + s[i]) * _base;
    }
  }
  unsigned get(int l, int r) // [l, r)
  {
    return ((hashed[r] - hashed[l] * power[r - l]));
  }
};

int main() {
  int N, M;
  scanf("%d %d", &N, &M);
  char S[300001];
  scanf(" %s", S);
  RollingHash hash1(S), hash2(S, 1e9 + 9);
  vector<tuple<unsigned, unsigned>> poyo;
  int l = 0, r = 1;
  while (M--) {
    scanf("%s", S);
    if (S[0] == 'R') {
      if (S[1] == '+')
        ++r;
      else
        --r;
    } else {
      if (S[1] == '+')
        ++l;
      else
        --l;
    }
    poyo.emplace_back(hash1.get(l, r), hash2.get(l, r));
  }
  sort(poyo.begin(), poyo.end());
  poyo.erase(unique(poyo.begin(), poyo.end()), poyo.end());
  printf("%d\\n", poyo.size());
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct RollingHash {
  vector<unsigned> hashed, power;
  RollingHash(const string &s, unsigned _base = 1000000007) {
    _base = _base;
    int sz = s.size();
    hashed.assign(sz + 1, 0);
    power.assign(sz + 1, 0);

    power[0] = 1;
    for (int i = 0; i < sz; i++) {
      power[i + 1] = power[i] * _base;
    }
    for (int i = 0; i < sz; i++) {
      hashed[i + 1] = (hashed[i] + s[i]) * _base;
    }
  }
  unsigned get(int l, int r) // [l, r)
  {
    return ((hashed[r] - hashed[l] * power[r - l]));
  }
};

int main() {
  int N, M;
  scanf("%d %d", &N, &M);
  char S[100001];
  scanf(" %s", S);
  RollingHash hash1(S), hash2(S, 1e9 + 9);
  vector<tuple<unsigned, unsigned>> poyo;
  int l = 0, r = 1;
  while (M--) {
    scanf("%s", S);
    if (S[0] == 'R') {
      if (S[1] == '+')
        ++r;
      else
        --r;
    } else {
      if (S[1] == '+')
        ++l;
      else
        --l;
    }
    poyo.emplace_back(hash1.get(l, r), hash2.get(l, r));
  }
  sort(poyo.begin(), poyo.end());
  poyo.erase(unique(poyo.begin(), poyo.end()), poyo.end());
  printf("%d\\n", poyo.size());
}