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

#include <stdio.h>
#include <string.h>
int main() {
  int n, m, ans = 0, count = 0, If = 1, Of = 0, i;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    scanf("%d", &m);
    char s1[m];
    scanf("%s", s1);
    for (i = 0; i < m; i++) {
      if (If == 1) {
        if (s1[i] == 'I') {
          If = 0;
          Of = 1;
          count++;
          if (count >= 2 * n + 1) {
            ans++;
          }
        } else {
          count = 0;
        }
      } else if (Of == 1) {
        If = 1;
        Of = 0;
        if (s1[i] == 'O') {
          count++;
        } else if (s1[i] == 'I') {
          If = 0;
          Of = 1;
          count = 1;
        } else {
          count = 0;
        }
      }
    }
    printf("%d\\n", ans);
    count = 0;
    ans = 0;
    If = 1;
    Of = 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m, count = 0;
  string s1[1], s2 = "IOI";

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cin >> m >> s1[0];
    for (long long int i = 1; i < n; i++) {
      s2 += "OI";
    }
    while (1) {
      if (s1[0].find(s2) == string::npos)
        break;
      s1[0][s1[0].find(s2)] = 'J';
      count++;
    }
    cout << count << endl;
    count = 0;
    s2 = "IOI";
  }

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

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

int main(void) {
  int n, m;
  string str;
  while (cin >> n, n) {
    cin >> m >> str;
    int cnt = 0, res = 0;
    for (int i = 1; i < m - 1; i++) {
      if (str[i - 1] == 'I' && str[i] == 'O' && str[i + 1] == 'I') {
        cnt++;
        i++;
      } else
        cnt = 0;
      if (n <= cnt)
        res++;
    }
    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define sz size()
#define lgt length()
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(X) (X).begin(), (X).end()
#define RALL(X) (X).rbegin(), (X).rend()
#define pb push_back
#define SORT(a) sort(ALL(a));
#define RSORT(a) sort(RALL(a));
#define mp make_pair
#define dump(x) cerr << #x << " = " << (x) << endl;
#define fir first
#define sec second

typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<string> vs;

int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
vs form(string a, char s) {
  vs v;
  string b;
  rep(i, a.lgt) {
    if (a[i] == s) {
      v.pb(b);
      b = "";
    } else
      b += a[i];
  }
  v.pb(b);
  return v;
}
vi formint(string a, char s) {
  vi v;
  string b;
  rep(i, a.lgt) {
    if (a[i] == s) {
      v.pb(toInt(b));
      b = "";
    } else
      b += a[i];
  }
  v.pb(toInt(b));
  return v;
}

const double EPS = 1e-10, PI = acos(-1.0);
const int INF = 2100000000;

int main(void) {
  int n, m;
  while (cin >> n, n) {
    int res = 0, lenp = (2 * n) + 1, lens = 0;
    string p, s;
    cin >> m >> s;
    lens = s.lgt;

    p += 'I';
    while (n--) {
      p += "OI";
    }
    for (int i = 0; i < (lens - lenp); i++) {
      if (p == s.substr(i, lenp))
        res++;
    }
    cout << res << endl;
  }

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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)

#define mp make_pair
#define P pair<int, int>
#define ll __int64
int n, ts;
const int MA = 1000003;
char t[MA];
int cnt[MA];
int solve[MA];
int main() {
  while (true) {
    rep(j, MA) t[j] = solve[j] = cnt[j] = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    scanf("%d", &ts);
    scanf("%s", t);

    int i = 2, ans = 0;

    while (i <= ts) {
      if (t[i] == 'I') {
        if (t[i - 1] == 'O' && t[i - 2] == 'I') {
          cnt[i] = cnt[i - 2] + 1;
        }
      }
      i++;
    }

    // rep(j,ts)cout<<cnt[j]<<" ";cout<<endl;
    rep(j, ts) if (cnt[j] >= n) ans++;

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


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)

#define mp make_pair
#define P pair<int, int>
#define ll __int64
int n, ts;
char t[10000];
int cnt[20000];
int solve[20000];
int main() {
  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;
    scanf("%d", &ts);
    scanf("%s", t);

    int i = 2, ans = 0;

    while (i <= ts) {
      if (t[i] == 'I') {
        while (t[i - 1] == 'O' && t[i - 2] == 'I') {
          cnt[i] = cnt[i - 2] + 1;
          i += 2;
        }
      }
      i++;
    }

    int pos = 0;

    rep(j, ts) if (cnt[j] != 0) solve[pos++] = cnt[j];

    // rep(j,pos)cout<<solve[j]<<" ";
    rep(j, pos) if (solve[j] >= n) ans++;
    cout << ans << endl;
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(int argc, char *argv[]) {
  int n, m;
  string s;
  while (std::cin >> n >> m >> s, n) {
    string p;
    for (int i = 0; i < n; i++) {
      p += "IO";
    }
    p += "I";
    if (p.length() > s.length()) {
      std::cout << 0 << std::endl;
      return 0;
    }
    int ans = 0, pos = 0;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] != p[pos]) {
        if (s[i] == 'I')
          pos = 1;
        else
          pos = 0;
      } else {
        pos++;
      }
      if (pos == p.length()) {
        ans++;
        i -= p.length() - 1;
        pos = 0;
      }
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main(int argc, char *argv[]) {
  int n, m;
  string s;
  while (std::cin >> n >> m >> s, n) {
    string p;
    for (int i = 0; i < n; i++) {
      p += "IO";
    }
    p += "I";
    if (p.length() > s.length()) {
      std::cout << 0 << std::endl;
      return 0;
    }
    int ans = 0;
    for (int i = 0; i < s.length() - p.length(); i++) {
      for (int j = 0; j < p.length(); j++) {
        if (s[i + j] != p[j])
          break;
        if (j == p.length() - 1)
          ans++;
      }
    }
    std::cout << ans << std::endl;
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int ans;
void kmp_maketable(const char *key, int *table) {
  table[0] = -1;
  table[1] = 0;
  int i = 2, j = 0;
  while (i == 0 || key[i - 1]) {
    if (key[i - 1] == key[j]) {
      table[i++] = ++j;
    } else if (j > 0) {
      j = table[j];
    } else {
      table[i++] = 0;
    }
  }
}

void kmp_search(const char *str, const char *key, const int *table) {
  int i = 0, j = 0;
  while (str[i]) {
    if (str[i] == key[j]) {
      if (!key[j + 1]) {
        //                printf("B: %d\\n", i-j);
        ans++;
      }
      i++, ++j;
    } else if (j > 0) {
      j = table[j];
    } else {
      i++;
    }
  }
}

int main() {

  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    int m;
    cin >> m;
    char s[1000010], p[1000010];
    scanf("%s", s);

    for (int i = 0; i < 2 * n + 1; i++) {
      if (i % 2 == 0)
        p[i] = 'I';
      else
        p[i] = 'O';
    }
    p[2 * n + 1] = '\\0';

    int table[1000010];
    //    string s,p = "IO";
    ans = 0;
    kmp_maketable(p, table);
    kmp_search(s, p, table);

    cout << ans << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {

  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    int m;
    cin >> m;
    string s, p = "IO";
    cin >> s;
    int ans = 0;

    for (int i = 0; i <= m - (2 * n + 1); i++) {
      bool f = true;
      for (int j = 0; j < (2 * n + 1); j++) {
        if (s[i + j] != p[j % 2]) {
          f = false;
          break;
        }
      }
      if (f)
        ans++;
    }
    cout << ans << "\\n";
  }
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, m, l, count;
  while (cin >> n, n) {
    string s, ans = "I";
    cin >> m >> s;
    l = -1;
    count = 0;
    for (int i = 0; i < n; i++)
      ans += "OI";
    while (s.find(ans, l + 1) != -1) {
      count++;
      l = s.find(ans, l + 1);
      // cout << l << endl;
    }
    cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int s, n;
  while (cin >> s, s) {
    cin >> n;
    char c[n + 2];
    cin >> c;
    c[n + 1] = 'A';
    int t = 1, f = 0;
    int count = 0;
    while (f < n) {
      while (c[t] == 'O')
        t++;
      if (c[t] == 'A')
        break;
      f = t + 1;
      while (f - t < s * 2) {
        if (c[f] == 'O' && (f - t) % 2 == 1)
          f++;
        else if (c[f] == 'I' && (f - t) % 2 == 0)
          f++;
        else {
          // t = f - 1;
          break;
        }
        if (f - t == s * 2) {
          count++;
          t += 2;
        }
      }
      t++;
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;
const int MAX = 1000010;
int dp[MAX];
string str;

int rec(int at, int end, int n) {
  if (at >= end)
    return 0;
  //	cout<<"    rec:string["<<at<<"] "<<str[at]<<endl;
  if (str[at] == 'O')
    return 0;
  if (str[at] == 'I' && str[at + 1] == 'O' && str[at + 2] == 'I') {
    //	cout<<"IOI"<<endl;
    dp[at]++;
    if (dp[at + 2] != 0) {
      dp[at] += dp[at + 2];
      return dp[at];
    } else {
      return (dp[at] += rec(at + 2, end, n));
    }
  }
  return 0;
}

int main() {
  while (true) {
    int n, _len, len;
    int ans;
    cin >> n;
    if (!n)
      break;
    cin >> len >> str;

    /*		for(int i=0;i<len;i++)printf("%2d",i);
                    cout<<endl;
                    for(int i=0;i<len;i++){
                            cout<<" "<<str[i];
                    }
                    cout<<endl;
                    for(int i=0;i<len;i++){
                            cout<<" "<<dp[i];
                    }
                    cout<<endl;
    */
    _len = len;
    int count = 0;
    for (--len; true; len--) {
      // cout<<"now:string["<<len<<"] "<<str[len]<<endl;
      if (len - (n * 2) < 0)
        break;
      if (str[len] == 'O')
        continue;
      if (rec(len - (n * 2), len, n) >= n) {
        count++;
      }
    }
    cout << count << endl;
    for (int i = 0; i < _len; i++)
      dp[i] = 0;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
const int MAX = 1000000;
int dp[MAX];
string str;

int rec(int at, int end, int n) {
  if (at >= end)
    return 0;
  //	cout<<"    rec:string["<<at<<"] "<<str[at]<<endl;
  if (str[at] == 'O')
    return 0;
  if (str[at] == 'I' && str[at + 1] == 'O' && str[at + 2] == 'I') {
    //	cout<<"IOI"<<endl;
    dp[at]++;
    if (dp[at + 2] != 0) {
      dp[at] += dp[at + 2];
      return dp[at];
    } else {
      return (dp[at] += rec(at + 2, end, n));
    }
  }
  return 0;
}

int main() {
  while (true) {
    int n, _len, len;
    int ans;
    cin >> n;
    if (!n)
      break;
    cin >> len >> str;

    /*		for(int i=0;i<len;i++)printf("%2d",i);
                    cout<<endl;
                    for(int i=0;i<len;i++){
                            cout<<" "<<str[i];
                    }
                    cout<<endl;
                    for(int i=0;i<len;i++){
                            cout<<" "<<dp[i];
                    }
                    cout<<endl;
    */
    _len = len;
    int count = 0;
    for (--len; true; len--) {
      // cout<<"now:string["<<len<<"] "<<str[len]<<endl;
      if (len - (n * 2) < 0)
        break;
      if (str[len] == 'O')
        continue;
      if (rec(len - (n * 2), len, n) >= n) {
        count++;
      }
    }
    cout << count << endl;
    for (int i = 0; i <= _len; i++)
      dp[i] = 0;
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

int memo[500001];

int main() {
  int n, m;
  while (std::cin >> n >> m, n) {
    for (int i = 0; i < 500001; i++) {
      memo[i] = 0;
    }

    bool flag = false;
    char prev_c, c;
    int combo = 0;
    for (int i = 0; i < m; i++) {
      std::cin >> c;

      if (flag) {
        if (prev_c == c) { // 清算処理
          for (int j = 1; j <= combo; j++) {
            memo[j] += combo - (j - 1);
          }
          flag = false;
          combo = 0;
        } else {
          combo += (c == 'I') ? 1 : 0;
        }
      }

      if (!flag && c == 'I') {
        flag = true;
      }

      prev_c = c;
    }

    std::cout << memo[n] << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

int findText(std::string text, std::string oldText) {
  int times = 0, pos = 0;
  while (pos = text.find(oldText, pos), pos != std::string::npos) {
    pos++, times++;
  }
  return times;
}

int main() {
  int n, m;
  std::string s;
  while (std::cin >> n, n) {
    std::cin >> m;
    std::cin >> s;
    std::string findstr = "I";
    for (int i = 0; i < n; i++)
      findstr += "OI";
    std::cout << findText(s, findstr) << std::endl;
  }
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

int main() {
  int n;
  string str;
  int res;
  for (; n = in();) {
    in();
    res = 0;
    cin >> str;
    int i;
    int len = 0;
    for (i = 0; i < str.size(); i++) {
      if (len == 0) {
        if (str[i] == 'I')
          len++;
      } else if (len % 2) {
        if (str[i] == 'O')
          len++;
        else
          len = 1;
      } else {
        if (str[i] == 'I')
          len++;
        else
          len = 0;
      }
      if (len % 2 && len > n * 2)
        res++;
      // cout<<len<<endl;
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

int main() {
  int n, m;
  while (n = in()) {
    cin >> m;
    string str;
    cin >> str;
    int i, j, res = 0;
    for (i = 0; i < m - 2 * n - 1; i++) {
      for (j = 0; j < 2 * n + 1; j++) {
        if (j % 2 == 0) {
          if (str[i + j] != 'I')
            goto a;
        } else {
          if (str[i + j] != 'O')
            goto a;
        }
      }
      res++;
    a:;
    }
    cout << res << endl;
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, m;
  while (cin >> n, n) {
    string s;
    cin >> m >> s;

    int dp[1000001] = {0};
    for (int i = 2; i < s.size(); i++) {
      if (s[i - 2] == 'I' && s[i - 1] == 'O' && s[i] == 'I') {
        dp[i] = dp[i - 2] + 1;
      }
    }
    int ans = 0;
    for (int i = 0; i < s.size(); i++) {
      if (dp[i] >= n) {
        ans++;
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

string make_IOI(int n) {
  string s;
  for (int i = 0; i < n; i++) {
    s += "IO";
  }
  s += "I";
  return s;
}

int main() {
  int n, m, ans;
  string s;

  while (cin >> n, n) {
    ans = 0;
    string IOI = make_IOI(n);
    cin >> m >> s;
    for (int i = 0; i < (int)s.size();) {
      int a = s.find(IOI, i);
      if (a == string::npos) {
        break;
      } else {
        ans++;
        i = 2 + a;
      }
    }
    cout << ans << endl;
  }
}
Problem p00461 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>

using namespace std;

typedef long long ll;

int cnt[1000000];

int main() {
  int n, m;
  string s;
  while (cin >> n && n) {
    cin >> m >> s;

    fill(cnt, cnt + 1000000, 0);
    for (int i = m - 1; i > 1; i--) {
      if (s[i] == 'I' && s[i - 1] == 'O' && s[i - 2] == 'I') {
        cnt[i - 2] = cnt[i] + 1;
      }
    }

    int ret = 0;
    for (int i = 0; i < m; i++) {
      if (cnt[i] >= n) {
        ret++;
      }
    }

    cout << ret << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

typedef long long ll;

int cnt[10000];

int main() {
  int n, m;
  string s;
  while (cin >> n && n) {
    cin >> m >> s;

    fill(cnt, cnt + 10000, 0);
    for (int i = m - 1; i > 1; i--) {
      if (s[i] == 'I' && s[i - 1] == 'O' && s[i - 2] == 'I') {
        cnt[i - 2] = cnt[i] + 1;
      }
    }

    int ret = 0;
    for (int i = 0; i < m; i++) {
      if (cnt[i] >= n) {
        ret++;
      }
    }

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

#include <iostream>
using namespace std;

int main() {
  while (1) {
    char s[1000000 + 2];
    int n, m, ans = 0, c = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    scanf("%d", &m);
    scanf("%s", &s);
    for (int i = 0; i < m; i++) {
      if ((c % 2 == 0 && s[i] == 'I') || (c % 2 == 1 && s[i] == 'O'))
        c++;
      else {
        if (c >= 2 * n + 1)
          ans += 1 - n + ((c - 1) - (c - 1) % 2) / 2;
        c = 0;
        if (s[i] == 'I')
          c = 1;
      }
    }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m, ans, k;
  string a, b, c, d = "";
  c = "IO";
  char s[1000000];
  while (cin >> n) {
    ans = 0;
    a = "I";
    for (int i = 0; i < n; i++) {
      a += "OI";
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> s[i];
    }
    for (int i = 0; i < m - 2 * n - 1; i++) {
      b = "";
      d = "";
      d += s[i];
      d += s[i + 1];
      if (d == c) {
        for (int j = i; j < 2 * n + i + 1; j++) {
          b += s[j];
        }
      }
      if (a == b) {
        ans++;
      }
    }
    cout << ans << endl;
  }
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n;
  while (cin >> n && n) {
    string t = "I";

    rep(i, n) t += "OI";

    int m;
    cin >> m;

    string s;
    cin >> s;

    vector<string> res;
    rep(i, m) {
      if (s[i] == 'I') {
        string l = "I";
        REP(j, i, m - 2) {
          if (s[j + 1] == 'O' && s[j + 2] == 'I') {
            i += 2;
            j++;
            l += "OI";
          } else
            break;
        }

        if (l == "I")
          continue;
        res.push_back(l);
      }
    }

    int ans = 0;
    rep(i, res.size()) {
      if (res[i].size() >= t.size()) {
        int cnt = res[i].size() - t.size();
        ans += cnt / 2 + 1;
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n;
  while (cin >> n && n) {
    string t = "I";

    rep(i, n) t += "OI";

    int m;
    cin >> m;

    string s;
    cin >> s;

    int ans = 0;
    rep(i, m - t.size()) {
      bool flag = true;
      rep(j, t.size()) {
        if (t[j] == s[j + i])
          continue;
        flag = false;
      }

      if (flag)
        ans++;
    }

    cout << ans << endl;
  }

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

#include "stdio.h"

char s[1111111];
int w[1111111];
int h[1000000];

int main() {
  int noww = 0;
  while (1) {
    int n;
    int m;

    {
      scanf("%d", &n);
      if (n == 0) {
        break;
      }
      scanf("%d", &m);
      scanf("%s", s + 1);
    }

    {
      s[0] = s[1];
      w[0] = 0;
      for (int i = 1; i <= m; i++) {
        if (s[i - 1] != s[i]) {
          w[i] = w[i - 1] + 1;
        } else {
          w[i] = 1;
        }
      }
    }

    {
      h[noww] = 0;
      for (int i = 1; i <= m; i++) {
        if (w[i] > n * 2 && s[i] == 'I') {
          h[noww]++;
        }
      }
      noww++;
    }
  }

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

  return 0;
}

Error: -11

Fixed submission:

#include "stdio.h"

char s[1111111];
int w[1111111];
int h[1000000];

int main() {
  int noww = 0;
  while (1) {
    int n;
    int m;

    {
      scanf("%d", &n);
      if (n == 0) {
        break;
      }
      scanf("%d", &m);
      scanf("%s", s + 1);
    }

    {
      s[0] = s[1];
      w[0] = 0;
      for (int i = 1; i <= m; i++) {
        if (s[i - 1] != s[i]) {
          w[i] = w[i - 1] + 1;
        } else {
          w[i] = 1;
        }
      }
    }

    {
      h[noww] = 0;
      for (int i = 1; i <= m; i++) {
        if (w[i] > n * 2 && s[i] == 'I') {
          h[noww]++;
        }
      }
      noww++;
    }
  }

  for (int i = 0; noww; i++) {
    printf("%d\\n", h[i]);
  }

  return 0;
}