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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

const int mod = 100000;

int main() {
  while (true) {

    int n, m, s;

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

    if (n == 0 && m == 0 && s == 0)
      break;

    n = n * n;
    s -= (0 + (n - 1)) * n / 2;
    m = min(m, s);

    static int dp[3001][2001] = {0};

    memset(dp[0], 0, 3001 * 2001 * sizeof(int));

    for (int i = 1; i <= m; ++i)
      dp[i][i] = 1;

    for (int i = 1; i < n; ++i) {

      for (int x = 1; x <= m - i; ++x) {

        for (int sum = s; sum > x; --sum)
          dp[sum][x] = (dp[sum - 1][x - 1] + dp[sum - x][x]) % mod;

        for (int sum = x; sum > 0; --sum)
          dp[sum][x] = (dp[sum - 1][x - 1]) % mod;
      }
    }

    int ans = 0;

    for (int i = 1; i <= m - (n - 1); ++i) {
      ans += dp[s][i];
      ans %= mod;
    }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

const int mod = 100000;

int main() {
  while (true) {

    int n, m, s;

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

    if (n == 0 && m == 0 && s == 0)
      break;

    n = n * n;
    s -= (0 + (n - 1)) * n / 2;
    m = min(m, s);

    static int dp[3001][2001] = {0};

    memset(dp[0], 0, 3001 * 2001 * sizeof(int));

    for (int i = 1; i <= m; ++i)
      dp[i][i] = 1;

    for (int i = 1; i < n; ++i) {

      for (int x = 1; x <= m - i; ++x) {

        for (int sum = s; sum > 0; --sum) {

          dp[sum][x] = 0;
          if (1 < sum && 1 < x)
            dp[sum][x] += dp[sum - 1][x - 1];
          if (0 < sum - x)
            dp[sum][x] += dp[sum - x][x];
          dp[sum][x] %= mod;
        }
      }
    }

    int ans = 0;

    for (int i = 1; i <= m - (n - 1); ++i) {
      ans += dp[s][i];
      ans %= mod;
    }

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

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

#include <iostream>
#include <vector>
#define repeat(i, n) for (int i = 0; (i) < (n); ++(i))
#define repeat_reverse(i, n) for (int i = (n)-1; (i) >= 0; --(i))
using namespace std;
const int mod = 100000;
int main() {
  while (true) {
    int n, m, s;
    cin >> n >> m >> s;
    if (n == 0 and m == 0 and s == 0)
      break;
    vector<vector<int>> dp(n * n + 1, vector<int>(s + 1));
    dp[0][0] = 1;
    repeat(i, m) {
      repeat_reverse(j, n * n) {
        repeat(k, s - i) {
          dp[j + 1][k + (i + 1)] += dp[j][k];
          dp[j + 1][k + (i + 1)] %= mod;
        }
      }
    }
    cout << dp[n * n][s] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#define repeat(i, n) for (int i = 0; (i) < (n); ++(i))
using namespace std;
const int mod = 100000;
int dp[2][3001][2001];
int (&cur)[3001][2001] = dp[0];
int (&prv)[3001][2001] = dp[1];
int main() {
  while (true) {
    int n, m, s;
    cin >> n >> m >> s;
    if (n == 0 and m == 0 and s == 0)
      break;
    // vector<vector<int> > cur(s+1, vector<int>(m+1));
    // vector<vector<int> > prv(s+1, vector<int>(m+1));
    memset(dp, 0, sizeof(dp));
    cur[0][0] = 1;
    repeat(i, n * n) {
      // prv.swap(cur);
      swap(cur, prv);
      repeat(j, s + 1) {
        cur[j][0] = 0;
        repeat(k, min(m, j)) {
          cur[j][k + 1] = (cur[j - 1][k] + prv[j - k - 1][k]) % mod;
        }
      }
    }
    // int ans = accumulate(cur[s].begin(), cur[s].end(), 0ll) % mod;
    long long ans = 0;
    repeat(i, m + 1) ans += cur[s][i];
    ans %= mod;
    cout << ans << endl;
  }
  return 0;
}
Problem p00460 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
const int mod = 100000;
int N, M, S;
int main() {
  while (cin >> N >> M >> S, N) {
    N *= N;
    M -= N;
    S -= N * (N + 1) / 2;
    vector<vector<int>> dp(M + 1, vector<int>(S + 1, 0));
    dp[0][0] = 1;
    for (int i = 1; i <= N; i++) {
      vector<vector<int>> dp2(M + 1, vector<int>(S + 1, 0));
      for (int j = 0; j <= M; j++) {
        for (int k = j; k <= S; k++) {
          dp2[j][k] = dp[j][k - j];
          if (j >= 1) {
            dp2[j][k] += dp2[j - 1][k - 1];
            if (dp2[j][k] >= mod)
              dp2[j][k] -= mod;
          }
        }
      }
      dp = dp2;
    }
    int ret = 0;
    for (int i = 0; i <= M; i++) {
      ret += dp[i][S];
      if (ret >= mod)
        ret -= mod;
    }
    cout << ret << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
const int mod = 100000;
int N, M, S;
int main() {
  while (cin >> N >> M >> S, N) {
    N *= N;
    M -= N;
    S -= N * (N + 1) / 2;
    vector<vector<vector<int>>> dp(
        N + 1, vector<vector<int>>(M + 1, vector<int>(S + 1, 0)));
    dp[0][0][0] = 1;
    for (int i = 1; i <= N; i++) {
      for (int j = 0; j <= M; j++) {
        for (int k = j; k <= S; k++) {
          dp[i][j][k] = dp[i - 1][j][k - j];
          if (j >= 1)
            dp[i][j][k] += dp[i][j - 1][k - 1];
          dp[i][j][k] %= mod;
        }
      }
    }
    int ret = 0;
    for (int i = 0; i <= M; i++) {
      ret += dp[N][i][S];
      ret %= mod;
    }
    cout << ret << endl;
  }
  return 0;
}
Problem p00460 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORE(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(i, b) FOR(i, 0, b)
using namespace std;
typedef long long ll;
int DP[2][2001][3001];
int main() {
  int N, M, S;
  while (cin >> N >> M >> S && N) {
    int sum = 0;
    REP(i, 2)
    REP(j, 2001)
    REP(k, 3001)
    DP[i][j][k] = 0;
    DP[0][0][0] = 1;
    N *= N;
    FORE(i, 1, N)
    FORE(j, 0, M)
    FORE(k, 0, S)
    if (j <= k && j)
      DP[i % 2][j][k] =
          (DP[i % 2][j - 1][k - 1] + DP[(i + 1) % 2][j - 1][k - j]) % 100000;
    else
      DP[i % 2][j][k] = 0;
    FORE(i, 1, M)
    sum = (sum + DP[N % 2][i][S]) % 100000;
    cout << sum % 100000 << endl;
  }
  // your code goes here
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
using namespace std;
int DP[50][2001][3001];
int main() {
  while (true) {
    int N, M, S;
    cin >> N >> M >> S;
    if (N == M && M == S && S == 0)
      break;
    for (int i = 1; i <= M; i++) {
      DP[1][i][i] = 1;
    }
    for (int i = 2; i <= N * N; i++) {       // 今回の枚数
      for (int j = i - 1; j <= M; j++) {     // 前回のカード
        for (int k = 1; k <= S; k++) {       // 今回の総和
          for (int s = j + 1; s <= M; s++) { // 今回のカード
            if (k - s >= 0)
              DP[i][s][k] = (DP[i][s][k] + DP[i - 1][j][k - s]) % 100000;
          }
        }
      }
    }
    int sum = 0;
    for (int i = 1; i <= M; i++) {
      sum = (sum + DP[N * N][i][S]) % 100000;
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00460 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

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

int dp[50][3001];
const int MOD = 100000;

int main() {
  while (true) {
    int n, m, s;
    cin >> n >> m >> s;
    if (n == 0)
      break;
    n = n * n;
    rep(i, 50) rep(j, 3001) dp[i][j] = 0;
    dp[0][0] = 1;
    rep(i, m + 1) if (i > 0) for (int j = n; j > 0; --j) for (int k = i; k <= s;
                                                              ++k) {
      dp[j][k] += dp[j - 1][k - i];
      dp[j][k] %= MOD;
    }
    cout << dp[n][s] << endl;
  }
}

Error: WA

Fixed submission:

#include <iostream>

using namespace std;

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

int dp[50][3001];
const int MOD = 100000;

int main() {
  while (true) {
    int n, m, s;
    cin >> n >> m >> s;
    if (n == 0)
      break;
    n = n * n;
    rep(i, 50) rep(j, 3001) dp[i][j] = 0;
    dp[0][0] = 1;
    rep(i, m + 1) if (i > 0) for (int j = n; j > 0; --j) for (int k = j; k <= s;
                                                              ++k) {
      dp[j][k] += dp[j - 1][k - i];
      dp[j][k] %= MOD;
    }
    cout << dp[n][s] << endl;
  }
}
Problem p00460 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define MAX_N 2000
#define MAX_M 3000

int x[MAX_N + 1][MAX_M + 1];
int y[MAX_N + 1][MAX_M + 1];

int z[50][MAX_M];
int sum = 0;

int main() {
  int n, m, s, d, e, f, g;
  while (true) {
    cin >> n >> m >> s;
    if ((n == 0 && m == 0) && s == 0) {
      break;
    }
    memset(x, 0, sizeof(x));
    memset(y, 0, sizeof(y));
    memset(z, 0, sizeof(z));

    if (m < s - ((n * n) - 2) * ((n * n) - 1) / 2) {
      memset(y, 0, sizeof(y));
      for (int i = 1; i <= m; i++) {
        x[i][i] = 1;
      }
      for (int i = 2; i <= n * n; i++) {
        d = s / (n * n - i + 1);
        e = min(m, d);
        f = s * i / (n * n) + i - n * n;
        g = (i - 2) * (i - 1) / 2;
        for (int j = i - 1; j <= e; j++) {
          for (int k = j + g; k <= f; k++) {
            for (int l = j + 1; l <= e; l++) {
              y[l][k + l] += x[j][k];
            }
          }
        }
        y[2000][3000] = 0;
        for (int j = i; j <= e; j++) {
          for (int k = j; k <= s; k++) {
            x[j][k] = y[j][k] % 100000;
            y[j][k] = 0;
          }
        }
        y[0][0] = 0;
      }
      sum = 0;
      for (int i = 0; i <= m; i++) {
        sum += x[i][s];
        sum %= 100000;
      }
    }

    else {
      z[0][0] = 1;
      for (int i = 1; i <= n * n; i++) {
        d = n * n - i + 1;
        for (int j = 0; j <= s; j++) {
          e = 0;
          while (j + e * d <= s) {
            z[i][j + e * d] += z[i - 1][j];
            z[i][j + e * d] %= 100000;
            e++;
          }
        }
      }
      sum = z[n * n][s - ((n * n) + 1) * (n * n) / 2] % 100000;
    }

    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define MAX_N 2000
#define MAX_M 3000

int x[MAX_N + 1][MAX_M + 1];
int y[MAX_N + 1][MAX_M + 1];

int main() {
  int n, m, s, d, e, f, g;
  while (true) {
    cin >> n >> m >> s;
    if ((n == 0 && m == 0) && s == 0) {
      break;
    }
    memset(x, 0, sizeof(x));
    memset(y, 0, sizeof(y));
    for (int i = 1; i <= m; i++) {
      x[i][i] = 1;
    }
    for (int i = 2; i <= n * n; i++) {
      d = s / (n * n - i + 1);
      e = min(m, d);
      f = s * i / (n * n) + i - n * n;
      g = (i - 2) * (i - 1) / 2;
      for (int j = i - 1; j <= e; j++) {
        for (int k = j + g; k <= f; k++) {
          for (int l = j + 1; l <= e; l++) {
            y[l][k + l] += x[j][k];
          }
        }
      }
      y[2000][3000] = 0;
      for (int j = i; j <= e; j++) {
        for (int k = j; k <= s; k++) {
          x[j][k] = y[j][k] % 100000;
          y[j][k] = 0;
        }
      }
      y[0][0] = 0;
    }
    int sum = 0;
    for (int i = 0; i <= m; i++) {
      sum += x[i][s];
      sum %= 100000;
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00460 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>
using namespace std;

int D = 100000;
int dp[2][50][3001];
int n, m, s;

void set() {
  int x, y, z;
  for (x = 0; x < 2; x++) {
    for (y = 0; y <= n * n; y++) {
      for (z = 0; z <= s; z++) {
        dp[x][y][z] = 0;
      }
    }
  }
  dp[0][0][0] = 1;
  for (y = 1; y <= m; y++) {
    for (x = 0; x <= n * n; x++) {
      for (z = 0; z <= s; z++) {
        if (x && z >= y) {
          dp[y % 2][x][z] =
              (dp[(y - 1) % 2][x][z] + dp[(y - 1) % 2][x - 1][z - y]);
        } else {
          dp[y % 2][x][z] = dp[(y - 1) % 2][x][z];
        }
        dp[y % 2][x][z] %= D;
      }
    }
  }
}

int main() {
  while (cin >> n >> m >> s, n | m | s) {
    set();
    cout << dp[m % 2][n * n][s] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:


#include <iostream>
using namespace std;

long long int D = 100000;
int dp[2][50][3000];
int n, m, s;

void set() {
  int x, y, z;
  for (x = 0; x < 2; x++) {
    for (y = 0; y < 50; y++) {
      for (z = 0; z < 3000; z++) {
        dp[x][y][z] = 0;
      }
    }
  }
  dp[0][0][0] = 1;
  for (y = 1; y <= m; y++) {
    for (x = 0; x <= n * n; x++) {
      for (z = 0; z <= s; z++) {
        if (x && z >= y) {
          dp[y % 2][x][z] =
              (dp[(y - 1) % 2][x][z] + dp[(y - 1) % 2][x - 1][z - y]);
        } else {
          dp[y % 2][x][z] = dp[(y - 1) % 2][x][z];
        }
        dp[y % 2][x][z] %= D;
      }
    }
  }
}

int main() {
  while (cin >> n >> m >> s, n | m | s) {
    long long int ans = 0;
    set();
    cout << dp[m % 2][n * n][s] << endl;
  }
  return 0;
}
Problem p00460 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int dp[2200][3200], _prev[2200][3200], n, m, s;
int main() {
  while (true) {
    cin >> n >> s >> m;
    if (n == 0)
      return 0;
    n *= n;
    for (int i = 0; i < 2200; i++) {
      fill(dp[i], dp[i] + 3180, 0);
      fill(_prev[i], _prev[i] + 3180, 0);
    }
    for (int i = 0; i <= s; i++)
      _prev[i][0] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= s; j++) {
        for (int k = j; k <= m; k++) {
          dp[j][k] += _prev[j - 1][k - j];
          dp[j][k] %= 100000;
        }
      }
      for (int j = 0; j <= s; j++) {
        for (int k = 0; k <= m; k++) {
          _prev[j][k] = dp[j][k];
          dp[j][k] = 0;
        }
      }
      for (int j = 1; j <= s; j++) {
        for (int k = 1; k <= m; k++) {
          _prev[j][k] += _prev[j - 1][k];
        }
      }
    }
    cout << _prev[s][m] % 100000 << endl;
  }
}

Error: MLE

Fixed submission:

#include <iostream>
using namespace std;
int dp[2500][3500], _prev[2500][3500], n, m, s;
int main() {
  while (true) {
    cin >> n >> s >> m;
    if (n == 0)
      return 0;
    n *= n;
    for (int i = 0; i < 8750000; i++) {
      _prev[i / 3500][i % 3500] = 0;
      dp[i / 3500][i % 3500] = 0;
    }
    for (int i = 0; i <= s; i++)
      _prev[i][0] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= s; j++) {
        for (int k = j; k <= m; k++) {
          dp[j][k] += _prev[j - 1][k - j];
          dp[j][k] %= 100000;
        }
      }
      for (int j = 0; j <= s; j++) {
        for (int k = 0; k <= m; k++) {
          _prev[j][k] = dp[j][k];
          dp[j][k] = 0;
        }
      }
      for (int j = 1; j <= s; j++) {
        for (int k = 1; k <= m; k++) {
          _prev[j][k] += _prev[j - 1][k];
        }
      }
    }
    cout << _prev[s][m] % 100000 << endl;
  }
}
Problem p00460 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int dp[55][3010];
  int n, m, s;
  while (1) {
    for (int i = 0; i < 55; i++)
      for (int j = 0; j < 3010; j++)
        dp[i][j] = 0;
    cin >> n >> m >> s;
    if (n == 0 && m == 0 && s == 0)
      break;
    int bin = n * n;
    dp[0][0] = 1;
    for (int k = 1; k <= m; k++)
      for (int i = bin; i > 0; i--) {
        for (int j = k; j <= s; j++) {
          dp[i][j] = (dp[i - 1][j - k] + dp[i][j]) % 100000;
        }
      }
    cout << dp[bin][s] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
//????????§??¶????????????
int main() {
  int dp[55][3010];
  int n, m, s;
  while (1) {
    fill((int *)dp, ((int *)dp + 3010), 0);
    cin >> n >> m >> s;
    const int bin = n * n;
    dp[0][0] = 1;
    for (int k = 1; k <= m; k++)
      for (int i = bin; i > 0; i--) {
        for (int j = k; j <= s; j++) {
          dp[i][j] = (dp[i - 1][j - k] + dp[i][j]) % 100000;
        }
      }
    cout << dp[bin][s] << endl;
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  for (;;) {
    cin >> n;
    if (!n)
      break;
    cin >> m;
    cin >> x;

    int ans = 0;
    for (int i = 0; i < m; i++) {
      if (x[i] == 'I') {
        int dif = 1;
        while (i + 2 * dif < m && x[i + 2 * dif - 1] == 'O' &&
               x[i + 2 * dif] == 'I')
          dif++;
        dif--;
        if (dif >= n)
          ans += dif - n + 1;
        i += 2 * dif;
      }
    }

    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

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

  for (;;) {
    cin >> n;
    if (!n)
      break;
    cin >> m;
    cin >> x;

    string a = "I";
    for (int i = 0; i < n; i++)
      a += "OI";

    int ans = 0;
    string::size_type pos = 0;
    ;
    while (1) {
      pos = x.find(a, pos);
      if (pos == string::npos)
        break;
      ans++;
      pos++;
    }

    cout << ans << endl;
  }
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

// 2007-yo-2のように直接探す方法では、最大O(MN/2)となり、制限時間内に終わらない。そこで、2011-ho-1のように交互列を考える。
#include <stdio.h>

int N, M;
int data[1000000];
int main() {
  char c[2];
  int count, corsole;
  int num; // 個数
  int i;
  while (1) {
    count = 0;
    corsole = 0;
    num = 0;
    scanf("%d", &N);
    if (N == 0)
      break;
    scanf("%d", &M);
    do {
      scanf("%c", c + 1);
    } while (c[1] != 'I' && c[1] != 'O');
    for (i = 1; i < M;) {
      while (c[1] != 'I' && i < M) {
        c[0] = c[1];
        scanf("%c", c + 1);
        i++;
      }
      c[0] = 'X'; // ループを抜けた際の履歴が次回に影響しないようにする。
      while (c[1] != c[0]) {
        count++;
        if (i >= M)
          break;
        c[0] = c[1];
        scanf("%c", c + 1);
        i++;
      }
      data[corsole] = (count - 1) / 2;
      if (data[corsole] > 0)
        corsole++;
      count = 0;
    }
    for (i = 0; i <= corsole; i++) {
      if (N <= data[i]) {
        num += data[i] - N + 1;
      }
    }
    printf("%d\\n", num);
  }
  return 0;
}

Error: TLE

Fixed submission:

// 2007-yo-2のように直接探す方法では、最大O(MN/2)となり、制限時間内に終わらない。そこで、2011-ho-1のように交互列を考える。
#include <stdio.h>

int N, M;
int data[1000000];
int main() {
  char c[2];
  int count, corsole;
  int num; // 個数
  int i;
  while (1) {
    count = 0;
    corsole = 0;
    num = 0;
    scanf("%d", &N);
    if (N == 0)
      break;
    scanf("%d%c", &M, c + 1);
    for (i = 1; i < M;) {
      while (c[1] != 'I' && i < M) {
        c[0] = c[1];
        scanf("%c", c + 1);
        i++;
      }
      while (c[1] != c[0] && i < M) {
        c[0] = c[1];
        scanf("%c", c + 1);
        count++;
        i++;
      }
      data[corsole] = (count - 1) / 2;
      if (data[corsole] > 0)
        corsole++;
      count = 0;
    }
    for (i = 0; i <= corsole; i++) {
      if (N <= data[i]) {
        num += data[i] - N + 1;
      }
    }
    printf("%d\\n", num);
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

char ss[1000100];

int main() {
  int n, m, i, j, c, flag, t;

  while (~scanf("%d", &n)) {
    if (n == 0)
      break;
    scanf("%d", &m);

    scanf(" %s ", ss);

    c = 0;
    t = 0;
    flag = 0;

    for (i = 0; i < m; i++) {
      if (flag == 0 && ss[i] == 'I') {
        flag = (i + 1) % 2 + 1;
        t = i;
      }
      //			printf("%d %d\\n",i,t);
      if (flag == 1 && i % 2 == 0 && ss[i] == 'O') {
        continue;
      } else if (flag == 1 && i % 2 == 1 && ss[i] == 'I') {
        continue;
      } else if (flag == 2 && i % 2 == 0 && ss[i] == 'I') {
        continue;
      } else if (flag == 2 && i % 2 == 1 && ss[i] == 'O') {
        continue;
      } else if (flag != 0) {
        if (i - t > 2 * n) {
          if ((i - t) % 2 == 1)
            c += (i - t - 2 * n + 1) / 2;
          else
            c += (i - t - 2 * n) / 2;
          //					printf("%d\\n",c);
        }
        if (ss[i] == 'I') {
          flag = (i + 1) % 2 + 1;
          t = i;
          continue;
        }
        flag = 0;
      }
    }

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

Error: TLE

Fixed submission:

#include <stdio.h>

char ss[1000100];

int main() {
  int n, m, i, j, c, flag;

  while (~scanf("%d", &n)) {
    if (n == 0)
      break;
    scanf("%d", &m);

    scanf(" %s ", ss);

    c = 0;

    for (i = 0; i < m - 2 * n; i++) {
      flag = 1;
      for (j = 0; j < 2 * n + 1; j++) {
        if (j % 2) {
          if (ss[i + j] != 'O')
            flag = 0;
        } else {
          if (ss[i + j] != 'I')
            flag = 0;
        }
      }
      c += flag;
    }

    printf("%d\\n", c);
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int i, j, n, m, ans;
  string str;
  vector<int> v;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cin >> m;
    cin >> str;

    v.push_back(0);
    v.push_back(0);
    for (i = 2; i < m; i++) {
      if (str[i] == 'I' && str[i - 1] == 'O' && str[i - 2] == 'I')
        v.push_back(v[i - 2] + 1);
      else
        v.push_back(0);
    }

    ans = 0;
    for (i = 2; i < m; i++) {
      if (v[i] >= n)
        ans++;
    }

    cout << ans << endl;
    v.clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int i, j, n, m, c, ans;
  string str;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cin >> m;
    cin >> str;

    ans = 0;
    for (i = 0; i < m - 2 * n; i++) {
      if (str[i] == 'I' && str[i + 1] == 'O') {
        c = 0;
        for (j = i + 1; j < i + 1 + n * 2; j += 2) {
          if (str[j] == 'O' && str[j + 1] == 'I')
            c++;
          else
            break;
        }
        if (c == n) {
          ans++;
          i++;
        } else if (c > 0)
          i += 2 * c;
      }
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long int
#define rep(a, b) for (int a = 0; a < b; a++)
#define pb push_back
#define str string
#define INF
#define sf(a) scanf("%d", &a)
using namespace std;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, num, ans = 0, b;
  char s, mae;
  str st;
  // vector<int> ans;
  while (1) {
    cin >> n;
    // cout << n;
    if (!n)
      break;
    cin >> num;
    cin >> st;
    int sum = 0;
    bool ok = false;
    ans = 0;
    for (int j = 2; j < num;) {
      if (num < j)
        break;
      if (st[j - 2] == 'I' && st[j - 1] == 'O' && st[j] == 'I') {
        ok = true;
        sum++;
        //  cout << sum << "a";
        if (sum >= n) {
          ans++;
          sum--;
        }
      } else {
        ok = false;
        sum = 0;
      }
      j += (ok) ? 2 : 1;
    }
    if (sum >= n) {
      ans++;
      sum--;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long int
#define rep(a, b) for (int a = 0; a < b; a++)
#define pb push_back
#define str string
#define INF
#define sf(a) scanf("%d", &a)
using namespace std;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, num, ans = 0;
  char s, mae;
  str st;
  // vector<int> ans;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cin >> num;
    rep(i, num) { cin >> st[i]; }
    int j = 2, sum = 0;
    bool ok = false;
    ans = 0;
    while (1) {
      if (num < j)
        break;

      if (st[j - 2] == 'I' && st[j - 1] == 'O' && st[j] == 'I') {
        ok = true;
        sum++;
        if (sum >= n)
          ans++;
      } else {
        ok = false;
        sum = 0;
      }
      j += (ok) ? 2 : 1;
    }
    cout << ans << endl;
  }
  return 0;
}