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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

#define cost first
#define state second
#define all(c) (c).begin(), (c).end()

using namespace std;

typedef pair<int, vector<int>> p2;

map<vector<int>, int> dist2;

int n;
queue<p2> que;

map<vector<int>, int> bfs(vector<int> start) {

  map<vector<int>, int> dist;

  que.push(p2(0, start));
  dist[start] = 0;
  while (!que.empty()) {
    p2 now = que.front();
    que.pop();

    if (dist.find(now.state) != dist.end() && dist[now.state] < now.cost)
      continue;
    if (now.cost >= (n - 1) / 2)
      continue;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        vector<int> t = now.state;
        reverse(t.begin() + i, t.begin() + j + 1);
        if (dist.find(t) == dist.end()) {
          dist[t] = now.cost + 1;
          que.push(p2(now.cost + 1, t));
        }
      }
    }
  }
  return dist;
}

int solve(vector<int> start) {
  map<vector<int>, int> a, b;
  a = bfs(start);
  vector<int> goal = start;
  sort(all(goal));
  b = bfs(goal);

  int ans = start.size() - 1;
  if (a.find(goal) != a.end())
    ans = min(ans, a[goal]);

  do {
    if (a.find(goal) == a.end())
      continue;
    if (b.find(goal) == b.end())
      continue;
    ans = min(ans, a[goal] + b[goal]);
  } while (next_permutation(all(goal)));

  return ans;
}

int main(void) {

  cin >> n;

  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];

  cout << solve(v) << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

#define cost first
#define state second
#define all(c) (c).begin(), (c).end()

using namespace std;

typedef pair<int, vector<int>> p2;

map<vector<int>, int> dist2;

int n;
queue<p2> que;

map<vector<int>, int> bfs(vector<int> start) {

  map<vector<int>, int> dist;

  que.push(p2(0, start));
  dist[start] = 0;
  while (!que.empty()) {
    p2 now = que.front();
    que.pop();

    if (dist.find(now.state) != dist.end() && dist[now.state] < now.cost)
      continue;
    dist[now.state] = now.cost;
    if (now.cost >= (n - 1) / 2)
      continue;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        vector<int> t = now.state;
        reverse(t.begin() + i, t.begin() + j + 1);
        if (dist.find(t) == dist.end())
          que.push(p2(now.cost + 1, t));
      }
    }
  }
  return dist;
}

int solve(vector<int> start) {
  map<vector<int>, int> a, b;
  a = bfs(start);
  vector<int> goal = start;
  sort(all(goal));
  b = bfs(goal);

  int ans = start.size() - 1;
  if (a.find(goal) != a.end())
    ans = min(ans, a[goal]);

  do {
    if (a.find(goal) == a.end())
      continue;
    if (b.find(goal) == b.end())
      continue;
    ans = min(ans, a[goal] + b[goal]);
  } while (next_permutation(all(goal)));

  return ans;
}

int main(void) {

  cin >> n;

  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];

  cout << solve(v) << endl;

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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;
typedef vector<int> vi;
typedef map<vi, int> ma;

vi a, b;
int N;

ma bfs(vi vec) {
  ma tem;
  queue<vi> que;
  que.push(vec);
  tem[vec] = 0;
  while (!que.empty()) {

    vi v = que.front();
    que.pop();
    //        cout<<"tem[v]: "<<tem[v]<<endl;
    //        for(int i=0;i<v.size();i++)
    //            cout<<v[i]<<" ";
    //        cout<<endl;
    if (tem[v] >= (N - 1) / 2)
      continue;
    for (int i = 0; i < N; i++)
      for (int j = i + 2; j <= N; j++) {
        vi n = v;
        reverse(n.begin() + i, n.begin() + j);
        if (tem.find(n) == tem.end()) {
          tem[n] = tem[v] + 1;
          que.push(n);
        }
      }
  }
  return tem;
}

void solve() {
  ma m_a = bfs(a);
  ma m_b = bfs(b);

  if (m_a.find(b) != m_a.end()) {
    cout << m_a[b] << endl;
    return;
  }
  int ans = N - 1;
  do {
    if (m_a.find(b) == m_a.end() || m_b.find(b) == m_b.end())
      continue;
    else
      ans = min(m_a[b] + m_b[b], ans);
  } while (next_permutation(b.begin(), b.end()));
  cout << ans << endl;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    int t;
    cin >> t;
    a.push_back(t);
  }
  b = a;
  sort(b.begin(), b.end());
  solve();
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;
typedef vector<int> vi;
typedef map<vi, int> ma;

vi a, b;
int N;

ma bfs(vi vec) {
  ma tem;
  queue<vi> que;
  que.push(vec);
  tem[vec] = 0;
  while (!que.empty()) {

    vi v = que.front();
    que.pop();
    //        cout<<"tem[v]: "<<tem[v]<<endl;
    //        for(int i=0;i<v.size();i++)
    //            cout<<v[i]<<" ";
    //        cout<<endl;
    if (tem[v] > (N - 1) / 2)
      continue;
    for (int i = 0; i < N; i++)
      for (int j = i + 2; j <= N; j++) {
        vi n = v;
        reverse(n.begin() + i, n.begin() + j);
        if (tem.find(n) == tem.end()) {
          tem[n] = tem[v] + 1;
          que.push(n);
        }
      }
  }
  return tem;
}

void solve() {
  ma m_a = bfs(a);
  ma m_b = bfs(b);

  if (m_a.find(b) != m_a.end()) {
    cout << m_a[b] << endl;
    return;
  }
  int ans = 1000000;
  do {
    if (m_a.find(b) == m_a.end() || m_b.find(b) == m_b.end())
      continue;
    else
      ans = min(m_a[b] + m_b[b], ans);
  } while (next_permutation(b.begin(), b.end()));
  cout << ans << endl;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    int t;
    cin >> t;
    a.push_back(t);
  }
  b = a;
  sort(b.begin(), b.end());
  solve();
  return 0;
}
Problem p01557 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int N;
  cin >> N;
  vi a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    --a[i];
  }
  vi b = a;
  sort(b.begin(), b.end());
  if (a == b) {
    puts("0");
    return 0;
  }
  queue<vi> q[2];
  map<vi, int> d[2];
  q[0].push(a);
  d[0][a] = 0;
  q[1].push(b);
  d[1][b] = 0;
  int dist = 0;
  while (++dist) {
    for (int i = 0; i < 2; i++) {
      while (!q[i].empty() && d[i][q[i].front()] < dist) {
        auto v = q[i].front();
        q[i].pop();
        for (int x = 0; x < N; x++) {
          for (int y = x + 2; y <= N; y++) {
            auto tmp = v;
            reverse(tmp.begin() + x, tmp.begin() + y);
            if (d[i].count(tmp) == 0) {
              q[i].push(tmp);
              d[i][tmp] = d[i][v] + 1;
              if (d[!i].count(tmp)) {
                cout << d[0][tmp] + d[1][tmp] << endl;
                return 0;
              }
            }
          }
        }
      }
    }
  }
  assert(false);
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;

vi rev(vi in, int s, int t) {
  reverse(in.begin() + s, in.begin() + t);
  return in;
}

int main() {
  int N;
  cin >> N;
  vi A(N), g(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    g[i] = i + 1;
  }
  if (A == g) {
    cout << 0 << endl;
    return 0;
  }
  vector<vi> q;
  q.push_back(A);
  bool flag = false;
  map<vi, int> mp;
  mp[A] = 1;
  for (int i = 0; !q.empty(); i++) {
    auto tmp = q;
    q.clear();
    for (auto v : tmp) {
      for (int i = 0; i < N; i++) {
        for (int j = i + 1; j < N; j++) {
          auto t = rev(v, i, j + 1);
          if (t == g) {
            flag = true;
            goto to;
          }
          if (mp[t]) {
            continue;
          }
          mp[t] = 1;
          q.push_back(t);
        }
      }
    }
  to:
    if (flag) {
      cout << i + 1 << endl;
      break;
    }
  }
  return 0;
}
Problem p01557 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int N;
vector<int> A;

bool check(vector<int> &a) {
  for (int i = 0; i < N; i++)
    if (a[i] != i)
      return false;
  return true;
}
int ind(vector<int> &a, int x) {
  for (int i = 0; i < N; i++)
    if (a[i] == x)
      return i;
}
void view(vector<int> &a) {
  for (int i = 0; i < N; i++)
    cout << a[i] << " ";
  cout << endl;
}

typedef pair<vector<int>, int> P;

void solve(map<vector<int>, int> &M, vector<int> &A) {
  M.clear();
  queue<P> q;
  q.push(P(A, 0));
  M[A] = 0;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    if (p.second == 4)
      return;
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        vector<int> a = p.first;
        reverse(a.begin() + i, a.begin() + j + 1);
        if (M.count(a) == 0) {
          M[a] = p.second + 1;
          q.push(P(a, p.second + 1));
        }
      }
    }
  }
}

int main() {
  while (cin >> N) {
    A.resize(N);
    for (int i = 0; i < N; i++) {
      cin >> A[i];
      A[i]--;
    }
    map<vector<int>, int> M1, M2;
    solve(M1, A);
    sort(A.begin(), A.end());
    solve(M2, A);
    int res = 9;
    for (auto it = M1.begin(); it != M1.end(); it++)
      if (M2.count(it->first) != 0)
        res = min(res, it->second + M2[it->first]);
    cout << res << endl;
  }
}

Error: MLE

Fixed submission:

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

int N;
vector<int> A;

bool check(vector<int> &a) {
  for (int i = 0; i < N; i++)
    if (a[i] != i)
      return false;
  return true;
}
int ind(vector<int> &a, int x) {
  for (int i = 0; i < N; i++)
    if (a[i] == x)
      return i;
}
void view(vector<int> &a) {
  for (int i = 0; i < N; i++)
    cout << a[i] << " ";
  cout << endl;
}

typedef pair<vector<int>, int> P;
map<vector<int>, int> M;

int solve() {
  if (check(A))
    return 0;
  M.clear();
  queue<P> q;
  q.push(P(A, 0));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        vector<int> a = p.first;
        reverse(a.begin() + i, a.begin() + j + 1);
        if (M.count(a) == 0) {
          if (check(a))
            return p.second + 1;
          M[a] = p.second + 1;
          q.push(P(a, p.second + 1));
        }
      }
    }
  }
}

int main() {
  while (cin >> N) {
    A.resize(N);
    for (int i = 0; i < N; i++) {
      cin >> A[i];
      A[i]--;
    }
    cout << solve() << endl;
  }
}
Problem p01557 (C++) — buggy submission, verdict: Runtime Error

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

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int H(vector<int> &a) {
  int ret = 0, f = 1;
  for (int i = a.size() - 1; i >= 0; i--) {
    int cnt = 0;
    for (int j = i; j < a.size(); j++)
      if (a[i] > a[j])
        cnt++;
    ret += f * cnt;
    f *= a.size() - i;
  }
  return ret;
}

void bfs(vint A, vint &cost) {
  fill(all(cost), 1001001001);
  cost[H(A)] = 0;
  queue<vint> que;
  que.push(A);
  while (que.size()) {
    vint a = que.front();
    que.pop();
    int h0 = H(a);
    if (cost[h0] == 4)
      break;
    for (int i = 0; i < a.size(); i++) {
      for (int j = i + 1; j <= a.size(); j++) {
        reverse(a.begin() + i, a.begin() + j);
        int h = H(a);
        if (cost[h] == 1001001001) {
          cost[h] = cost[h0] + 1;
          que.push(a);
        }
        reverse(a.begin() + i, a.begin() + j);
      }
    }
  }
}

signed main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  vector<int> B(N);
  rep(i, N) B[i] = i + 1;

  vector<int> costA(3628800), costB(3628800);
  bfs(A, costA);
  bfs(B, costB);

  int ans = 9;
  rep(i, 3628800) chmin(ans, costA[i] + costB[i]);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
typedef pair<string, int> P;
int main() {
  int n;
  cin >> n;
  string start, goal;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    start += a + '0' - 1;
  }
  for (int i = 0; i < n; i++) {
    goal += '0' + i;
  }
  map<string, int> M;
  queue<P> Q;
  Q.push(P(start, 0));
  int f = -1;
  while (Q.size()) {
    P p = Q.front();
    Q.pop();
    if (M[p.first] == 1)
      continue;
    M[p.first] = 1;
    if (p.first == goal) {
      f = p.second;
      break;
    }

    for (int i = 0; i < n - 1; i++) {
      for (int j = i + 2; j <= n; j++) {
        string str = p.first;
        reverse(str.begin() + i, str.begin() + j);
        Q.push(P(str, p.second + 1));
      }
    }
  }
  if (f != -1)
    cout << f << endl;
  else
    cout << "NA" << endl;
}
Problem p01557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<vector<int>, int> P;

int n, d, idx;

ll Vtoll(vector<int> V) {

  ll res = 0;

  for (int i = 0; i < V.size(); i++)
    res = res * 10 + V[i];

  return res;
}

map<ll, int> memo[2];

queue<P> q;

void bfs(vector<int> s) {

  q.push(P(s, 0));

  memo[idx][Vtoll(s)] = 0;

  while (!q.empty()) {

    P t = q.front();
    q.pop();

    int cost = t.second;
    vector<int> S = t.first;

    if (cost == d)
      continue;

    for (int i = 0; i < n - 1; i++)
      for (int j = i + 2; j <= n; j++) {

        vector<int> nS = S;
        reverse(nS.begin() + i, nS.begin() + j);

        if (memo[idx].count(Vtoll(nS)))
          continue;
        memo[idx][Vtoll(nS)] = cost + 1;

        q.push(P(nS, cost + 1));
      }
  }
}

int main() {

  cin >> n;

  vector<int> A;

  A.resize(n);

  for (int i = 0; i < n; i++)
    cin >> A[i], A[i]--;

  d = (n - 1) / 2;

  idx = 0;

  bfs(A);

  d = (n - 1) / 2;

  idx = 1;

  for (int i = 0; i < n; i++)
    A[i] = i;

  bfs(A);

  int ans = n - 1;

  map<ll, int>::iterator ite = memo[0].begin();

  while (ite != memo[0].end()) {

    if (memo[1].count((*ite).first))
      ans = min(ans, (*ite).second + memo[1][(*ite).first]);

    ite++;
  }

  cout << ans << endl;

  return 0;
}

/*

SAK

*/

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<vector<int>, int> P;

int n, d, idx;

ll Vtoll(vector<int> V) {

  ll res = 0;

  for (int i = 0; i < V.size(); i++)
    res = res * 10 + V[i];

  return res;
}

unordered_map<ll, int> memo[2];

queue<P> q;

void bfs(vector<int> s) {

  q.push(P(s, 0));

  memo[idx][Vtoll(s)] = 0;

  while (!q.empty()) {

    P t = q.front();
    q.pop();

    int cost = t.second;
    vector<int> S = t.first;

    if (cost == d)
      continue;

    for (int i = 0; i < n - 1; i++)
      for (int j = i + 2; j <= n; j++) {

        vector<int> nS = S;
        reverse(nS.begin() + i, nS.begin() + j);

        if (memo[idx].count(Vtoll(nS)))
          continue;
        memo[idx][Vtoll(nS)] = cost + 1;

        q.push(P(nS, cost + 1));
      }
  }
}

int main() {

  cin >> n;

  vector<int> A;

  A.resize(n);

  for (int i = 0; i < n; i++)
    cin >> A[i], A[i]--;

  d = n / 2;

  idx = 0;

  bfs(A);

  d = n / 2;

  idx = 1;

  for (int i = 0; i < n; i++)
    A[i] = i;

  bfs(A);

  int ans = n - 1;

  unordered_map<ll, int>::iterator ite = memo[0].begin();

  while (ite != memo[0].end()) {

    if (memo[1].count((*ite).first))
      ans = min(ans, (*ite).second + memo[1][(*ite).first]);

    ite++;
  }

  cout << ans << endl;

  return 0;
}

/*

SAK

*/
Problem p01557 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define range(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
map<vector<int>, int> memo[2];
int main(void) {
  int n;
  cin >> n;

  vector<int> a(n);
  for (auto &e : a)
    cin >> e;

  int nn = (n - 1) / 2;

  int res = n - 1;
  rep(i, 2) {
    queue<vector<int>> q;
    q.push(a);
    memo[i][a] = 0;

    while (q.size()) {
      vector<int> b;
      b = q.front();
      q.pop();

      if (i == 1 and memo[0].find(b) != end(memo[0])) {
        res = min(res, memo[0][b] + memo[1][b]);
        continue;
      }

      if (memo[i][b] >= nn)
        continue;

      rep(j, n) {
        range(k, j + 1, n) {
          vector<int> nb = b;
          reverse(begin(nb) + j, begin(nb) + k + 1);

          if (memo[i].find(nb) != end(memo[i]))
            continue;

          memo[i][nb] = memo[i][b] + 1;
          q.push(nb);
        }
      }
    }

    sort(begin(a), end(a));
  }

  cout << res << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
map<vector<int>, int> memo;
int main(void) {
  int n;
  cin >> n;

  vector<int> a(n);
  for (auto &e : a)
    cin >> e;

  int nn = (n - 1) / 2;

  int res = n - 1;
  rep(i, 2) {
    queue<pair<int, vector<int>>> q;
    q.push(make_pair(0, a));

    while (q.size()) {
      int cnt;
      vector<int> b;
      cnt = q.front().first, b = q.front().second;
      // cerr << cnt << ": ";
      // for(auto & e : b) cerr << e << " ";
      // cerr << endl;
      q.pop();

      if (cnt == nn)
        continue;

      if (memo.find(b) != end(memo)) {
        if (i == 0)
          continue;
        else {
          res = memo[b] + cnt;
          break;
        }
      }
      if (i == 0)
        memo[b] = cnt;

      rep(j, n) {
        range(k, j + 1, n) {
          vector<int> nb = b;
          reverse(begin(nb) + j, begin(nb) + k + 1);

          q.push(make_pair(cnt + 1, nb));
        }
      }
    }

    rep(j, n) a[j] = j + 1;
  }

  cout << res << endl;

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
using Map = map<vector<int>, int>;

int N;

vector<int> flip(vector<int> &vec, int &l, int &r) {
  vector<int> ret = vec;

  for (int i = l; i <= r; i++) {
    int il = i, ir = r - (i - l);
    if (il >= ir)
      break;

    swap(ret[il], ret[ir]);
  }
  return ret;
}

int solve(vector<int> &init, vector<int> &term, Map &cur, Map &pre) {
  int ret = N - 1;
  queue<vector<int>> que;
  que.push(init);

  while (que.size()) {
    vector<int> vec = que.front();
    que.pop();
    if (pre.count(vec)) {
      ret = min(ret, cur[vec] + pre[vec]);
    }
    if (cur[vec] >= (N - 1) / 2)
      continue;

    for (int l = 0; l < N; l++) {
      for (int r = l + 1; r < N; r++) {
        vector<int> nxt = flip(vec, l, r);
        if (cur.count(nxt))
          continue;
        cur[nxt] = cur[vec] + 1;
        que.push(nxt);
      }
    }
  }

  return ret;
}

int main() {
  scanf("%d", &N);
  vector<int> perm(N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &perm[i]);
  }

  vector<int> term(N);
  iota(term.begin(), term.end(), 1);

  Map ml, mr;
  ml[perm] = 0;
  mr[term] = 0;

  int ans = N - 1;
  ans = min(ans, solve(perm, term, ml, mr));
  ans = min(ans, solve(term, perm, mr, ml));
  printf("%d\\n", ans);
  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <map>
#include <numeric>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
using Map = map<vector<int>, int>;

int N;

vector<int> flip(vector<int> &vec, int &l, int &r) {
  vector<int> ret = vec;

  for (int i = l; i <= r; i++) {
    int il = i, ir = r - (i - l);
    if (il >= ir)
      break;

    swap(ret[il], ret[ir]);
  }
  return ret;
}

int solve(vector<int> &init, vector<int> &term, Map &cur, Map &pre) {
  int ret = N - 1;
  queue<vector<int>> que;
  que.push(init);

  while (que.size()) {
    vector<int> vec = que.front();
    que.pop();
    if (cur[vec] >= N / 2)
      continue;
    if (pre.count(vec)) {
      ret = min(ret, cur[vec] + pre[vec]);
    }

    for (int l = 0; l < N; l++) {
      for (int r = l + 1; r < N; r++) {
        vector<int> nxt = flip(vec, l, r);
        if (cur.count(nxt))
          continue;
        cur[nxt] = cur[vec] + 1;
        que.push(nxt);
      }
    }
  }

  return ret;
}

int main() {
  scanf("%d", &N);
  vector<int> perm(N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &perm[i]);
  }

  vector<int> term(N);
  iota(term.begin(), term.end(), 1);

  Map ml, mr;
  ml[perm] = 0;
  mr[term] = 0;

  int ans = N - 1;
  ans = min(ans, solve(perm, term, ml, mr));
  ans = min(ans, solve(term, perm, mr, ml));
  printf("%d\\n", ans);
  return 0;
}
Problem p01558 (C++) — buggy submission, verdict: Runtime Error

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

typedef unsigned long long ulli;

const ulli B = 1000000007ULL;
const int MAXN = 300005;
int n, m;
string s;
ulli Bn[MAXN];
ulli sum_h[MAXN];

int main() {
  for (int i = 0; i < MAXN; ++i) {
    if (i == 0)
      Bn[i] = 1;
    else
      Bn[i] = Bn[i - 1] * B;
  }
  while (cin >> n >> m) {
    cin >> s;
    sum_h[0] = 0;
    for (int i = 0; i < n; ++i) {
      sum_h[i + 1] = sum_h[i] + s[i] * Bn[i];
    }
    set<ulli> t;
    int l = 0, r = 0;
    while (m--) {
      string q;
      cin >> q;
      if (0) {
      } else if (q == "L++") {
        l++;
      } else if (q == "L--") {
        l--;
      } else if (q == "R++") {
        r++;
      } else if (q == "R--") {
        r--;
      }
      ulli hash = (sum_h[r + 1] - sum_h[l]) * Bn[n - r];
      t.insert(hash);
    }
    cout << t.size() << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

typedef unsigned long long ulli;

const ulli B = 1000000007ULL;
const int MAXN = 100005;
int n, m;
string s;
ulli Bn[MAXN];
ulli sum_h[MAXN];

int main() {
  for (int i = 0; i < MAXN; ++i) {
    if (i == 0)
      Bn[i] = 1;
    else
      Bn[i] = Bn[i - 1] * B;
  }
  while (cin >> n >> m) {
    cin >> s;
    sum_h[0] = 0;
    for (int i = 0; i < n; ++i) {
      sum_h[i + 1] = sum_h[i] + s[i] * Bn[i];
    }
    set<ulli> t;
    int l = 0, r = 0;
    while (m--) {
      string q;
      cin >> q;
      if (0) {
      } else if (q == "L++") {
        l++;
      } else if (q == "L--") {
        l--;
      } else if (q == "R++") {
        r++;
      } else if (q == "R--") {
        r--;
      }
      ulli hash = (sum_h[r + 1] - sum_h[l]) * Bn[n - r];
      t.insert(hash);
    }
    cout << t.size() << endl;
  }
  return 0;
}