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

#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;
}

Error: MLE

Fixed submission:

#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;
}
Problem p00493 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define LL lon long

using namespace std;

string s;
string str1, str2;
static const int MOD = 10000;
int M;
int mem[501][10][3][3][500];

int cal(int n, int p, int pp, int f, int mod) {
  int res = 0;
  if (n == s.size()) {
    if (mod == 0)
      return 1;
    else
      return 0;
  }
  if (mem[n][p][pp][f][mod] != -1)
    return mem[n][p][pp][f][mod];
  int t = 9;
  if (f != 1)
    t = s[n] - '0';
  for (int i = 0; i <= t; ++i) {
    if (pp == 1 && p >= i)
      continue;
    else if (pp == 0 && p <= i)
      continue;
    else if (pp == 2 && p != 0 && p == i)
      continue;

    int b;
    if (pp == 2) {
      if (p == 0)
        b = 2;
      else if (p < i)
        b = 0;
      else
        b = 1;
    } else if (pp == 1)
      b = 0;
    else
      b = 1;

    int k;
    if (f == 1)
      k = 1;
    else if (i != s[n] - '0')
      k = 1;
    else
      k = 0;

    int temp = mod * 10 + i;
    res += cal(n + 1, i, b, k, temp % M);
  }
  return mem[n][p][pp][f][mod] = res % MOD;
}

int main() {
  int vr = 0;
  int vl = 0;
  cin >> str1 >> str2 >> M;
  for (int i = str1.size() - 1; i >= 0; i--) {
    if (str1[i] == '0') {
      str1[i] = '9';
    } else {
      str1[i]--;
      break;
    }
  }
  s = str1;
  memset(mem, -1, sizeof(mem));
  vl = cal(0, 0, 2, 0, 0);
  s = str2;
  memset(mem, -1, sizeof(mem));
  vr = cal(0, 0, 2, 0, 0);
  cout << (vr - vl + MOD) % MOD << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define LL lon long

using namespace std;

string s;
string str1, str2;
static const int MOD = 10000;
int M;
int mem[500][10][3][2][500];

int solve(int n, int p, int pp, int f, int mod) {
  int res = 0;
  if (n == s.size()) {
    if (mod == 0)
      return 1;
    else
      return 0;
  }
  if (mem[n][p][pp][f][mod] != -1)
    return mem[n][p][pp][f][mod];
  int t = 9;
  if (f != 1)
    t = s[n] - '0';
  for (int i = 0; i <= t; ++i) {
    if (pp == 1 && p >= i)
      continue;
    else if (pp == 0 && p <= i)
      continue;
    else if (pp == 2 && p != 0 && p == i)
      continue;

    int b;
    if (pp == 2) {
      if (p == 0)
        b = 2;
      else if (p < i)
        b = 0;
      else
        b = 1;
    } else if (pp == 1)
      b = 0;
    else
      b = 1;

    int k;
    if (f == 1)
      k = 1;
    else if (i != s[n] - '0')
      k = 1;
    else
      k = 0;

    int temp = mod * 10 + i;
    res += solve(n + 1, i, b, k, temp % M);
  }
  return mem[n][p][pp][f][mod] = res;
}

int main() {
  int vr = 0;
  int vl = 0;
  cin >> str1 >> str2 >> M;
  for (int i = str1.size() - 1; i >= 0; i--) {
    if (str1[i] == '0') {
      str1[i] = '9';
    } else {
      str1[i]--;
      break;
    }
  }
  s = str1;
  memset(mem, -1, sizeof(mem));
  vl = solve(0, 0, 2, 0, 0);
  s = str2;
  memset(mem, -1, sizeof(mem));
  vr = solve(0, 0, 2, 0, 0);
  cout << (vr - vl) % MOD << endl;

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

#include <iostream>

using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
string A, B;
int M;
int memo[502][2][4][10][500];
// pos, less, hoge, pre, modM
string S;
const int up = 2, down = 3;
const int mod = 1e4;

int rec(int i, int j, int k, int l, int m) {

  if (memo[i][j][k][l][m] != -1)
    return memo[i][j][k][l][m];

  if (i == S.size()) {
    return memo[i][j][k][l][m] = ((k != 0) && (m == 0));
  }
  int ret = 0;
  int ue = (j ? 9 : S[i] - '0');
  rep(d, ue + 1) {
    int diff = d - l;
    int nj = (j || d < ue);
    int nk;
    int nm = (10 * m + d) % M;
    if (k == 0) {
      nk = (d != 0);
    } else if (k == 1) {
      if (diff == 0)
        continue;
      nk = (diff > 0 ? up : down);
    } else if (k == up && diff < 0) {
      nk = down;
    } else if (k == down && diff > 0) {
      nk = up;
    } else {
      continue;
    }
    (ret += rec(i + 1, nj, nk, d, nm)) %= mod;
  }

  return memo[i][j][k][l][m] = (ret % mod);
}

int calc(string N) {

  fill((int *)memo, (int *)memo + 502 * 2 * 4 * 10 * 500, -1);
  S = N;
  return rec(0, 0, 0, 0, 0);
}

void minus1() {

  int len = A.size();
  for (int i = 0; i < len; i++) {
    char ch = A[len - 1 - i];
    if (ch == '0') {
      A[len - 1 - i] = '9';
    } else {
      A[len - 1 - i] = ch - 1;
      break;
    }
  }
  if (A[0] == '0' && A.size() > 1)
    A = A.substr(1);
}

int main() {

  cin >> A >> B >> M;

  minus1();
  cout << (calc(B) - calc(A) + mod) % mod << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
string A, B;
int M;
int memo[501][2][4][10][500];
// pos, less, hoge, pre, modM
string S;
const int up = 2, down = 3;
const int mod = 1e4;

int rec(int i, int j, int k, int l, int m) {

  if (memo[i][j][k][l][m] != -1)
    return memo[i][j][k][l][m];

  if (i == S.size()) {
    return memo[i][j][k][l][m] = ((k != 0) && (m == 0));
  }
  int ret = 0;
  int ue = (j ? 9 : S[i] - '0');
  rep(d, ue + 1) {
    int diff = d - l;
    int nj = (j || d < ue);
    int nk;
    int nm = (10 * m + d) % M;
    if (k == 0) {
      nk = (d != 0);
    } else if (k == 1) {
      if (diff == 0)
        continue;
      nk = (diff > 0 ? up : down);
    } else if (k == up && diff < 0) {
      nk = down;
    } else if (k == down && diff > 0) {
      nk = up;
    } else {
      continue;
    }
    (ret += rec(i + 1, nj, nk, d, nm)) %= mod;
  }

  return memo[i][j][k][l][m] = ret;
}

int calc(string N) {

  fill((int *)memo, (int *)memo + 501 * 2 * 4 * 10 * 500, -1);
  S = N;
  return rec(0, 0, 0, 0, 0);
}

void minus1() {

  int len = A.size();
  for (int i = 0; i < len; i++) {
    char ch = A[len - 1 - i];
    if (ch == '0') {
      A[len - 1 - i] = '9';
    } else {
      A[len - 1 - i] = ch - 1;
      break;
    }
  }
  if (A[0] == '0' && A.size() > 1)
    A = A.substr(1);
}

int main() {

  cin >> A >> B >> M;

  minus1();
  cout << (calc(B) - calc(A) + mod) % mod << endl;

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
// dp[何文字目か][直前の数字][上下][自由に選べるか][余り]
// 上:0 下:1
int m;
string s;
char z[555];
int dp[501][10][3][2][500];
int solve(int n, int be, int ud, int ok, int md) {
  if (n == (int)s.size()) {
    // if(!md)cout << z << endl;
    return md ? 0 : 1;
  }
  if (dp[n][be][ud][ok][md] >= 0)
    return dp[n][be][ud][ok][md];

  int ret = 0;
  for (int i = 0; i <= ((ok) ? 9 : (int)(s[n] - '0')); i++) {
    if (ud == 0 && be <= i)
      continue;
    if (ud == 1 && be >= i)
      continue;
    if (ud == 2 && be != 0 && be == i)
      continue;
    int u;
    if (ud == 2) {
      if (be == 0)
        u = 2;
      else if (be > i)
        u = 1;
      else
        u = 0;
    } else {
      u = (ud + 1) % 2;
    }
    // z[n] = i + '0';
    ret += solve(n + 1, i, u, (ok || i != (int)(s[n] - '0')) ? 1 : 0,
                 (md * 10 + i) % m);
    // z[m] = '\\0';
  }
  return dp[n][be][ud][ok][md] = ret % 10000;
}

int main(void) {
  string c, d;
  cin >> c >> d >> m;
  for (int i = (int)c.size() - 1; i >= 0; i--) {
    if (c[i] == '0') {
      c[i] = '9';
    } else {
      c[i]--;
      break;
    }
  }
  s = c;
  memset(dp, -1, sizeof(dp));
  int a = solve(0, 0, 2, 0, 0);
  // cout << "A" << endl;
  s = d;
  memset(dp, -1, sizeof(dp));
  int b = solve(0, 0, 2, 0, 0);
  // cout << "B" << endl;
  // cout << a << endl;
  // cout << b << endl;
  cout << (b + 10000 - a) % 10000 << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
// dp[何文字目か][直前の数字][上下][自由に選べるか][余り]
// 上:0 下:1
int m;
string s;
// char z[555];
int dp[555][11][3][2][555];
int solve(int n, int be, int ud, int ok, int md) {
  if (n == (int)s.size()) {
    // cout << z << endl;
    return md ? 0 : 1;
  }
  if (dp[n][be][ud][ok][md] >= 0)
    return dp[n][be][ud][ok][md];

  int ret = 0;
  for (int i = 0; i <= ((ok) ? 9 : (int)(s[n] - '0')); i++) {
    if (ud == 0 && be <= i)
      continue;
    if (ud == 1 && be >= i)
      continue;
    if (ud == 2 && be != 0 && be == i)
      continue;
    int u;
    if (ud == 2) {
      if (be == 0)
        u = 2;
      else if (be > i)
        u = 1;
      else
        u = 0;
    } else {
      u = (ud + 1) % 2;
    }
    // z[n] = i + '0';
    ret += solve(n + 1, i, u, (ok || i != (int)(s[n] - '0')) ? 1 : 0,
                 (md * 10 + i) % m);
    // z[m] = '\\0';
  }
  return dp[n][be][ud][ok][md] = ret % 10000;
}

int main(void) {
  string c, d;
  cin >> c >> d >> m;
  for (int i = (int)c.size() - 1; i >= 0; i--) {
    if (c[i] == '0') {
      c[i] = '9';
    } else {
      c[i]--;
      break;
    }
  }
  s = c;
  memset(dp, -1, sizeof(dp));
  int a = solve(0, 0, 2, 0, 0);
  // cout << "A" << endl;
  s = d;
  memset(dp, -1, sizeof(dp));
  int b = solve(0, 0, 2, 0, 0);
  // cout << "B" << endl;
  // cout << a << endl;
  // cout << b << endl;
  cout << b - a << endl;
}
Problem p00494 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int main() {
  string str;
  int ans = 0, i = 0;
  cin >> str;
  while (i < str.size()) {
    int jcnt = 0, ocnt = 0, icnt = 0;
    if (str[i] == 'J') {
      bool flag = false;
      jcnt++;
      if (i + 1 >= str.size()) {
        break;
      }
      for (int j = i + 1; j < str.size(); ++j) {
        if (str[j] != 'J') {
          i = j;
          break;
        }
        jcnt++;
        if (j == str.size() - 1)
          i = j + 1;
      }
      if (i >= str.size()) {
        break;
      }
      for (int j = i; j < str.size(); ++j) {
        if (str[j] != 'O') {
          i = j;
          break;
        }
        ocnt++;
        flag = true;
        if (j == str.size() - 1)
          i = j + 1;
      }
      if (i >= str.size()) {
        break;
      }
      if (!flag)
        continue;
      for (int j = i; j < str.size(); ++j) {
        if (str[j] != 'I') {
          i = j;
          break;
        }
        icnt++;
      }
      if (jcnt >= ocnt && ocnt <= icnt)
        ans = max(ans, min(jcnt, ocnt));
    } else {
      i++;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int main() {
  string str;
  int ans = 0, i = 0;
  cin >> str;
  while (i < str.size()) {
    int jcnt = 0, ocnt = 0, icnt = 0;
    if (str[i] == 'J') {
      bool flag = false;
      jcnt++;
      if (i + 1 >= str.size()) {
        break;
      }
      for (int j = i + 1; j < str.size(); ++j) {
        if (str[j] != 'J') {
          i = j;
          break;
        }
        jcnt++;
      }
      if (i + 1 >= str.size()) {
        break;
      }
      for (int j = i; j < str.size(); ++j) {
        if (str[j] != 'O') {
          i = j;
          break;
        }
        ocnt++;
        flag = true;
      }
      if (i >= str.size()) {
        break;
      }
      if (!flag)
        continue;
      for (int j = i; j < str.size(); ++j) {
        if (str[j] != 'I') {
          i = j;
          break;
        }
        icnt++;
      }
      if (jcnt >= ocnt && ocnt <= icnt)
        ans = max(ans, min(jcnt, ocnt));
    } else {
      i++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00494 (C++) — buggy submission, verdict: Runtime Error

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

#define loop(i, n) for (int i = 1; i <= n; i++)
typedef pair<char, int> P;
vector<P> v;

int main() {
  char c;
  while (cin >> c) {
    if (!v.empty() && v.back().first == c) {
      v.back().second++;
    } else {
      v.push_back(P(c, 1));
    }
  }
  int m = 0;
  if (v.size() >= 3)
    loop(p, v.size() - 2) {
      if (v[p - 1].first == 'J' && v[p].first == 'O' && v[p + 1].first == 'I') {
        int j = v[p - 1].second;
        int o = v[p].second;
        int i = v[p + 1].second;
        if (j >= o && o <= i) {
          m = max(m, o);
        }
      }
    }
  cout << m << endl;
  return 0;
}

Error: 0

Fixed submission:

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

#define loop(i, n) for (int i = 1; i <= n; i++)
typedef pair<char, int> P;
vector<P> v;

int main() {
  char c;
  while (cin >> c) {
    if (!v.empty() && v.back().first == c) {
      v.back().second++;
    } else {
      v.push_back(P(c, 1));
    }
  }
  int m = 0;
  loop(p, v.size() - 2) {
    if (v[p - 1].first == 'J' && v[p].first == 'O' && v[p + 1].first == 'I') {
      int j = v[p - 1].second;
      int o = v[p].second;
      int i = v[p + 1].second;
      if (j >= o && o <= i) {
        m = max(m, o);
      }
    }
  }
  cout << m << endl;
  return 0;
}
Problem p00494 (C++) — buggy submission, verdict: 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] == 'I'; a3++)
      ;
    if (a1 - a0 >= a2 - a1 && a2 - a1 <= a3 - a2)
      ans = max(ans, a2 - a1);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

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] == 'J'; 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, verdict: 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] == 'I'; a3++)
      ;
    if (a1 - a0 >= a2 - a1 && a2 - a1 <= a3 - a2)
      R = max(R, a2 - a1);
  }
  cout << R << endl;
  return 0;
}

Error: TLE

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] == 'C'; 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, verdict: 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 = 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;
}


Error: TLE

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++) {
    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, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;

void print(vi v) {
  for (int i = 0; i < v.size(); i++) {
    cout << " " << v[i];
  }
  cout << endl;
}

int main() {
  string s;

  cin >> s;

  vi v(s.size(), 1);

  for (int i = 1; i < s.size(); i++) {
    if (s[i] == s[i - 1]) {
      v[i] = v[i - 1] + 1;
    } else {
      reverse(v.begin() + (i - v[i - 1]), v.begin() + i);
    }
  }
  reverse(v.begin() + (v.size() - 1 - v[v.size() - 2]), v.begin() + (v.size()));
  // print(v);
  // print(v);
  int ans = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'J') {
      if (i + v[i] < s.size() && s[i + v[i]] == 'O' && v[i + v[i]] == v[i]) {
        if (i + v[i] * 2 < s.size() && s[i + v[i] * 2] == 'I' &&
            v[i + v[i] * 2] >= v[i + v[i]]) {
          ans = max(ans, v[i]);
        }
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1000000;

int main() {
  string s;
  cin >> s;
  int ans = 0;
  string J = "J", O = "O", I = "I";
  while (J.size() + O.size() + I.size() <= s.size()) {
    if (s.find(J + O + I) != string::npos)
      ans = J.size();
    J += 'J';
    O += 'O';
    I += 'I';
  }
  cout << ans << endl;
}