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

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

int main() {
  int n, s[100], cop[100], rank[100];

  cin >> n;
  for (int i = 0; i < n * (n - 1) / 2; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (c == d) {
      s[a - 1]++;
      s[b - 1]++;
    } else {
      (c > d ? s[a - 1] : s[b - 1]) += 3;
    }
  }

  for (int i = 0; i < n; i++)
    cop[i] = s[i];
  sort(cop, cop + n);
  reverse(cop, cop + n);
  for (int i = n - 1; i >= 0; i--) {
    rank[cop[i]] = i + 1;
  }

  for (int i = 0; i < n; i++) {
    cout << rank[s[i]] << endl;
  }
}

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

int main() {
  int n, s[101], cop[101], rank[10101];

  cin >> n;
  for (int i = 0; i < n * (n - 1) / 2; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (c == d) {
      s[a - 1]++;
      s[b - 1]++;
    } else {
      (c > d ? s[a - 1] : s[b - 1]) += 3;
    }
  }

  for (int i = 0; i < n; i++)
    cop[i] = s[i];
  sort(cop, cop + n);
  reverse(cop, cop + n);
  for (int i = n - 1; i >= 0; i--) {
    rank[cop[i]] = i + 1;
  }

  for (int i = 0; i < n; i++) {
    cout << rank[s[i]] << endl;
  }
}
Problem p00489 (C++)

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

int main() {
  int n;
  int score[100] = {0}, a, b, c, d;

  cin >> n;
  for (int i = 0; i < n * (n - 1) / 2; i++) {
    cin >> a >> b >> c >> d;
    if (c > d) {
      score[a - 1] += 3;
    } else if (c < d) {
      score[b - 1] += 3;
    } else {
      score[a - 1] += 1;
      score[b - 1] += 1;
    }
  }

  int cnt;
  for (int i = 0; i < n; i++) {
    cnt = 0;
    for (int j = 0; j < n * (n - 1) / 2; j++) {
      if (score[j] > score[i])
        cnt++;
    }
    cout << cnt + 1 << endl;
  }

  return 0;
}

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

int main() {
  int n;
  int score[10000] = {0}, a, b, c, d;

  cin >> n;
  for (int i = 0; i < n * (n - 1) / 2; i++) {
    cin >> a >> b >> c >> d;
    if (c > d) {
      score[a - 1] += 3;
    } else if (c < d) {
      score[b - 1] += 3;
    } else {
      score[a - 1] += 1;
      score[b - 1] += 1;
    }
  }

  int cnt;
  for (int i = 0; i < n; i++) {
    cnt = 0;
    for (int j = 0; j < n * (n - 1) / 2; j++) {
      if (score[j] > score[i])
        cnt++;
    }
    cout << cnt + 1 << endl;
  }

  return 0;
}
Problem p00489 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>

using namespace std;

int main() {
  int n;
  int a[105], b[105], c[105], d[105];
  int t[11000], k[105];
  cin >> n;
  for (int i = 0; i < (n * (n - 1)) / 2; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    t[i] = 0;
  }
  for (int i = 0; i < (n * (n - 1)) / 2; i++) {
    if (c[i] > d[i])
      t[a[i]] += 3;
    if (c[i] < d[i])
      t[b[i]] += 3;
    if (c[i] == d[i]) {
      t[a[i]]++;
      t[b[i]]++;
    }
  }
  int j = 1;
  int h = 0;
  for (int g = n * 3; g >= 0; g--) {
    for (int i = 1; i < n + 1; i++) {
      if (g == t[i]) {
        k[i] = j;
        h++;
      }
    }
    j += h;
    h = 0;
  }
  for (int i = 1; i < n + 1; i++) {
    cout << k[i] << endl;
  }
  return 0;
}

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

using namespace std;

int main() {
  int n;
  int a[11000], b[11000], c[11000], d[11000];
  int t[11000], k[11000];
  cin >> n;
  for (int i = 0; i < (n * (n - 1)) / 2; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    t[i] = 0;
  }
  for (int i = 0; i < (n * (n - 1)) / 2; i++) {
    if (c[i] > d[i])
      t[a[i]] += 3;
    if (c[i] < d[i])
      t[b[i]] += 3;
    if (c[i] == d[i]) {
      t[a[i]]++;
      t[b[i]]++;
    }
  }
  int j = 1;
  int h = 0;
  for (int g = n * 3; g >= 0; g--) {
    for (int i = 1; i < n + 1; i++) {
      if (g == t[i]) {
        k[i] = j;
        h++;
      }
    }
    j += h;
    h = 0;
  }
  for (int i = 1; i < n + 1; i++) {
    cout << k[i] << endl;
  }
  return 0;
}
Problem p00490 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int n;
  int a, b;
  int c;
  int s[100];
  int x, y;

  cin >> n;

  cin >> a >> b;
  cin >> c;

  for (int i = 1; i < n + 1; i++) {
    cin >> s[i];
  }
  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < n + 1; j++) {
      if (s[i] > s[j]) {
        x = s[i];
        s[i] = s[j];
        s[j] = x;
      }
    }
  }

  x = c;
  y = a;

  for (int i = 1; i < n + 1; i++) {
    if ((x / y) > ((x + s[i]) / (y + b)))
      break;

    else {
      x += s[i];
      y += b;
    }
  }
  cout << x / y << endl;

  return 0;
}

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

int main() {
  int n;
  int a, b;
  int c;
  int s[101];
  int x, y;

  cin >> n;

  cin >> a >> b;
  cin >> c;

  for (int i = 1; i < n + 1; i++) {
    cin >> s[i];
  }
  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < n + 1; j++) {
      if (s[i] > s[j]) {
        x = s[i];
        s[i] = s[j];
        s[j] = x;
      }
    }
  }

  x = c;
  y = a;

  for (int i = 1; i < n + 1; i++) {
    if ((x / y) > ((x + s[i]) / (y + b)))
      break;

    else {
      x += s[i];
      y += b;
    }
  }
  cout << x / y << endl;

  return 0;
}
Problem p00490 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

int main() {
  int n, a, b, base, top[100], cal[100];
  std::cin >> n >> a >> b >> base;
  for (int i = 0; i < n; i++)
    std::cin >> top[i];
  std::sort(top + 0, top + n);
  std::reverse(top + 0, top + n);
  for (int i = 0; i <= n; i++) {
    int all_cal = base;
    for (int j = 0; j < i; j++)
      all_cal += top[j];
    cal[i] = all_cal / (a + i * b);
  }
  std::sort(cal + 0, cal + n + 1);
  std::reverse(cal + 0, cal + n + 1);
  std::cout << cal[0] << std::endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>

int main() {
  int n, a, b, base, top[101], cal[101];
  std::cin >> n >> a >> b >> base;
  for (int i = 0; i < n; i++)
    std::cin >> top[i];
  std::sort(top + 0, top + n);
  std::reverse(top + 0, top + n);
  for (int i = 0; i <= n; i++) {
    int all_cal = base;
    for (int j = 0; j < i; j++)
      all_cal += top[j];
    cal[i] = all_cal / (a + i * b);
  }
  std::sort(cal + 0, cal + n + 1);
  std::reverse(cal + 0, cal + n + 1);
  std::cout << cal[0] << std::endl;
  return 0;
}
Problem p00491 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
const int MAX_N = 110;
int n, k;
int plan[MAX_N];
int memo[3][3][MAX_N];
int dfs(int a, int b, int depth) {
  if (memo[a][b][depth])
    return memo[a][b][depth];
  if (depth > n)
    return 1;
  int res = 0;
  if (plan[depth]) {
    if (not(a == b && b == plan[depth]))
      res += dfs(b, plan[depth], depth + 1);
  } else {
    for (int c = 1; c <= 3; c++) {
      if (a == b && b == c)
        continue;
      res += dfs(b, c, depth + 1);
    }
  }
  return memo[a][b][depth] = res % 10000;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> k;
  memset(memo, 0, sizeof(memo));
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    plan[a] = b;
  }
  cout << dfs(0, 0, 1) % 10000 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
const int MAX_N = 110;
int n, k;
int plan[MAX_N];
int memo[4][4][MAX_N];
int dfs(int a, int b, int depth) {
  if (memo[a][b][depth])
    return memo[a][b][depth];
  if (depth > n)
    return 1;
  int res = 0;
  if (plan[depth]) {
    if (not(a == b && b == plan[depth]))
      res += dfs(b, plan[depth], depth + 1);
  } else {
    for (int c = 1; c <= 3; c++) {
      if (a == b && b == c)
        continue;
      res += dfs(b, c, depth + 1);
    }
  }
  return memo[a][b][depth] = res % 10000;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> k;
  memset(memo, 0, sizeof(memo));
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    plan[a] = b;
  }
  cout << dfs(0, 0, 1) % 10000 << endl;
  return 0;
}
Problem p00491 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int N, K, num;
int day[101];
int a[101][4][3];

long long int dp(int d, int k, int n) {
  if (n == 3)
    return 0;

  if (d == N)
    return a[d][k][n] = 1;

  long long int b = 0;
  if (day[d + 1] != -1) {
    if (day[d + 1] == k) {
      b += dp(d + 1, day[d + 1], n + 1) % 10000;
    } else {
      b += dp(d + 1, day[d + 1], 1) % 10000;
    }
  } else {
    for (int i = 1; i <= 3; i++) {
      if (i == k) {
        b += dp(d + 1, i, n + 1) % 10000;
      } else {
        b += dp(d + 1, i, 1) % 10000;
      }
    }
  }
  return a[d][k][n] = b % 10000;
}

int main() {
  fill_n(**a, 101 * 4 * 3, -1);
  fill_n(day, 101, -1);
  cin >> N >> K;
  for (int i = 0; i < K; i++) {
    cin >> num;
    cin >> day[num];
  }
  cout << dp(0, 0, 0) << endl;
}

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

using namespace std;

int N, K, num;
int day[101];
int a[101][4][3];

long long int dp(int d, int k, int n) {
  if (n == 3)
    return 0;

  if (a[d][k][n] >= 0)
    return a[d][k][n];

  if (d == N)
    return a[d][k][n] = 1;

  long long int b = 0;
  if (day[d + 1] != -1) {
    if (day[d + 1] == k) {
      b += dp(d + 1, day[d + 1], n + 1) % 10000;
    } else {
      b += dp(d + 1, day[d + 1], 1) % 10000;
    }
  } else {
    for (int i = 1; i <= 3; i++) {
      if (i == k) {
        b += dp(d + 1, i, n + 1) % 10000;
      } else {
        b += dp(d + 1, i, 1) % 10000;
      }
    }
  }
  return a[d][k][n] = b % 10000;
}

int main() {
  fill_n(**a, 101 * 4 * 3, -1);
  fill_n(day, 101, -1);
  cin >> N >> K;
  for (int i = 0; i < K; i++) {
    cin >> num;
    cin >> day[num];
  }
  cout << dp(0, 0, 0) << endl;
}
Problem p00492 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define MP make_pair

typedef pair<int, int> PII;

int w, h, maps[102][102];
int dx[2][6] = {{0, 1, 0, -1, -1, -1}, {1, 1, 1, 0, -1, 0}};
int dy[6] = {-1, 0, 1, 1, 0, -1};

inline bool check(int x, int y) { return x >= 0 && x < w && y >= 0 && y < h; }

int main() {
  cin >> w >> h;
  REP(i, h) REP(j, w) scanf("%d", &maps[i + 1][j + 1]);
  w += 2;
  h += 2;

  queue<PII> q;
  q.push(MP(0, 0));

  while (!q.empty()) {
    PII now = q.front();
    q.pop();
    int x = now.first, y = now.second;
    maps[y][x] = 2;
    REP(i, 6) {
      int nx = x + dx[y % 2][i];
      int ny = y + dy[i];
      if (check(nx, ny) && maps[ny][nx] == 0)
        q.push(MP(nx, ny));
    }
  }

  int ans = 0;
  FOR(i, 1, h - 1) FOR(j, 1, w - 1) if (maps[i][j] == 1) {
    REP(k, 6) {
      int nx = j + dx[i % 2][k];
      int ny = i + dy[k];
      if (check(nx, ny) && maps[ny][nx] == 2)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define MP make_pair

typedef pair<int, int> PII;

int w, h, maps[102][102];
int dx[2][6] = {{0, 1, 0, -1, -1, -1}, {1, 1, 1, 0, -1, 0}};
int dy[6] = {-1, 0, 1, 1, 0, -1};

inline bool check(int x, int y) { return x >= 0 && x < w && y >= 0 && y < h; }

int main() {
  cin >> w >> h;
  REP(i, h) REP(j, w) scanf("%d", &maps[i + 1][j + 1]);
  w += 2;
  h += 2;

  queue<PII> q;
  q.push(MP(0, 0));

  while (!q.empty()) {
    PII now = q.front();
    q.pop();
    int x = now.first, y = now.second;
    if (maps[y][x] == 2)
      continue;
    maps[y][x] = 2;
    REP(i, 6) {
      int nx = x + dx[y % 2][i];
      int ny = y + dy[i];
      if (check(nx, ny) && maps[ny][nx] == 0)
        q.push(MP(nx, ny));
    }
  }

  int ans = 0;
  FOR(i, 1, h - 1) FOR(j, 1, w - 1) if (maps[i][j] == 1) {
    REP(k, 6) {
      int nx = j + dx[i % 2][k];
      int ny = i + dy[k];
      if (check(nx, ny) && maps[ny][nx] == 2)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00493 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int mod = 10000;
string A, B, S;
int M;

int memo[1000][10][3][2][500];

int calc(int keta = 0, int mae = 0, int hoko = 0, int jiyuu = 0,
         int amari = 0) {
  if (keta == S.size()) {
    return amari == 0;
  }
  if (~memo[keta][mae][hoko][jiyuu][amari])
    return memo[keta][mae][hoko][jiyuu][amari];

  int ret = 0;
  for (int i = 0; i < 10; i++) {
    if (jiyuu == 0 && S[keta] < i + '0')
      break;
    if ((hoko || mae) && mae == i)
      continue;
    int njiyuu = jiyuu | (i + '0' != S[keta]);
    int namari = (amari * 10 + i) % M;
    if (hoko == 0) {
      if (mae)
        ret = (ret + calc(keta + 1, i, mae < i ? 2 : 1, njiyuu, namari)) % mod;
      else
        ret = (ret + calc(keta + 1, i, 0, njiyuu, namari)) % mod;
    } else if (hoko == 1 && mae < i) {
      ret = (ret + calc(keta + 1, i, 2, njiyuu, namari)) % mod;
    } else if (hoko == 2 && mae > i) {
      ret = (ret + calc(keta + 1, i, 1, njiyuu, namari)) % mod;
    }
  }
  return memo[keta][mae][hoko][jiyuu][amari] = ret;
}

int main() {
  cin >> A >> B >> M;
  A[A.size() - 1]--;
  for (int i = A.size() - 1; i > 0; i--) {
    if (A[i] >= '0')
      break;
    A[i] = '9';
    A[i - 1]--;
  }

  S = B;
  memset(memo, -1, sizeof(memo));
  int vb = calc();

  S = A;
  memset(memo, -1, sizeof(memo));
  int va = calc();

  cout << (vb - va + mod) % mod << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int mod = 10000;
string A, B, S;
int M;

int memo[501][10][3][2][500];

int calc(int keta = 0, int mae = 0, int hoko = 0, int jiyuu = 0,
         int amari = 0) {
  if (keta == S.size()) {
    return amari == 0;
  }
  if (~memo[keta][mae][hoko][jiyuu][amari])
    return memo[keta][mae][hoko][jiyuu][amari];

  int ret = 0;
  for (int i = 0; i < 10; i++) {
    if (jiyuu == 0 && S[keta] < i + '0')
      break;
    if ((hoko || mae) && mae == i)
      continue;
    int njiyuu = jiyuu | (i + '0' != S[keta]);
    int namari = (amari * 10 + i) % M;
    if (hoko == 0) {
      if (mae)
        ret = (ret + calc(keta + 1, i, mae < i ? 2 : 1, njiyuu, namari)) % mod;
      else
        ret = (ret + calc(keta + 1, i, 0, njiyuu, namari)) % mod;
    } else if (hoko == 1 && mae < i) {
      ret = (ret + calc(keta + 1, i, 2, njiyuu, namari)) % mod;
    } else if (hoko == 2 && mae > i) {
      ret = (ret + calc(keta + 1, i, 1, njiyuu, namari)) % mod;
    }
  }
  return memo[keta][mae][hoko][jiyuu][amari] = ret;
}

int main() {
  cin >> A >> B >> M;
  A[A.size() - 1]--;
  for (int i = A.size() - 1; i > 0; i--) {
    if (A[i] >= '0')
      break;
    A[i] = '9';
    A[i - 1]--;
  }

  S = B;
  memset(memo, -1, sizeof(memo));
  int vb = calc();

  S = A;
  memset(memo, -1, sizeof(memo));
  int va = calc();

  cout << (vb - va + mod) % mod << endl;

  return 0;
}
Problem p00494 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)

ll ans = 0;
ll n;
string s;
ll cnt = 0;
vector<P> p;
ll sz;
int main() {
  cin >> s;
  p.reserve(1000001);
  n = s.length();
  rep(i, n) {
    if (i == 0) {
      if (s[0] == 'J')
        p.pb(P(1, 0));
      else if (s[0] == 'O')
        p.pb(P(1, 1));
      else
        p.pb(P(1, 2));
    } else {
      if (s[i] != s[i - 1]) {
        if (s[i] == 'J')
          p.pb(P(1, 0));
        else if (s[i] == 'O')
          p.pb(P(1, 1));
        else
          p.pb(P(1, 2));
      } else {
        p[p.size() - 1].fr++;
      }
    }
  }
  rep(i, p.size() - 2) {
    if (p[i].sc == 0 && p[i + 1].sc == 1 && p[i + 2].sc == 2 &&
        p[i].fr >= p[i + 1].fr && p[i + 1].fr <= p[i + 2].fr) {
      ans = max(ans, p[i + 1].fr);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep1(i, x) for (ll i = 1; i <= x; i++)
#define rrep(i, x) for (ll i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (ll i = x; i > 0; i--)

ll ans = 0;
ll n;
string s;
ll cnt = 0;
vector<P> p;
ll sz;
int main() {
  cin >> s;
  p.reserve(1000001);
  n = s.length();
  rep(i, n) {
    if (i == 0) {
      if (s[0] == 'J')
        p.pb(P(1, 0));
      else if (s[0] == 'O')
        p.pb(P(1, 1));
      else
        p.pb(P(1, 2));
    } else {
      if (s[i] != s[i - 1]) {
        if (s[i] == 'J')
          p.pb(P(1, 0));
        else if (s[i] == 'O')
          p.pb(P(1, 1));
        else
          p.pb(P(1, 2));
      } else {
        p[p.size() - 1].fr++;
      }
    }
  }
  for (ll i = 0; i + 2 < p.size(); i++) {
    if (p[i].sc == 0 && p[i + 1].sc == 1 && p[i + 2].sc == 2 &&
        p[i].fr >= p[i + 1].fr && p[i + 1].fr <= p[i + 2].fr) {
      ans = max(ans, p[i + 1].fr);
    }
  }
  cout << ans << endl;
}
Problem p00494 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

char s[1000001];

int main() {

  fgets(s, sizeof(s), stdin);
  int res = 0, oc, ic, jc;
  int i, j, k, l;
  int slen = strlen(s);
  jc = oc = ic = 0;
  rep(i, slen) {
    if (ic == 0 && oc == 0 && s[i] == 'J')
      jc++;
    else if (jc && ic == 0 && s[i] == 'O')
      oc++;
    else if (jc && oc && s[i] == 'I')
      ic++;
    else {
      if (jc >= oc && ic >= oc)
        res = max(res, oc);
      jc = oc = ic = 0;
      i--;
    }
  }

  if (jc >= oc && ic >= oc)
    res = max(res, oc);
  printf("%d\n", res);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

char s[1000001];

int main() {

  fgets(s, sizeof(s), stdin);
  int res = 0, oc, ic, jc;
  int i, j, k, l;
  int slen = strlen(s);
  jc = oc = ic = 0;
  rep(i, slen) {
    if (ic == 0 && oc == 0 && s[i] == 'J')
      jc++;
    else if (jc && ic == 0 && s[i] == 'O')
      oc++;
    else if (jc && oc && s[i] == 'I')
      ic++;
    else {
      if (jc >= oc && ic >= oc)
        res = max(res, oc);
      jc = oc = ic = 0;
      if (s[i] == 'J')
        jc++;
    }
  }

  if (jc >= oc && ic >= oc)
    res = max(res, oc);
  printf("%d\n", res);
  return 0;
}
Problem p00494 (C++)

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

typedef pair<int, int> pii;

int main() {
  char s[1000001];
  vector<pii> v;
  cin >> s;
  int l = strlen(s);
  int cnt = 0;
  int x;
  for (int i = 0; i < l; i++) {
    if (i > 0 && s[i] != s[i - 1]) {
      v.push_back(pii(x, cnt));
      cnt = 0;
    }
    switch (s[i]) {
    case 'J':
      x = 0;
      break;
    case 'O':
      x = 1;
      break;
    case 'I':
      x = 2;
      break;
    }
    cnt++;
  }
  v.push_back(pii(x, cnt));
  int ans = 0;
  for (int i = 0; i < v.size() - 2; i++) {
    if (v[i].first == 0 && v[i + 1].first == 1 && v[i + 2].first == 2 &&
        v[i].second >= v[i + 1].second && v[i + 1].second <= v[i + 2].second) {
      ans = max(ans, v[i + 1].second);
    }
  }
  cout << ans << endl;
  return 0;
}

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

typedef pair<int, int> pii;

int main() {
  char s[1000001];
  vector<pii> v;
  cin >> s;
  int l = strlen(s);
  int cnt = 0;
  int x;
  for (int i = 0; i < l; i++) {
    if (i > 0 && s[i] != s[i - 1]) {
      v.push_back(pii(x, cnt));
      cnt = 0;
    }
    switch (s[i]) {
    case 'J':
      x = 0;
      break;
    case 'O':
      x = 1;
      break;
    case 'I':
      x = 2;
      break;
    }
    cnt++;
  }
  v.push_back(pii(x, cnt));
  int ans = 0;
  for (int i = 0; i < (int)v.size() - 2; i++) {
    if (i < (int)v.size() - 2 && v[i].first == 0 && v[i + 1].first == 1 &&
        v[i + 2].first == 2 && v[i].second >= v[i + 1].second &&
        v[i + 1].second <= v[i + 2].second) {
      ans = max(ans, v[i + 1].second);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00494 (C++)

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

using namespace std;

string S;

int main() {
  cin >> S;
  int res = 0;
  for (int i = 0; i < S.size(); i++) {
    if (S[i] != 'J')
      continue;
    int jc, oc, ic;
    jc = oc = ic = 0;
    int j;
    for (j = i; j < S.size(); j++) {
      if (S[j] != 'J')
        break;
      jc++;
    }
    //    if(S[j]!='O') continue;
    for (; j < S.size(); j++) {
      if (S[j] != 'O')
        break;
      oc++;
    }
    for (; j < S.size(); j++) {
      if (S[j] != 'I')
        break;
      ic++;
    }
    //    res = max(res, min(jc, min(oc,ic)));
    if (oc <= ic && oc <= jc)
      res = max(res, oc);
  }

  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

string S;

int main() {
  cin >> S;
  int res = 0;
  for (int i = 0; i < S.size(); i++) {
    if (S[i] != 'J')
      continue;
    int jc, oc, ic;
    jc = oc = ic = 0;
    int j;
    for (j = i; j < S.size(); j++) {
      if (S[j] != 'J')
        break;
      jc++;
    }
    //    if(S[j]!='O') continue;
    for (; j < S.size(); j++) {
      if (S[j] != 'O')
        break;
      oc++;
    }
    for (; j < S.size(); j++) {
      if (S[j] != 'I')
        break;
      ic++;
    }
    //    res = max(res, min(jc, min(oc,ic)));
    if (oc <= ic && oc <= jc)
      res = max(res, oc);
    i = j - 1;
  }

  cout << res << endl;
}
Problem p00494 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#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 long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1001001001;

int main() {
  char str[100010];
  scanf("%s", str);
  int n = strlen(str);

  vector<pair<char, int>> data;
  rep(i, n) {
    if (!data.empty() && data[data.size() - 1].first == str[i])
      data[data.size() - 1].second++;
    else
      data.push_back(make_pair(str[i], 1));
  }

  int ans = 0;
  rep(i, data.size() - 2) {
    if (data[i].first == 'J' && data[i + 1].first == 'O' &&
        data[i + 2].first == 'I') {
      int a = data[i].second;
      int b = data[i + 1].second;
      int c = data[i + 2].second;
      if (b <= a && b <= c)
        ans = max(ans, b);
    }
  }
  printf("%d\n", ans);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#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 long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1001001001;

int main() {
  char str[1000100];
  scanf("%s", str);
  int n = strlen(str);

  vector<pair<char, int>> data;
  rep(i, n) {
    if (!data.empty() && data[data.size() - 1].first == str[i])
      data[data.size() - 1].second++;
    else
      data.push_back(make_pair(str[i], 1));
  }

  int ans = 0;
  rep(i, data.size() - 2) {
    if (data[i].first == 'J' && data[i + 1].first == 'O' &&
        data[i + 2].first == 'I') {
      int a = data[i].second;
      int b = data[i + 1].second;
      int c = data[i + 2].second;
      if (b <= a && b <= c)
        ans = max(ans, b);
    }
  }
  printf("%d\n", ans);

  return 0;
}
Problem p00494 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
  string s;
  cin >> s;
  vector<char> ch;
  vector<int> co;
  int c = 0;
  rep(i, s.size()) {
    c++;
    if (i == s.size() - 1 || s[i] != s[i + 1]) {
      co.pb(c);
      ch.pb(s[i]);
      c = 0;
    }
  }
  int out = 0;
  rep(i, ch.size() - 2) {
    if (ch[i] == 'J' && ch[i + 1] == 'O' && ch[i + 2] == 'I')
      out = max(out, min(co[i], min(co[i + 1], co[i + 2])));
  }
  cout << out << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
  string s;
  cin >> s;
  vector<char> ch;
  vector<int> co;
  int c = 0;
  rep(i, s.size()) {
    c++;
    if (i == s.size() - 1 || s[i] != s[i + 1]) {
      co.pb(c);
      ch.pb(s[i]);
      c = 0;
    }
  }
  int out = 0;
  if (ch.size() > 2) {
    rep(i, ch.size() - 2) {
      if (ch[i] == 'J' && ch[i + 1] == 'O' && ch[i + 2] == 'I')
        if (co[i + 1] <= co[i] && co[i + 1] <= co[i + 2])
          out = max(out, co[i + 1]);
    }
  }
  cout << out << endl;
}
Problem p00494 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
typedef pair<char, int> P;

string s;

int main() {
  cin >> s;
  vector<P> v;
  char c = s[0];
  int length = 1;
  for (int i = 1; i < s.size(); i++) {
    if (s[i] == c)
      length++;
    else {
      v.push_back(P(c, length));
      c = s[i], length = 1;
    }
  }

  v.push_back(P(c, length));

  int ans = 0;

  for (int i = 0; i < v.size() - 2; i++) {
    if (v[i].first == 'J' && v[i + 1].first == 'O' && v[i + 2].first == 'I') {
      if (v[i].second >= v[i + 1].second &&
          v[i + 1].second <= v[i + 2].second) {
        ans = max(ans, v[i + 1].second);
      }
    }
  }

  cout << ans << endl;

  return 0;
}

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

using namespace std;
typedef pair<char, int> P;

string s;

int main() {
  cin >> s;
  vector<P> v;
  char c = s[0];
  int length = 1;
  for (int i = 1; i < s.size(); i++) {
    if (s[i] == c)
      length++;
    else {
      v.push_back(P(c, length));
      c = s[i], length = 1;
    }
  }

  v.push_back(P(c, length));

  int ans = 0;

  if (v.size() < 3) {
    cout << 0 << endl;
    return 0;
  }

  for (int i = 0; i < v.size() - 2; i++) {
    if (v[i].first == 'J' && v[i + 1].first == 'O' && v[i + 2].first == 'I') {
      if (v[i].second >= v[i + 1].second &&
          v[i + 1].second <= v[i + 2].second) {
        ans = max(ans, v[i + 1].second);
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p00494 (C++)

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

#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second

string s;
vector<pair<int, int>> a;
int ans = 0;

int main() {
  cin >> s;
  For(i, 0, s.size()) {
    int cnt;
    if (s[i] == 'J') {
      cnt = 0;
      while (s[i] == 'J') {
        cnt++;
        i++;
      }
      a.pb(mp(1, cnt));
    }
    if (s[i] == 'O') {
      cnt = 0;
      while (s[i] == 'O') {
        cnt++;
        i++;
      }
      a.pb(mp(2, cnt));
    }
    if (s[i] == 'I') {
      cnt = 0;
      while (s[i] == 'I') {
        cnt++;
        i++;
      }
      a.pb(mp(3, cnt));
    }
    i--;
  }
  For(i, 0,
      a.size() - 2) if (a[i].fi == 1 && a[i + 1].fi == 2 && a[i + 2].fi == 3) {
    ans = max(ans, min(a[i].se, min(a[i + 1].se, a[i + 2].se)));
    i += 2;
  }
  cout << ans << '\n';
}

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

#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second

string s;
vector<pair<int, int>> a;
int ans = 0;

int main() {
  cin >> s;
  For(i, 0, s.size()) {
    int cnt;
    if (s[i] == 'J') {
      cnt = 0;
      while (s[i] == 'J') {
        cnt++;
        i++;
      }
      a.pb(mp(1, cnt));
    }
    if (s[i] == 'O') {
      cnt = 0;
      while (s[i] == 'O') {
        cnt++;
        i++;
      }
      a.pb(mp(2, cnt));
    }
    if (s[i] == 'I') {
      cnt = 0;
      while (s[i] == 'I') {
        cnt++;
        i++;
      }
      a.pb(mp(3, cnt));
    }
    i--;
  }
  if (a.size() <= 2) {
    cout << "0\n";
    return 0;
  }
  For(i, 0, a.size() - 2) if (a[i].fi == 1 && a[i + 1].fi == 2 &&
                              a[i + 2].fi == 3 &&
                              min(a[i].se, min(a[i + 1].se, a[i + 2].se)) ==
                                  a[i + 1].se) {
    ans = max(ans, min(a[i].se, min(a[i + 1].se, a[i + 2].se)));
    i += 2;
  }
  cout << ans << '\n';
}
Problem p00494 (C++)

Buggy submission (status: Time Limit Exceeded):
//============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
string S;
int main() {
  cin >> S;

  int D = 0, E = 0, F = 0;
  int ans = 0;
  int A, B, C;
  for (int i = 0; i < S.size(); i++) {
    for (A = i; S[A] == 'J'; A++)
      ;
    for (B = A; S[B] == 'O'; B++)
      ;
    for (C = B; S[C] == 'I'; C++)
      ;
    D = A - i;
    E = B - A;
    F = C - B;
    if (D >= E && E <= F)
      ans = max(ans, E);
  }

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


Fixed submission:
//============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
string S;
int main() {
  cin >> S;

  int D = 0, E = 0, F = 0;
  int ans = 0;
  int A, B, C;
  for (int i = 0; i < S.size(); i = C) {
    for (A = i; S[A] == 'J'; A++)
      ;
    for (B = A; S[B] == 'O'; B++)
      ;
    for (C = B; S[C] == 'I'; C++)
      ;
    D = A - i;
    E = B - A;
    F = C - B;
    if (D >= E && E <= F)
      ans = max(ans, E);
  }

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

Buggy submission (status: Time Limit Exceeded):
//============================================================================
// Name        : AOJ.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>
using namespace std;
string s;
int main() {
  cin >> s;
  int R = 0;
  int a0, a1, a2, a3;

  for (a0 = 0; a0 < s.size(); a0 = a3) {
    for (a1 = a0; s[a1] == 'J'; a1++)
      ;
    for (a2 = a1; s[a2] == 'O'; a2++)
      ;
    for (a3 = a2; s[a3] == 'C'; a3++)
      ;
    if (a1 - a0 >= a2 - a1 && a2 - a1 <= a3 - a2)
      R = max(R, a2 - a1);
  }
  cout << R << endl;
  return 0;
}

Fixed submission:
//============================================================================
// Name        : AOJ.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>
using namespace std;
string s;
int main() {
  cin >> s;
  int R = 0;
  int a0, a1, a2, a3;

  for (a0 = 0; a0 < s.size(); a0 = a3) {
    for (a1 = a0; s[a1] == 'J'; a1++)
      ;
    for (a2 = a1; s[a2] == 'O'; a2++)
      ;
    for (a3 = a2; s[a3] == 'I'; a3++)
      ;
    if (a1 - a0 >= a2 - a1 && a2 - a1 <= a3 - a2)
      R = max(R, a2 - a1);
  }
  cout << R << endl;
  return 0;
}
Problem p00494 (C++)

Buggy submission (status: Time Limit Exceeded):
//============================================================================
// Name        : AOJ.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>
using namespace std;
int main() {
  string s;
  cin >> s;
  int ans = 0;
  int a0, a1, a2, a3;
  for (a0 = 0; a0 < s.size(); a0 = a3) {
    for (a1 = a0; s[a1] == 'J'; a1++)
      ;
    for (a2 = a1; s[a2] == 'O'; a2++)
      ;
    for (a3 = a2; s[a3] == 'J'; a3++)
      ;
    if (a1 - a0 >= a2 - a1 && a2 - a1 <= a3 - a2)
      ans = max(ans, a2 - a1);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
//============================================================================
// Name        : AOJ.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>
using namespace std;
int main() {
  string s;
  cin >> s;
  int ans = 0;
  int a0, a1, a2, a3;
  for (a0 = 0; a0 < s.size(); a0 = a3) {
    for (a1 = a0; s[a1] == 'J'; a1++)
      ;
    for (a2 = a1; s[a2] == 'O'; a2++)
      ;
    for (a3 = a2; s[a3] == 'I'; a3++)
      ;
    if (a1 - a0 >= a2 - a1 && a2 - a1 <= a3 - a2)
      ans = max(ans, a2 - a1);
  }
  cout << ans << endl;
  return 0;
}
Problem p00494 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

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

using namespace std;

typedef pair<char, int> P;

string s;
vector<P> x;

int main() {
  cin >> s;
  rep(i, s.size()) {
    if (x.empty() || x.back().first != s[i]) {
      x.push_back(P(s[i], 1));
    } else {
      ++x.back().second;
    }
  }
  int ans = 0;
  rep(i, x.size() - 2) {
    if (x[i].first == 'J' && x[i + 1].first == 'O' && x[i + 2].first == 'I') {
      ans = max(min(x[i].second, min(x[i + 1].second, x[i + 2].second)), ans);
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

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

using namespace std;

typedef pair<char, int> P;

string s;
vector<P> x;

int main() {
  cin >> s;
  rep(i, s.size()) {
    if (x.empty() || x.back().first != s[i]) {
      x.push_back(P(s[i], 1));
    } else {
      ++x.back().second;
    }
  }
  int ans = 0;
  rep(i, (int)x.size() - 2) {
    P &a = x[i];
    P &b = x[i + 1];
    P &c = x[i + 2];
    if (a.first == 'J' && b.first == 'O' && c.first == 'I') {
      if (a.second >= b.second && c.second >= b.second) {
        ans = max(b.second, ans);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00494 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

string s;
int index = 0, ci = 0;
int ans = 0;
int c[200001];

int main() {

  cin >> s;
  while (index < s.length()) {
    for (int i = 0; i < 3; i++) {
      char ct;
      if (i == 0)
        ct = 'J';
      else if (i == 1)
        ct = 'O';
      else if (i == 2)
        ct = 'I';
      int cnt;
      for (cnt = 0; ct == s[index + cnt]; cnt++)
        ;
      index += cnt;
      c[ci] = cnt;
      ci++;
    }
  }
  // Oを探索
  for (int i = 1; i < ci; i += 3) {
    int oi = c[i];
    int ji = c[i - 1];
    int ii = c[i + 1];
    if (oi > ji || oi > ii)
      continue;
    else if (ans < oi)
      ans = oi;
  }

  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

string s;
int index = 0, ci = 0;
int ans = 0;
int c[2000000];

int main() {

  cin >> s;
  while (index < s.length()) {
    for (int i = 0; i < 3; i++) {
      char ct;
      if (i == 0)
        ct = 'J';
      else if (i == 1)
        ct = 'O';
      else if (i == 2)
        ct = 'I';
      int cnt;
      for (cnt = 0; ct == s[index + cnt]; cnt++)
        ;
      index += cnt;
      c[ci] = cnt;
      ci++;
    }
  }
  // Oを探索
  for (int i = 1; i < ci; i += 3) {
    int oi = c[i];
    int ji = c[i - 1];
    int ii = c[i + 1];
    if (oi > ji || oi > ii)
      continue;
    else if (ans < oi)
      ans = oi;
  }

  printf("%d\n", ans);
  return 0;
}
Problem p00494 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
char str[1000005];
int main() {
  scanf("%s", &str);
  int leng = strlen(str);
  int ans = 0, i = 0;
  for (;;) {
    if (str[i] == 'J') {
      int a = 0, b = 0, c = 0;
      while (str[i] == 'J' && i < leng) {
        i++;
        a++;
      }
      while (str[i] == 'O' && i < leng) {
        i++;
        b++;
      }

      while (str[i] == 'I' && i < leng) {
        i++;
        c++;
      }
      if (b <= a && b <= c) {
        ans = max(ans, b);
      }
      if (i == leng)
        break;
    } else {
      i++;
    }
  }

  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
char str[1000005];
int main() {
  scanf("%s", &str);
  int leng = strlen(str);
  int ans = 0, i = 0;
  for (;;) {
    if (str[i] == 'J') {
      int a = 0, b = 0, c = 0;
      while (str[i] == 'J' && i < leng) {
        i++;
        a++;
      }
      while (str[i] == 'O' && i < leng) {
        i++;
        b++;
      }

      while (str[i] == 'I' && i < leng) {
        i++;
        c++;
      }
      if (b <= a && b <= c) {
        ans = max(ans, b);
      }
      if (i == leng)
        break;
    } else {
      i++;
      if (i == leng)
        break;
    }
  }

  printf("%d\n", ans);
  return 0;
}
Problem p00494 (C++)

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

char a[1000000];
int b;
int g = 0;
struct JOI {
  int J, O, I;
};
JOI c(int d) {
  JOI e;
  e.J = e.O = e.I = 0;
  int f;
  for (f = d; f < b; f++) {
    if (a[f] != 'J')
      break;
  }
  e.J = f - d;
  d = f;
  for (f = d; f < b; f++) {
    if (a[f] != 'O')
      break;
  }
  e.O = f - d;
  d = f;
  for (f = d; f < b; f++) {
    if (a[f] != 'I')
      break;
  }
  e.I = f - d;
  return e;
}
int main() {
  scanf("%s", a);
  b = strlen(a);
  int MAX = 0;
  for (; g < b; g++) {
    if (a[g] == 'J') {
      JOI s = c(g);
      if (s.O <= s.I && s.O <= s.J)
        MAX = max(MAX, s.O);
    }
  }
  cout << MAX << endl;
}

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

char a[1000000];
int b;
int g = 0;
struct JOI {
  int J, O, I;
};
JOI c(int d) {
  JOI e;
  e.J = e.O = e.I = 0;
  int f;
  for (f = d; f < b; f++) {
    if (a[f] != 'J')
      break;
  }
  e.J = f - d;
  d = f;
  for (f = d; f < b; f++) {
    if (a[f] != 'O')
      break;
  }
  e.O = f - d;
  d = f;
  for (f = d; f < b; f++) {
    if (a[f] != 'I')
      break;
  }
  e.I = f - d;
  g = f - 1;
  return e;
}
int main() {
  scanf("%s", a);
  b = strlen(a);
  int MAX = 0;
  for (; g < b; g++) {
    if (a[g] == 'J') {
      JOI s = c(g);
      if (s.O <= s.I && s.O <= s.J)
        MAX = max(MAX, s.O);
    }
  }
  cout << MAX << endl;
}
Problem p00494 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;

char s[1234567];
vector<pair<char, int>> ss;

int main() {
  scanf("%s", s);
  int lastsame = 1;
  char thlett = s[0];
  for (int i = 1; i < strlen(s); ++i) {
    if (thlett != s[i]) {
      ss.push_back(make_pair(thlett, lastsame));
      thlett = s[i];
      lastsame = 1;
    } else {
      ++lastsame;
    }
  }
  ss.push_back(make_pair(thlett, lastsame));
  if (ss.size() < 3)
    return 0 * printf("0\n");
  int ans = 0;
  for (int j = 0; j < ss.size() - 2; ++j) {
    if (ss[j].first == 'J' && ss[j + 1].first == 'O' &&
        ss[j + 2].first == 'I') {
      if (ss[j + 1].second <= min(ss[j].second, ss[j + 2].second)) {
        ans = max(ans, ss[j + 1].second);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;

char s[1234567];
vector<pair<char, int>> ss;

int main() {
  scanf("%s", s);
  int lastsame = 1;
  char thlett = s[0];
  int n = strlen(s);
  for (int i = 1; i < n; ++i) {
    if (thlett != s[i]) {
      ss.push_back(make_pair(thlett, lastsame));
      thlett = s[i];
      lastsame = 1;
    } else {
      ++lastsame;
    }
  }
  ss.push_back(make_pair(thlett, lastsame));
  if (ss.size() < 3)
    return 0 * printf("0\n");
  int ans = 0;
  for (int j = 0; j < ss.size() - 2; ++j) {
    if (ss[j].first == 'J' && ss[j + 1].first == 'O' &&
        ss[j + 2].first == 'I') {
      if (ss[j + 1].second <= min(ss[j].second, ss[j + 2].second)) {
        ans = max(ans, ss[j + 1].second);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p00494 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define loop(i, a, b) for (int i = a; i < int(b); i++)
#define rep(i, b) loop(i, 0, b)

char s[100010];
int l[100010];
int main() {
  scanf("%s", s);
  int n = strlen(s);
  l[0] = 1;
  rep(i, n - 1) {
    if (s[i + 1] == s[i])
      l[i + 1] = l[i] + 1;
    else
      l[i + 1] = 1;
  }
  int ans = 0;
  loop(i, 1, n - 1) {
    if (s[i] == 'O' && s[i + 1] != 'O' && i + l[i] < n && i - l[i] >= 0 &&
        l[i + l[i]] >= l[i] && l[i - l[i]] >= l[i] && s[i + l[i]] == 'I' &&
        s[i - l[i]] == 'J') {
      ans = max(ans, l[i]);
    }
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define loop(i, a, b) for (int i = a; i < int(b); i++)
#define rep(i, b) loop(i, 0, b)

char s[1000100];
int l[1000100];
int main() {
  scanf("%s", s);
  int n = strlen(s);
  l[0] = 1;
  rep(i, n - 1) {
    if (s[i + 1] == s[i])
      l[i + 1] = l[i] + 1;
    else
      l[i + 1] = 1;
  }
  int ans = 0;
  loop(i, 1, n - 1) {
    if (s[i] == 'O' && s[i + 1] != 'O' && i + l[i] < n && i - l[i] >= 0 &&
        l[i + l[i]] >= l[i] && l[i - l[i]] >= l[i] && s[i + l[i]] == 'I' &&
        s[i - l[i]] == 'J') {
      ans = max(ans, l[i]);
    }
  }
  printf("%d\n", ans);
}
Problem p00494 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int main() {

  string s;
  int kmax = 0;

  cin >> s;

  for (int i = 0; i < s.size(); i++) {
    bool j1, j2;
    int cnt = 0;
    if (s[i] == 'O') {
      j1 = false, j2 = false;
      cnt = 1;
      for (int j = i + 1; s[j] == 'O' && j < s.size(); j++)
        cnt++;
      for (int j = i - 1, c = 1; s[j] == 'J' && j >= 0; j--, c++) {
        if (c == cnt) {
          j1 = true;
          break;
        }
      }
      for (int j = i + cnt, c = 1; s[j] == 'I' && j < s.size(); j++, c++) {
        if (c == cnt) {
          j2 = true;
          break;
        }
      }
    }
    if (j1 && j2)
      kmax = max(kmax, cnt);
  }

  cout << kmax << endl;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int main() {

  string s;
  int kmax = 0;

  cin >> s;

  for (int i = 0; i < s.size(); i++) {
    bool j1, j2;
    int cnt = 0;
    if (s[i] == 'O') {
      j1 = false, j2 = false;
      cnt = 1;
      for (int j = i + 1; s[j] == 'O' && j < s.size(); j++)
        cnt++;
      for (int j = i - 1, c = 1; s[j] == 'J' && j >= 0; j--, c++) {
        if (c == cnt) {
          j1 = true;
          break;
        }
      }
      for (int j = i + cnt, c = 1; s[j] == 'I' && j < s.size(); j++, c++) {
        if (c == cnt) {
          j2 = true;
          break;
        }
      }
    }
    if (j1 && j2)
      kmax = max(kmax, cnt);
    i += cnt;
  }

  cout << kmax << endl;
}