prithivMLmods/Coder-Stat huggingface dataset:
Problem p01934 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;

#define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second
#define pb push_back
#define REC(ret, ...) std::function<ret(__VA_ARGS__)>
template <typename T> inline bool chmin(T &l, T r) {
  bool a = l > r;
  if (a)
    l = r;
  return a;
}
template <typename T> inline bool chmax(T &l, T r) {
  bool a = l < r;
  if (a)
    l = r;
  return a;
}

typedef vector<LL> V;
typedef vector<V> VV;
const LL INF = 1e15;
int a[112345];
int b[112345];
LL c[112345];
LL d[112345];
int main() {
  int N, M, s, t;
  cin >> N >> M >> s >> t;
  s--;
  t--;
  V dist(N, INF);
  VV g(N);
  REP(i, N) cin >> d[i];
  REP(i, M) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    c[i] = d[b[i]];
    g[a[i]].pb(i);
  }
  REP(i, N - 1) {
    a[i + M] = i + 1;
    b[i + M] = i;
    c[i + M] = 0;
    g[a[i + M]].pb(i + M);
  }
  dist[s] = 0;
  priority_queue<pair<LL, int>> Q;
  Q.push({0ll, s});
  while (Q.size()) {
    auto q = Q.top();
    Q.pop();
    LL cost = -q.fi;
    int v = q.se;
    if (cost > dist[v])
      continue;
    for (auto &e : g[v]) {
      int u = b[e];
      LL nxtcost = c[e] + cost;
      if (chmin(dist[u], nxtcost)) {
        Q.push({-nxtcost, u});
      }
    }
  }
  cout << dist[t] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;

#define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second
#define pb push_back
#define REC(ret, ...) std::function<ret(__VA_ARGS__)>
template <typename T> inline bool chmin(T &l, T r) {
  bool a = l > r;
  if (a)
    l = r;
  return a;
}
template <typename T> inline bool chmax(T &l, T r) {
  bool a = l < r;
  if (a)
    l = r;
  return a;
}

typedef vector<LL> V;
typedef vector<V> VV;
const LL INF = 1e15;
int a[212345];
int b[212345];
LL c[212345];
LL d[212345];
int main() {
  int N, M, s, t;
  cin >> N >> M >> s >> t;
  s--;
  t--;
  V dist(N, INF);
  VV g(N);
  REP(i, N) cin >> d[i];
  REP(i, M) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    c[i] = d[b[i]];
    g[a[i]].pb(i);
  }
  REP(i, N - 1) {
    a[i + M] = i + 1;
    b[i + M] = i;
    c[i + M] = 0;
    g[a[i + M]].pb(i + M);
  }
  dist[s] = 0;
  priority_queue<pair<LL, int>> Q;
  Q.push({0ll, s});
  while (Q.size()) {
    auto q = Q.top();
    Q.pop();
    LL cost = -q.fi;
    int v = q.se;
    if (cost > dist[v])
      continue;
    for (auto &e : g[v]) {
      int u = b[e];
      LL nxtcost = c[e] + cost;
      if (chmin(dist[u], nxtcost)) {
        Q.push({-nxtcost, u});
      }
    }
  }
  cout << dist[t] << endl;
  return 0;
}
Problem p01939 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

static const int MOD = 1000000007;

int n, m;
int ns, f, d;
int ans;

signed main() {

  cin >> n >> m;

  ns = ((m + 1) / (n - 1)) % MOD;
  f = (m + 1 - (n - 1)) % MOD;
  d = -(n - 1) % MOD;

  ans = (ns * ((2 * f + ((ns - 1) * d) % MOD) % MOD) % MOD + m + 1) % MOD;

  cout << ans << endl;
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

static const int MOD = 1000000007;

int n, m;
int ns, f, d;
int ans;

signed main() {

  cin >> n >> m;

  if (n == 1) {
    ns = 0;
  } else
    ns = ((m + 1) / (n - 1)) % MOD;
  f = (m + 1 - (n - 1)) % MOD;
  d = -(n - 1) % MOD;

  ans = (ns * ((2 * f + ((ns - 1) * d) % MOD) % MOD) % MOD + m + 1) % MOD;

  cout << ans << endl;
  return 0;
}
Problem p01939 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll mod = 1000000007;
  ll n;
  ll m;
  cin >> n >> m;
  // ll ks=m/(n-1)
  if (n == 1) {
    assert(0);
    cout << m + 1 << endl;
    return 0;
  }

  ll sy = m + 1;
  ll ma = sy % (n - 1);
  ll ks = sy / (n - 1);
  //  if(ma)ks++;
  ks++;
  cout << ((((sy + ma) % mod) * (ks % mod)) % mod - sy % mod + mod) % mod
       << endl;
  //  cout << (m+1)%mod*((m+1)/(n-1))%mod<< endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll mod = 1000000007;
  ll n;
  ll m;
  cin >> n >> m;
  // ll ks=m/(n-1)
  if (n == 1) {
    cout << (m + 1) % mod << endl;
    return 0;
  }

  ll sy = m + 1;
  ll ma = sy % (n - 1);
  ll ks = sy / (n - 1);
  //  if(ma)ks++;
  ks++;
  cout << ((((sy + ma) % mod) * (ks % mod)) % mod - sy % mod + mod) % mod
       << endl;
  //  cout << (m+1)%mod*((m+1)/(n-1))%mod<< endl;
  return 0;
}
Problem p01940 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

int main(void) {
  string S;
  string P;
  cin >> S;
  cin >> P;

  int search = 0;
  int count = 0;
  bool finish = false;
  int preserch = 0;
  bool many_same_char = false;
  vector<tuple<int, int>> st;

  for (int ptr = 0; !finish; ptr++) {
    if (S[ptr] == P[search]) {
      preserch = search;
      if (search >= P.length() - 1) {
        int oldcount = count;
        for (int subptr = ptr; subptr < S.length(); subptr++) {
          if (S[subptr] == P[search]) {
            count++;
          }
        }
        if (count - oldcount == 1) {
          if (many_same_char) {
            count = 2000;
          } else {
            count = 1;
          }
          finish = true;
        }

        if (!st.empty()) {
          tuple<int, int> t = st.back();
          st.pop_back();
          ptr = get<0>(t);
          search = get<1>(t);
        } else {
          finish = true;
        }

        if (count >= 2) {
          finish = true;
        }
      } else {
        st.push_back(make_tuple(ptr, search));
        search++;
      }
    } else if (ptr >= S.length() - 1) {
      if (!st.empty()) {
        tuple<int, int> t = st.back();
        st.pop_back();
        ptr = get<0>(t);
        search = get<1>(t);
      } else {
        finish = true;
      }
    } else if (S[ptr] == P[preserch]) {
      many_same_char = true;
    }
  }

  if (count == 1) {
    cout << "yes" << endl;
  } else {
    cout << "no" << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

int main(void) {
  string S;
  string P;
  cin >> S;
  cin >> P;

  int search = 0;
  int count = 0;
  bool finish = false;
  int preserch = 0;
  bool many_same_char = false;
  vector<tuple<int, int>> st;

  for (int ptr = 0; !finish; ptr++) {
    if (S[ptr] == P[search]) {
      preserch = search;
      if (search >= P.length() - 1) {
        int oldcount = count;
        for (int subptr = ptr; subptr < S.length(); subptr++) {
          if (S[subptr] == P[search]) {
            count++;
          }
        }
        if (count - oldcount == 1) {
          if (many_same_char) {
            count = 2000;
          } else {
            count = 1;
          }
          finish = true;
        }

        if (!st.empty()) {
          tuple<int, int> t = st.back();
          st.pop_back();
          ptr = get<0>(t);
          search = get<1>(t);
        } else {
          finish = true;
        }

        if (count >= 2) {
          finish = true;
        }
      } else {
        st.push_back(make_tuple(ptr, search));
        search++;
      }
    } else if (ptr >= S.length() - 1) {
      finish = true;
    } else if (S[ptr] == P[preserch]) {
      many_same_char = true;
    }
  }

  if (count == 1) {
    cout << "yes" << endl;
  } else {
    cout << "no" << endl;
  }

  return 0;
}
Problem p01967 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 101

int capacity[NUM], table[NUM];

int main() {

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

  for (int i = 1; i <= N; i++) {
    scanf("%d", &capacity[i]);
    table[i] = 0;
  }

  int Q;
  scanf("%d", &Q);

  int command, apple_id, num;

  for (int loop = 0; loop < Q; loop++) {
    scanf("%d %d %d", &command, &apple_id, &num);

    if (command == 1) {
      table[apple_id] += num;
      if (table[apple_id] > capacity[apple_id]) {
        printf("%d\n", apple_id);
        return 0;
      }
    } else {
      table[apple_id] -= num;
      if (table[apple_id] < 0) {
        printf("%d\n", apple_id);
        return 0;
      }
    }
  }

  printf("0\n");

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 1001

int capacity[NUM], table[NUM];

int main() {

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

  for (int i = 1; i <= N; i++) {
    scanf("%d", &capacity[i]);
    table[i] = 0;
  }

  int Q;
  scanf("%d", &Q);

  int command, apple_id, num;

  for (int loop = 0; loop < Q; loop++) {
    scanf("%d %d %d", &command, &apple_id, &num);

    if (command == 1) {
      table[apple_id] += num;
      if (table[apple_id] > capacity[apple_id]) {
        printf("%d\n", apple_id);
        return 0;
      }
    } else {
      table[apple_id] -= num;
      if (table[apple_id] < 0) {
        printf("%d\n", apple_id);
        return 0;
      }
    }
  }

  printf("0\n");

  return 0;
}
Problem p01970 (C++)

Buggy submission (status: Runtime Error):
// {{{
#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 ll = long long;

const ll MOD = 1000000007;

char prime[1000005];
void init_prime() {
  fill(prime, prime + 1000005, 1);
  prime[0] = prime[1] = 0;
  for (int x = 2; x <= 1000000; ++x) {
    if (prime[x]) {
      for (int y = x * x; y <= 1000000; y += x)
        prime[y] = 0;
    }
  }
}

ll N, Q[101010];

ll memo[101010];

int main() {
  init_prime();
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  for (int j = 1; j <= N; ++j)
    cin >> Q[j];
  reverse(Q + 1, Q + N + 1);

  fill(memo, memo + N + 1, 0);
  memo[0] = 1;
  memo[1] = prime[Q[1]] ? 1 : 0;

  for (int j = 2; j <= N; ++j) {
    if (prime[Q[j]]) {
      if (Q[j - 1] > Q[j]) {
        memo[j] = (memo[j] + memo[j - 1]) % MOD;
      }

      if (Q[j - 1] > 1 && (j < 3 || (prime[Q[j - 2]] && Q[j - 2] > Q[j]))) {
        memo[j] = (memo[j] + memo[j - 2]) % MOD;
      }
    }
  }

  cout << memo[N] << endl;

  return 0;
}


Fixed submission:
// {{{
#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 ll = long long;

const ll MOD = 1000000007;

char prime[1000005];
void init_prime() {
  fill(prime, prime + 1000005, 1);
  prime[0] = prime[1] = 0;
  for (int x = 2; x <= 1000000; ++x) {
    if (prime[x] && x <= 1000) {
      for (int y = x * x; y <= 1000000; y += x)
        prime[y] = 0;
    }
  }
}

ll N, Q[101010];

ll memo[101010];

int main() {
  init_prime();
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  for (int j = 1; j <= N; ++j)
    cin >> Q[j];
  reverse(Q + 1, Q + N + 1);

  fill(memo, memo + N + 1, 0);
  memo[0] = 1;
  memo[1] = prime[Q[1]] ? 1 : 0;

  for (int j = 2; j <= N; ++j) {
    if (prime[Q[j]]) {
      if (Q[j - 1] > Q[j]) {
        memo[j] = (memo[j] + memo[j - 1]) % MOD;
      }

      if (Q[j - 1] > 1 && (j < 3 || (prime[Q[j - 2]] && Q[j - 2] > Q[j]))) {
        memo[j] = (memo[j] + memo[j - 2]) % MOD;
      }
    }
  }

  cout << memo[N] << endl;

  return 0;
}
Problem p01970 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;

bool prime[1000009];

void era() {
  prime[0] = 1;
  prime[1] = 1;
  for (int i = 2; i < 1000009; i++) {
    if (!prime[i]) {
      for (int j = i + i; j < 1000009; j += i)
        prime[j] = 1;
    }
  }
}

int dp[100009][2];

signed main() {
  era();
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  if (!prime[a[0]]) {
    dp[0][0] = 1;
    dp[0][1] = 1;
  }
  for (int i = 0; i < n; i++) {

    // 1

    if (i + 1 < n && a[i] < a[i + 1] && !prime[a[i + 1]])
      dp[i + 1][0] += dp[i][0];
    if (i + 2 < n && a[i] < a[i + 1] && !prime[a[i + 1]])
      dp[i + 1][1] += dp[i][0];

    // 2

    if (i + 2 < n && a[i] < a[i + 2] && !prime[a[i + 2]])
      dp[i + 2][0] += dp[i][1];
    if (i + 3 < n && a[i] < a[i + 2] && !prime[a[i + 2]])
      dp[i + 2][1] += dp[i][1];

    dp[i + 1][0] %= 1000000007;
    dp[i + 1][1] %= 1000000007;
    dp[i + 2][0] %= 1000000007;
    dp[i + 2][1] %= 1000000007;
  }
  cout << (dp[n - 1][0] + dp[n - 2][1]) % 1000000007 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;

bool prime[1000009];

void era() {
  prime[0] = 1;
  prime[1] = 1;
  for (int i = 2; i < 1000009; i++) {
    if (!prime[i]) {
      for (int j = i + i; j < 1000009; j += i)
        prime[j] = 1;
    }
  }
}

int dp[100009][2];

signed main() {
  era();
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    scanf("%ld", &a[i]);
  if (!prime[a[0]]) {
    dp[0][0] = 1;
    dp[0][1] = 1;
  }
  for (int i = 0; i < n; i++) {

    // 1

    if (i + 1 < n && a[i] < a[i + 1] && !prime[a[i + 1]])
      dp[i + 1][0] += dp[i][0];
    if (i + 2 < n && a[i] < a[i + 1] && !prime[a[i + 1]])
      dp[i + 1][1] += dp[i][0];

    // 2

    if (i + 2 < n && a[i] < a[i + 2] && !prime[a[i + 2]])
      dp[i + 2][0] += dp[i][1];
    if (i + 3 < n && a[i] < a[i + 2] && !prime[a[i + 2]])
      dp[i + 2][1] += dp[i][1];

    dp[i + 1][0] %= 1000000007;
    dp[i + 1][1] %= 1000000007;
    dp[i + 2][0] %= 1000000007;
    dp[i + 2][1] %= 1000000007;
  }
  cout << (dp[n - 1][0] + dp[n - 2][1]) % 1000000007 << endl;
}
Problem p01973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)

using ull = unsigned long long;
const ull b = 1000000007;

ull f(string s) {
  ull h = 0;
  rep(i, s.size()) h = b * h + s[i];
  return h;
}

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

  string s;
  cin >> s;
  int n = s.size();
  int q;
  cin >> q;
  vector<unordered_set<ull>> hashs(n + 1);
  rep(i, q) {
    string t;
    cin >> t;
    int m = t.size();
    if (m > n)
      continue;
    hashs[m].insert(f(t));
  }

  vector<int> l(n, -1);
  ull bb = 1;
  for (int len = 1; len <= n; len++) {
    bb *= b;
    ull h = f(s.substr(0, len));
    int r = len - 1;
    while (true) {
      if (hashs[len].count(h) && l[r] == -1) {
        l[r] = r - len + 1;
      }
      r++;
      if (r >= n)
        break;
      h = b * h - bb * s[r - len] + s[r];
    }
  }

  int cnt = 0;
  int r = -1;
  rep(i, n) {
    if (r < l[i]) {
      cnt++;
      r = i;
    }
  }

  cout << cnt << endl;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)

using ull = unsigned long long;
const ull b = 1000000007;

ull f(string s) {
  ull h = 0;
  rep(i, s.size()) h = b * h + s[i];
  return h;
}

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

  string s;
  cin >> s;
  int n = s.size();
  int q;
  cin >> q;
  vector<unordered_set<ull>> hashs(n + 1);
  rep(i, q) {
    string t;
    cin >> t;
    int m = t.size();
    if (m > n)
      continue;
    hashs[m].insert(f(t));
  }

  vector<int> l(n, -1);
  ull bb = 1;
  for (int len = 1; len <= n; len++) {
    bb *= b;
    if (hashs[len].empty())
      continue;
    ull h = f(s.substr(0, len));
    int r = len - 1;
    while (true) {
      if (hashs[len].count(h) && l[r] == -1) {
        l[r] = r - len + 1;
      }
      r++;
      if (r >= n)
        break;
      h = b * h - bb * s[r - len] + s[r];
    }
  }

  int cnt = 0;
  int r = -1;
  rep(i, n) {
    if (r < l[i]) {
      cnt++;
      r = i;
    }
  }

  cout << cnt << endl;
}
Problem p01981 (Python)

Buggy submission (status: Runtime Error):
while True:
    a = list(map(str, input().split()))
    if a[0] == "0":
        break
    elif int(a[1]) <= 30:
        print(" ".join(a))
    elif int(a[1]) == 31 and int(a[2]) <= 4:
        print(" ".join(a))
    else:
        a[0] = "?"
        s = int(a[1])
        s -= 30
        a[1] = str(s)
        print(" ".join(a))


Fixed submission:
while True:
    a = list(map(str, input().split()))
    if a[0] == "#":
        break
    elif int(a[1]) <= 30:
        print(" ".join(a))
    elif int(a[1]) == 31 and int(a[2]) <= 4:
        print(" ".join(a))
    else:
        a[0] = "?"
        s = int(a[1])
        s -= 30
        a[1] = str(s)
        print(" ".join(a))
Problem p01985 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int SIZE = 100010;

vector<int> way[SIZE];
bool visited[SIZE];
int id[SIZE];
int counter[2];

bool dfs(int now, int f) {
  if (visited[now]) {
    return id[now] == f;
  }

  visited[now] = true;
  id[now] = f;
  counter[f]++;

  for (int i = 0; i < (int)way[now].size(); i++)
    if (dfs(way[now][i], !f) == false)
      return false;

  return true;
}

bool solve() {
  int n, m;
  int a, b;

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

  if (n == 0)
    return false;

  for (int i = 0; i < n; i++)
    way[i] = vector<int>();
  memset(visited, 0, sizeof(visited));
  counter[0] = counter[1] = 0;

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    a--;
    b--;

    way[a].push_back(b);
    way[b].push_back(a);
  }

  if (!dfs(0, 0)) {
    puts("0");
  } else {
    cerr << counter[0] << " " << counter[1] << endl;
    if (counter[0] > counter[1])
      swap(counter[0], counter[1]);

    vector<int> vec;

    if (counter[0] % 2 == 0) {
      vec.push_back(counter[0] / 2);
    }

    if (counter[0] != counter[1] && counter[1] % 2 == 0) {
      vec.push_back(counter[1] / 2);
    }

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

    for (int i = 0; i < vec.size(); i++) {
      printf("%d\n", vec[i]);
    }
  }

  return true;
}

int main() {
  while (solve())
    ;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int SIZE = 100010;

vector<int> way[SIZE];
bool visited[SIZE];
int id[SIZE];
int counter[2];

bool dfs(int now, int f) {
  if (visited[now]) {
    return id[now] == f;
  }

  visited[now] = true;
  id[now] = f;
  counter[f]++;

  for (int i = 0; i < (int)way[now].size(); i++)
    if (dfs(way[now][i], !f) == false)
      return false;

  return true;
}

bool solve() {
  int n, m;
  int a, b;

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

  if (n == 0)
    return false;

  for (int i = 0; i < n; i++)
    way[i] = vector<int>();
  memset(visited, 0, sizeof(visited));
  counter[0] = counter[1] = 0;

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    a--;
    b--;

    way[a].push_back(b);
    way[b].push_back(a);
  }

  if (!dfs(0, 0)) {
    puts("0");
  } else {
    // cerr << counter[0] << " " << counter[1] << endl;
    if (counter[0] > counter[1])
      swap(counter[0], counter[1]);

    vector<int> vec;

    if (counter[0] % 2 == 0) {
      vec.push_back(counter[0] / 2);
    }

    if (counter[0] != counter[1] && counter[1] % 2 == 0) {
      vec.push_back(counter[1] / 2);
    }

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

    for (int i = 0; i < vec.size(); i++) {
      printf("%d\n", vec[i]);
    }
  }

  return true;
}

int main() {
  while (solve())
    ;
}
Problem p02022 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6;
const ll LINF = 1e18;
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (int i = (a); i < (b); i++)
#define rrrep(i, a, b) for (int i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl

ll n, cnt, ans, a[ohara], b[ohara], c, d, tmp, tmpp, m, h, w, x, y, sum, pos, k;
ld doua;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};
// int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"), s;
bool fl;
struct edge {
  int to, cost;
};

//-------------------------↓↓↓↓↓↓------------------------

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

  cin >> n >> m;
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  rep(i, n) {
    rep(j, m) { ans += a[i] * b[j]; }
  }
  Cout(ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6;
const ll LINF = 1e18;
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (int i = (a); i < (b); i++)
#define rrrep(i, a, b) for (int i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl

ll n, cnt, ans, a[ohara], b[ohara], c, d, tmp, tmpp, m, h, w, x, y, sum, pos, k;
ld doua;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};
// int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"), s;
bool fl;
struct edge {
  int to, cost;
};

//-------------------------↓↓↓↓↓↓------------------------

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

  cin >> n >> m;
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i], sum += b[i];
  rep(i, n) { ans += a[i] * sum; }
  Cout(ans);
  return 0;
}
Problem p02022 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define int long long
using namespace std;
int a[1919], b[1919];
signed main() {
  int n, m, cnt = 0, cnta = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int j = 0; j < m; j++)
    cin >> b[j];
  for (int i = 0; i < n; i++) {
    cnt += a[i];
  }
  for (int j = 0; j < m; j++) {
    cnta += b[j];
  }
  cout << cnt * cnta << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define int long long
using namespace std;
int a[191919], b[191919];
signed main() {
  int n, m, cnt = 0, cnta = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int j = 0; j < m; j++)
    cin >> b[j];
  for (int i = 0; i < n; i++) {
    cnt += a[i];
  }
  for (int j = 0; j < m; j++) {
    cnta += b[j];
  }
  cout << cnt * cnta << endl;
}
Problem p02022 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

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

  long long a[1010];
  long long ans = 0;
  long long tmp = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  long long b;
  for (int i = 0; i < m; i++) {
    cin >> b;
    tmp += b;
  }

  for (int i = 0; i < n; i++) {
    ans += a[i] * tmp;
  }

  cout << ans << endl;

  return 0;
}


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

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

  long long a[100010];
  long long ans = 0;
  long long tmp = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  long long b;
  for (int i = 0; i < m; i++) {
    cin >> b;
    tmp += b;
  }

  for (int i = 0; i < n; i++) {
    ans += a[i] * tmp;
  }

  cout << ans << endl;

  return 0;
}
Problem p02026 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;

int a[200000], b[200000];
ll d[200000];
vector<P> E[200000];

int main() {
  int n;
  cin >> n;
  vector<P> A, B;
  rep(i, n) scanf("%d", &a[i]), A.push_back(P(a[i], i));
  rep(i, n) scanf("%d", &b[i]), B.push_back(P(b[i], i));
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  rep(i, n - 1) {
    E[A[i + 1].second].push_back(P(A[i + 1].first - A[i].first, A[i].second));
    E[A[i].second].push_back(P(A[i + 1].first - A[i].first, A[i + 1].second));
    E[B[i + 1].second].push_back(P(B[i + 1].first - B[i].first, B[i].second));
    E[B[i].second].push_back(P(B[i + 1].first - B[i].first, B[i + 1].second));
  }
  memset(d, 0x3f, sizeof(d));
  priority_queue<P> que;
  d[0] = 0;
  que.push(P(0, 0));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (d[p.second] != p.first)
      continue;
    for (auto u : E[p.second]) {
      if (p.first + u.first < d[u.second]) {
        d[u.second] = p.first + u.first;
        que.push(P(d[u.second], u.second));
      }
    }
  }
  rep(i, n) { printf("%lld\n", d[i]); }
}


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

int a[200000], b[200000];
ll d[200000];
vector<P> E[200000];

int main() {
  int n;
  cin >> n;
  vector<P> A, B;
  rep(i, n) scanf("%d", &a[i]), A.push_back(P(a[i], i));
  rep(i, n) scanf("%d", &b[i]), B.push_back(P(b[i], i));
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  rep(i, n - 1) {
    E[A[i + 1].second].push_back(P(A[i + 1].first - A[i].first, A[i].second));
    E[A[i].second].push_back(P(A[i + 1].first - A[i].first, A[i + 1].second));
    E[B[i + 1].second].push_back(P(B[i + 1].first - B[i].first, B[i].second));
    E[B[i].second].push_back(P(B[i + 1].first - B[i].first, B[i + 1].second));
  }
  memset(d, 0x3f, sizeof(d));
  priority_queue<P, vector<P>, greater<P>> que;
  d[0] = 0;
  que.push(P(0, 0));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (d[p.second] != p.first)
      continue;
    for (auto u : E[p.second]) {
      if (p.first + u.first < d[u.second]) {
        d[u.second] = p.first + u.first;
        que.push(P(d[u.second], u.second));
      }
    }
  }
  rep(i, n) { printf("%lld\n", d[i]); }
}
Problem p02030 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

int main(void) {
  int n, m;
  scanf("%d %d", &n, &m);
  int a[n];
  int b[m];
  vector<int> dup, all;

  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (int j = 0; j < m; j++) {
    scanf("%d", &b[j]);
  }

  int ap = 0;
  int bp = 0;
  int i = 1;
  while (ap != n or bp != m) {

    if (a[ap] == i and b[bp] == i)
      dup.push_back(i);
    if (a[ap] == i or b[bp] == i)
      all.push_back(i);

    while (a[ap] <= i and ap != n)
      ap++;
    while (b[bp] <= i and bp != m)
      bp++;

    if (ap != n and a[ap] < b[bp]) {
      i = a[ap];
    } else if (bp != n and a[ap] >= b[bp]) {
      i = b[bp];
    } else if (ap == n) {
      i = b[bp];
    } else {
      i = a[ap];
    }
  }

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

  for (auto itr = dup.begin(); itr != dup.end(); ++itr) {
    printf("%d\n", *itr);
  }
  for (auto itr = all.begin(); itr != all.end(); ++itr) {
    printf("%d\n", *itr);
  }

  return 0;
}


Fixed submission:
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

int main(void) {
  int n, m;
  scanf("%d %d", &n, &m);
  int a[n];
  int b[m];
  vector<int> dup, all;

  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (int j = 0; j < m; j++) {
    scanf("%d", &b[j]);
  }

  int ap = 0;
  int bp = 0;
  int i = 1;
  while (ap != n or bp != m) {

    if (a[ap] == i and b[bp] == i)
      dup.push_back(i);
    if (a[ap] == i or b[bp] == i)
      all.push_back(i);

    while (a[ap] <= i and ap != n)
      ap++;
    while (b[bp] <= i and bp != m)
      bp++;

    if (ap != n and a[ap] < b[bp]) {
      i = a[ap];
    } else if (bp != m and a[ap] >= b[bp]) {
      i = b[bp];
    } else if (ap == n) {
      i = b[bp];
    } else {
      i = a[ap];
    }
  }

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

  for (auto itr = dup.begin(); itr != dup.end(); ++itr) {
    printf("%d\n", *itr);
  }
  for (auto itr = all.begin(); itr != all.end(); ++itr) {
    printf("%d\n", *itr);
  }

  return 0;
}
Problem p02104 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n, p, ans[100005];
deque<int> q, q2[100005];

int main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    scanf("%d", &p);
    q2[p - 1].push_back(i + 1);
  }

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

      while (!q2[j].empty()) {
        q.push_front(q2[j].back());
        q2[j].pop_back();
      }

      if (ans[j] == 0) {
        ans[j] = q.front();
        q.pop_front();
      }
    }
  }

  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;

  return 0;
}

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

int n, p, ans[100005];
deque<int> q, q2[100005];

int main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    scanf("%d", &p);
    q2[p - 1].push_back(i + 1);
  }

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

      while (!q2[j].empty()) {
        q.push_front(q2[j].back());
        q2[j].pop_back();
      }

      if (ans[j] == 0 && !q.empty()) {
        ans[j] = q.front();
        q.pop_front();
      }
    }
  }

  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;

  return 0;
}
Problem p02115 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long N, a[1100], d = 0, ans1 = 0, ans2 = 0;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }
  for (int i = 2; i <= N; i++) {
    if (a[i] > a[i - 1]) {
      d++;
      if (d >= ans1) {
        ans1 = d;
      }
    }
    if (a[i] < a[i - 1]) {
      d = 0;
    }
  }
  d = 0;
  for (int i = 2; i <= N; i++) {
    if (a[i] < a[i - 1]) {
      d++;
      if (d >= ans2) {
        ans2 = d;
      }
    }
    if (a[i] > a[i - 1]) {
      d = 0;
    }
  }
  cout << max(ans1, ans2) + 1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long N, a[1000000], d = 0, ans1 = 0, ans2 = 0;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }
  for (int i = 2; i <= N; i++) {
    if (a[i] > a[i - 1]) {
      d++;
      if (d >= ans1) {
        ans1 = d;
      }
    }
    if (a[i] < a[i - 1]) {
      d = 0;
    }
  }
  d = 0;
  for (int i = 2; i <= N; i++) {
    if (a[i] < a[i - 1]) {
      d++;
      if (d >= ans2) {
        ans2 = d;
      }
    }
    if (a[i] > a[i - 1]) {
      d = 0;
    }
  }
  cout << max(ans1, ans2) + 1 << endl;
  return 0;
}
Problem p02116 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

ll calc(ll x, ll y) {
  if (y == 0) {
    return x;
  } else {
    return calc(y, x % y);
  }
}

int main() {

  ll N, POW[70];

  POW[0] = 1;
  POW[1] = 2;
  for (int i = 2; i <= 69; i++) {
    POW[i] = POW[i - 1] * 2;
  }

  scanf("%lld", &N);

  for (int i = 0; i <= 69; i++) {
    if (POW[i] - 1 == N) {
      printf("%lld\n", N + 1);
      return 0;
    }
  }

  if (N % 2 == 0) {
    printf("1\n");
    return 0;
  }

  vector<ll> bunshi;

  bunshi.push_back(N);

  ll tmp, common;

  for (ll m = 2;; m += 2) {

    ll tmp = N - (m - 1);

    if (tmp >= m) {
      common = calc(tmp, m);
    } else {
      common = calc(m, tmp);
    }

    tmp /= common;

    if (tmp % 2 == 0) {
      printf("%lld\n", m);
      return 0;
    }
  }

  printf("%lld\n", N + 1);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

ll calc(ll x, ll y) {
  if (y == 0) {
    return x;
  } else {
    return calc(y, x % y);
  }
}

int main() {

  ll N, POW[70];

  POW[0] = 1;
  POW[1] = 2;
  for (int i = 2; i <= 69; i++) {
    POW[i] = POW[i - 1] * 2;
  }

  scanf("%lld", &N);

  for (int i = 0; i <= 69; i++) {
    if (POW[i] - 1 == N) {
      printf("%lld\n", N + 1);
      return 0;
    }
  }

  if (N % 2 == 0) {
    printf("1\n");
    return 0;
  }

  vector<ll> bunshi;

  bunshi.push_back(N);

  ll tmp, common;

  for (ll m = 2;; m *= 2) {

    ll tmp = N - (m - 1);

    if (tmp >= m) {
      common = calc(tmp, m);
    } else {
      common = calc(m, tmp);
    }

    tmp /= common;

    if (tmp % 2 == 0) {
      printf("%lld\n", m);
      return 0;
    }
  }

  printf("%lld\n", N + 1);

  return 0;
}
Problem p02126 (C++)

Buggy submission (status: Runtime Error):
#include "algorithm"
#include "bitset"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

const long long int MOD = 1000000007;

long long int N, M, K, H, W, L, R;

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

  cin >> N >> M >> K;
  vector<queue<int>> v(1001);
  vector<int> num(N);
  for (int i = 0; i < K; i++) {
    cin >> num[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> L >> R;
    L--;
    v[R].push(L);
  }
  int ans = 0;
  for (int i = 1000; i > 0; i--) {
    while (!v[i].empty()) {
      int box = v[i].front();
      v[i].pop();
      if (num[box]) {
        num[box]--;
        M--;
        ans += i;
        if (!M) {
          cout << ans << endl;
          return 0;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include "algorithm"
#include "bitset"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

const long long int MOD = 1000000007;

long long int N, M, K, H, W, L, R;

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

  cin >> N >> M >> K;
  vector<queue<int>> v(1001);
  vector<int> num(K);
  for (int i = 0; i < K; i++) {
    cin >> num[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> L >> R;
    L--;
    v[R].push(L);
  }
  int ans = 0;
  for (int i = 1000; i > 0; i--) {
    while (!v[i].empty()) {
      int box = v[i].front();
      v[i].pop();
      if (num[box]) {
        num[box]--;
        M--;
        ans += i;
        if (!M) {
          cout << ans << endl;
          return 0;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02127 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  string s, t;

  cin >> s >> t;

  string x = t;
  int ans = 0;
  for (int i = 0; x.size() <= s.size(); i++) {
    // check
    // cerr << "x: " << x << endl;
    int idx = 0;
    for (char ch : s) {
      if (idx < x.size() and ch == x[idx]) {
        idx++;
      }
    }
    if (idx != x.size())
      break;

    ans++;

    string y = x;
    for (char ch : t) {
      y.push_back(ch);
      y += x;
    }
    x = y;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int main() {
  string s, t;

  cin >> s >> t;

  string x = t;
  int ans = 0;
  for (int i = 0; x.size() <= s.size(); i++) {
    // check
    // cerr << "x: " << x << endl;
    int idx = 0;
    for (char ch : s) {
      if (idx < x.size() and ch == x[idx]) {
        idx++;
      }
    }
    if (idx != x.size())
      break;

    ans++;

    long long size = x.size() * (t.size() + 1) + t.size();
    if (size > s.size())
      break;

    string y = x;
    for (char ch : t) {
      y.push_back(ch);
      y += x;
    }
    x = y;
  }

  cout << ans << endl;

  return 0;
}
Problem p02127 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000

typedef long long LL;
typedef long double LD;

bool ok(string &s, string &t) {
  int now = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == t[now]) {
      now++;
      if (now == t.length())
        return true;
    }
  }
  return false;
}

int main() {
  string s, t;
  cin >> s >> t;
  vector<string> tt(100, "");
  for (int i = 1; i < 100; i++) {
    tt[i] = tt[i - 1];
    for (int j = 0; j < t.length(); j++) {
      tt[i] += string(1, t[j]);
      tt[i] += tt[i - 1];
    }
    // cout << tt[i] << endl;
    if (!ok(s, tt[i])) {
      cout << i - 1 << endl;
      return 0;
    }
  }
  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repe(i, n) rep(i, (n) + 1)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define pere(i, n) rep(i, (n) + 1)
#define all(x) (x).begin(), (x).end()
#define SP << " " <<
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000

typedef long long LL;
typedef long double LD;

bool ok(string &s, string &t) {
  int now = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == t[now]) {
      now++;
      if (now == t.length())
        return true;
    }
  }
  return false;
}

int main() {
  string s, t;
  cin >> s >> t;
  vector<string> tt(100, "");
  for (int i = 1; i < 100; i++) {
    tt[i] = tt[i - 1];
    for (int j = 0; j < t.length(); j++) {
      tt[i] += string(1, t[j]);
      tt[i] += tt[i - 1];
      if (tt[i].length() > s.length()) {
        cout << i - 1 << endl;
        return 0;
      }
    }
    // cout << tt[i] << endl;
    if (!ok(s, tt[i])) {
      cout << i - 1 << endl;
      return 0;
    }
  }
  return 0;
}
Problem p02127 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

bool check(string T, string P) {
  if (P.length() > T.length())
    return false;

  int index = 0;
  for (int i = 0; i < T.length(); i++) {
    if (T[i] == P[index]) {
      index++;
      if (index == P.length())
        return true;
    }
  }
  return false;
}

int main() {

  string T, base_P, work_P, next_P;
  getline(cin, T);
  getline(cin, base_P);

  int ans = 0;

  work_P = base_P;
  while (check(T, work_P)) {
    ans++;

    next_P = base_P;

    for (int i = 0; i < work_P.length(); i++) {
      next_P += work_P[i] + base_P;
    }
    work_P = next_P;
  }

  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

bool check(string T, string P) {
  if (P.length() > T.length())
    return false;

  int index = 0;
  for (int i = 0; i < T.length(); i++) {
    if (T[i] == P[index]) {
      index++;
      if (index == P.length())
        return true;
    }
  }
  return false;
}

int main() {

  string T, base_P, work_P, next_P;
  getline(cin, T);
  getline(cin, base_P);

  int ans = 0;

  work_P = base_P;
  while (check(T, work_P)) {
    ans++;

    next_P = base_P;

    if ((work_P.length() + 1) * (base_P.length()) + work_P.length() >
        T.length())
      break;

    for (int i = 0; i < work_P.length(); i++) {
      next_P += work_P[i] + base_P;
    }
    work_P = next_P;
  }

  printf("%d\n", ans);

  return 0;
}
Problem p02127 (C++)

Buggy submission (status: Memory Limit Exceeded):

#if 1
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

auto &in = std::cin;
auto &out = std::cout;
#define all(C) std::begin(C), std::end(C)
const double PI =
    3.141592653589793238462643383279502884197169399375105820974944;

std::string s, t, tori;

int main() {
  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);
  in.tie(nullptr);
  out.tie(nullptr);

  in >> s >> t;
  tori = t;

  int32_t lev = 0;
  auto s_iter = s.begin();
  auto t_iter = t.begin();
  while (s_iter != s.end()) {
    if (*s_iter == *t_iter) {
      ++t_iter;
      if (t_iter == t.end()) {
        ++lev;
        std::string new_t = t;
        for (auto &ss : tori) {
          new_t.push_back(ss);
          new_t += t;
        }
        auto oldt_size = t.size();
        t = std::move(new_t);
        t_iter = t.begin() + oldt_size;
      }
    }
    ++s_iter;
  }
  out << lev << endl;

  return 0;
}
#endif


Fixed submission:

#if 1
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

auto &in = std::cin;
auto &out = std::cout;
#define all(C) std::begin(C), std::end(C)
const double PI =
    3.141592653589793238462643383279502884197169399375105820974944;

std::string s, t, tori;

int main() {
  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);
  in.tie(nullptr);
  out.tie(nullptr);

  in >> s >> t;
  tori = t;

  int32_t lev = 0;
  auto s_iter = s.begin();
  auto t_iter = t.begin();
  while (s_iter != s.end()) {
    if (*s_iter == *t_iter) {
      ++t_iter;
      if (t_iter == t.end()) {
        ++lev;
        std::string new_t = t;
        for (auto &ss : tori) {
          new_t.push_back(ss);
          new_t += t;
          if (new_t.size() > s.size()) {
            out << lev << endl;
            return 0;
          }
        }
        auto oldt_size = t.size();
        t = std::move(new_t);
        t_iter = t.begin() + oldt_size;
      }
    }
    ++s_iter;
  }
  out << lev << endl;

  return 0;
}
#endif
Problem p02127 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main() {
  string s, t;
  cin >> s >> t;
  int dep = 1;
  string targ = t;
  while (1) {
    int pos = 0;
    rep(i, s.length()) {
      if (pos >= (int)targ.length())
        break;
      if (s[i] == targ[pos]) {
        pos++;
      }
    }
    if (pos < (int)targ.length())
      break;
    dep++;
    string nxt = targ;
    rep(i, t.length()) nxt += t[i] + targ;
    targ = nxt;
  }
  cout << dep - 1 << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main() {
  string s, t;
  cin >> s >> t;
  int dep = 1;
  string targ = t;
  while (1) {
    int pos = 0;
    rep(i, s.length()) {
      if (pos >= (int)targ.length())
        break;
      if (s[i] == targ[pos]) {
        pos++;
      }
    }
    if (pos < (int)targ.length())
      break;
    dep++;
    lint nxtlen = (t.length() + 1) * (lint)targ.size() + t.length();
    if (s.length() < nxtlen)
      break;
    string nxt = targ;
    rep(i, t.length()) nxt += t[i] + targ;
    targ = nxt;
  }
  cout << dep - 1 << endl;
}
Problem p02127 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#define mod 1000000007
using ll = long long;
int dd[] = {0, 1, 0, -1, 0}; // →↓←↑

void solve() {
  string S, T;
  cin >> S >> T;

  string B = T;

  int cnt = 0;
  while (true) {
    // judge
    int s = 0, b = 0;
    while (b < B.size()) {
      if (s == S.size()) {
        cout << cnt << endl;
        return;
      }
      if (S[s] == B[b]) {
        b++;
      }
      s++;
    }
    cnt++;

    // next
    string tmp = B;
    for (int i = 0; i < T.size(); i++) {
      tmp += B[i] + B;
    }
    if (tmp.size() > S.size()) {
      cout << cnt << endl;
      return;
    }
    B = tmp;
  }
}

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


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define mod 1000000007
using ll = long long;
int dd[] = {0, 1, 0, -1, 0}; // →↓←↑

void solve() {
  string S, T;
  cin >> S >> T;

  string B = T;

  int cnt = 0;
  while (true) {
    // judge
    int s = 0, b = 0;
    while (b < B.size()) {
      if (s == S.size()) {
        cout << cnt << endl;
        return;
      }
      if (S[s] == B[b]) {
        b++;
      }
      s++;
    }
    cnt++;

    // next
    string tmp = B;
    for (int i = 0; i < T.size(); i++) {
      tmp += B[i] + B;
      if (tmp.size() > S.size()) {
        cout << cnt << endl;
        return;
      }
    }
    B = tmp;
  }
}

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