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

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

int main() {
  while (true) {
    int n, m;
    static char str[1200000];
    vector<int> c;
    scanf("%d", &n);
    if (n == 0)
      break;
    scanf("%d", &m);
    scanf("%s", str);
    c.reserve(m);
    for (int i = 0; i < m; ++i) {
      if (str[i] != 'I')
        continue;
      int it = i + 1;
      int ioi = 0;
      while (it < m - 1) {
        if (str[it] == 'O' && str[it + 1] == 'I')
          ioi += 1, it += 2;
        else
          break;
      }
      i = it - 1;
      c.push_back(ioi);
    }
    int ans = 0;
    for (int i = 0; i < c.size(); ++i) {
      if (c[i] >= n)
        ans += c[i] - n + 1;
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

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

int main() {
  while (true) {
    int n, m;
    static char str[1200000];
    vector<int> c;
    scanf("%d", &n);
    if (n == 0)
      break;
    scanf("%d", &m);
    scanf("%s", str);
    for (int i = 0; i < m; ++i) {
      if (str[i] != 'I')
        continue;
      int it = i;
      int ioi = 0;
      while (it < m - 1) {
        if (str[i] == 'O' && str[i + 1] == 'I')
          ioi += 1, it += 2;
        else
          break;
      }
      i = it - 1;
      c.push_back(ioi);
    }
    int ans = 0;
    for (int i = 0; i < c.size(); ++i) {
      if (c[i] >= n)
        ans += c[i] - n + 1;
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: 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;
ll n, m;
string s;
ll dp[1000001];
ll ans = 0;
int main() {
  while (1) {
    memset(dp, 0, sizeof(dp));
    cin >> n;
    if (!n)
      break;
    cin >> m >> s;
    ans = 0;
    for (ll i = 0; i < m; i++) {
      if (i > 0 && s[i - 1] != s[i]) {
        dp[i] = dp[i - 1] + 1;
      } else if (s[i] == 'I')
        dp[i] = 1;

      if (s[i] == 'I' && dp[i] > 2 * n) {
        ans++;
      }
    }
    cout << ans << endl;
  }
}

Error: 0

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;
ll n, m;
string s;
ll dp[100001];
ll ans = 0;
int main() {
  while (1) {
    memset(dp, 0, sizeof(dp));
    cin >> n;
    if (!n)
      break;
    cin >> m >> s;
    ans = 0;
    for (ll i = 0; i < m; i++) {
      if (i > 0 && s[i - 1] != s[i]) {
        dp[i] = dp[i - 1] + 1;
      } else if (s[i] == 'I')
        dp[i] = 1;

      if (s[i] == 'I' && dp[i] > 2 * n) {
        ans++;
      }
    }
    cout << ans << endl;
  }
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, m, j, ans = 0;
  string s;
  while (1) {
    ans = 0;
    cin >> n;
    if (n == 0)
      break;
    cin >> m >> s;
    for (int i = 0; i < m; i++) {
      if (s[i] != 'I')
        continue;
      for (j = 0; i < m - 2; i += 2, j++) {
        if (s[i + 1] != 'O' || s[i + 2] != 'I')
          break;
        if (j >= n - 1) {
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, m, j, ii, ans = 0;
  string s;
  while (1) {
    ans = 0;
    cin >> n;
    if (n == 0)
      break;
    cin >> m >> s;
    for (int i = 0, l = s.size(), flg = 0; i < l; i++) {
      if (s[i] != 'I')
        continue;
      ii = i;
      for (j = 0; i < m - 2 && j < n; i += 2, j++) {
        if (s[i + 1] != 'O' || s[i + 2] != 'I')
          break;
      }
      if (j >= n)
        ans++;
      i = ii;
    }
    cout << ans << endl;
  }
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n;
  while (cin >> n, n) {
    long long int siz, ans = 0;
    string str;
    cin >> siz >> str;
    for (int i = 0; i < siz - 2 * n; i++) {
      if (str[i] == 'I') {
        int co = 0;
        for (int j = i + 1; j < siz; j += 2, co++) {
          if (str[j] != 'O' || str[j + 1] != 'I')
            break;
        }
        if (co >= n) {
          ans += co - n + 1;
          i += 1 + 2 * co - n;
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n;
  while (cin >> n, n) {
    long long int siz, ans = 0;
    string str;
    cin >> siz >> str;
    for (int i = 0; i < siz - 2 * n; i++) {
      if (str[i] == 'I') {
        int co = 0, j = i + 1;
        for (; j < siz && co != n; j += 2, co++) {
          if (str[j] != 'O' || str[j + 1] != 'I')
            break;
        }
        if (co == n)
          ans++;
        if (co > 0)
          i++;
      }
    }
    cout << ans << endl;
  }
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) (x).begin()(x).end()

int n, m;
string s;
string IO = "IO";

int main() {
  while (cin >> n && n) {
    cin >> m >> s;
    int cur = 0;
    string tg = "";
    rep(i, 2 * n + 1) tg += IO[cur % 2], cur++;
    // cout<<tg<<endl;
    int ans = 0;
    rep(i, m - n + 1) {
      string t = s.substr(i, 2 * n + 1);
      if (t == tg)
        ans++;
    }
    cout << ans << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

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

#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) (x).begin()(x).end()

int n, m;
string s;
string IO = "IO";

int main() {
  while (cin >> n && n) {
    cin >> m >> s;
    int cur = 0;
    string tg = "";
    rep(i, 2 * n + 1) tg += IO[cur % 2], cur++;
    cout << tg << endl;
    int ans = 0;
    rep(i, m - n + 1) {
      string t = s.substr(i, m);
      if (t == tg)
        ans++;
    }
    cout << ans << "\\n";
  }

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

#include <bits/stdc++.h>
#define LLI long long int
LLI n, m, sum, ans, pp, j;
using namespace std;
string str;

int main() {

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

        i += sum * 2;
        if (sum - n + 1 > 0)
          ans += sum - n + 1;
        sum = 0;
      }
    }

    cout << ans << endl;
    ans = 0;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string str1, str2, str3;
int n, m, ans, but, sum, pp;

int main() {

  while (1) {
    ans = 0;
    str1 = "I";
    cin >> n;
    pp = 1 + (n * 2);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      str1 += "OI";
    }

    cin >> m >> str2;

    for (int i = 0; i < m - pp; i++) {
      if (str2[i] == 'I') {
        for (int j = 1; j <= n * 2; j++) {
          sum++;
          if (str1[j] != str2[i + j]) {
            but = 1;
            break;
          }
        }
        if (but == 0)
          ans++;
        else
          i += sum - 1;
        but = 0;
        sum = 0;
      }
    }

    cout << ans << endl;
  }

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

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

int main(void) {
  int n;
  int m;
  string str;
  string cmp;
  while (1) {
    str.clear();
    cmp.clear();
    cin >> n;
    if (n == 0)
      return 0;
    cin >> m;
    cin >> str;
    cmp += "IOI";
    for (int i = 1; i < n; ++i) {
      cmp += "OI";
    }
    int count = 0;
    int next = 0;
    while (1) {
      auto a = str.find(cmp, next + 1);
      if (a == string::npos)
        break;
      else {
        next = a;
        ++count;
      }
    }
    cout << count << endl;
  }
}

Error: TLE

Fixed submission:

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

int main(void) {
  string str;
  string ionum;
  string::size_type index;
  int counter = 0;
  int n, maxnum;
  int i, j;

  while (1) {
    str.clear();
    ionum.clear();
    cin >> n;

    if (n == 0)
      return 0;

    cin >> maxnum >> str;

    ionum += 'I';
    for (i = 0; i < n; i++) {
      ionum += "OI";
    }

    for (i = 0; i < maxnum - n * 2 + 1; i++) {
      if (str.substr(i, n * 2 + 1) == ionum)
        counter++;
    }
    cout << counter << endl;
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

int main() {

  int n;
  while (std::cin >> n, n) {
    int sum, cnt = 0;
    std::cin >> sum;

    std::string s, p = "I";
    std::cin >> s;

    for (int i = 0; i < n; i++) {
      p += "OI";
    }

    for (int i = 0; i <= sum - (1 + n * 2); i++) {
      if (s.substr(i, 1 + 2 * n) == p)
        cnt++;
    }

    std::cout << cnt << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>

int main() {

  int n;
  while (std::cin >> n, n) {
    int a, cnt = 0;
    std::cin >> a;

    std::string s, p = "I";
    std::cin >> s;

    for (int i = 0; i < n; i++) {
      p += "OI";
    }

    int x = s.find(p);
    while (x != -1) {
      cnt++;
      s[x] = 'O';
      x = s.find(p);
    }

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

#include <cstring>
#include <iostream>
#include <string>

int data[1000001];

int main() {
  int n;

  while (std::cin >> n, n) {
    memset(data, 0, sizeof(data));

    int m;
    std::string str;
    std::cin >> m >> str;

    int res = 0;

    for (int i = m - 3; i >= 0; i--) {
      if (str[i] == 'I' && str[i + 1] == 'O' && str[i + 2] == 'I')
        data[i] = data[i + 2] + 1;
    }

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

    std::cout << res << std::endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>

int main() {

  int n;

  while (std::cin >> n, n) {
    int m;
    std::string ioi, str;

    std::cin >> m >> str;

    ioi = "I";
    for (int i = 0; i < n; i++) {
      ioi += "OI";
    }

    int res = 0;
    while (true) {
      int k = str.find(ioi);
      if (k == str.npos)
        break;
      else
        str = str.substr(k + 1);
      res++;
    }
    std::cout << res << std::endl;
  }

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

#include <algorithm>
#include <bitset>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (long long i = 0; i < n; i++)
#define MSG(a) cout << #a << " " << a << endl;

int main() {
  // ifstream ifs("2009-ho-data/data/2009-ho-t1-in2");
  int n;
  while (cin >> n && n) {
    int l, ans = 0;
    string s;
    vector<ll> I, O;

    cin >> l >> s;

    rep(i, l) I.push_back(
        s[i] == 'I' ? i < 2 ? s[i] == 'I' ? 1 : 0 : I[i - 2] + 1 : 0);
    rep(i, l) O.push_back(
        s[i] == 'O' ? i < 2 ? s[i] == 'O' ? 1 : 0 : O[i - 2] + 1 : 0);

    rep(i, l - 2 * n) if (O[i + 2 * n - 1] >= n && I[i + 2 * n] >= n + 1) ans++;

    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (long long i = 0; i < n; i++)
#define MSG(a) cout << #a << " " << a << endl;

int main() {
  int n;
  while (cin >> n && n) {
    int l;
    string s;
    vector<ll> v;
    cin >> l >> s;

    rep(i, l) if (1 < i && s.substr(i - 2, 3) == "IOI") v.push_back(i);

    rep(i, n * 2 - 2) {
      char c = 'O' - i % 2 * ('O' - 'I');
      for (vector<ll>::iterator it = v.begin(); it != v.end();) {
        (*it)++;
        if ((*it) >= l || s[(*it)] != c)
          it = v.erase(it);
        else
          it++;
      }
    }
    cout << v.size() << endl;
  }
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <string.h>

int c[1000001];
char s[1000001];

int main(void) {
  int i, j;
  int n, m, ans;

  while (scanf("%d", &n) && n) {
    memset(c, 0, sizeof(c));

    scanf("%d%s", &m, s);
    for (i = m - 1; i > 1; i--) {
      if (s[i] == 'I' && s[i - 1] == 'O' && s[i - 2] == 'I') {
        c[i - 2] = c[i] + 1;
      }
    }

    ans = 0;
    for (i = 0; i < m; i++) {
      if (c[i] >= n) {
        ans++;
      }
    }
    printf("%d\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

void solve() {
  int a, n;
  while (cin >> a, a) {
    cin >> n;
    string str;
    for (int i = 0; i < a; ++i) {
      str += "IO";
    }
    str += "I";
    string str2;
    cin >> str2;
    int size = n - 2 * a - 1;
    int count = 0;
    for (int i = 0; i < size; ++i) {
      if (str[0] == str2[i]) {
        if (str == str2.substr(i, 2 * a + 1)) {
          count++;
        }
      }
    }
    cout << count << endl;
  }
}

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

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main() {
  int n, len;
  while (cin >> n, n) {
    string s;
    cin >> len >> s;
    int ans = 0, count = 0;
    rep(i, len - 2) {
      if (s[i] == 'I') {
        if (s[i + 1] == 'O' && s[i + 2] == 'I') {
          count++;
        } else {
          count = 0;
        }
        if (count >= n)
          ans++;
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main() {
  int n, len, ans;
  string s;
  while (scanf("%d", &n), n) {
    string str;
    ans = 0;
    ;
    str += 'I';
    scanf("%d", &len);
    cin >> s;
    rep(i, n) { str += "OI"; }
    rep(i, len - str.size() + 1) {
      bool check = true;
      rep(j, str.size()) {
        if (s[i + j] != str[j]) {
          check = false;
          break;
        }
      }
      if (check)
        ans++;
    }
    cout << ans << endl;
  }
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, m;
  char s;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    cin >> m;
    bool cut = true;
    int start = 0, count = 0, ans = 0;
    for (int i = 0; i < m; i++) {
      cin >> s;
      if (cut == true) {
        if (s == 'I') {
          start = i;
          count = 1;
          cut = false;
        }
      } else {
        if (((i - start) % 2 == 1 && s != 'O') ||
            ((i - start) % 2 == 0 && s != 'I')) {
          cut = true;
          ans += max(0, count - n);
          if (s == 'I') {
            start = i;
            count = 0;
            cut = false;
          }
        }
        if ((i - start) % 2 == 0 && s == 'I')
          count++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
char s[1000001];
int main() {
  int n, m, count;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    cin >> m;
    count = 0;
    for (int i = 0; i < m; i++)
      cin >> s[i];
    for (int i = 0; i <= m - 2 * n; i++) {
      bool t = true;
      if (s[i] != 'I')
        t = false;
      for (int j = 0; j < n; j++) {
        if (s[j * 2 + 1 + i] != 'O')
          t = false;
        if (s[j * 2 + 2 + i] != 'I')
          t = false;
      }
      if (t == true)
        count++;
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int n, m;
  string t = "IO";
  while (cin >> n, n) {
    cin >> m;
    int ret = 0;

    int w = 0;
    bool f = 0;
    rep(i, m) {
      char c;
      cin >> c;
      if (t[f] == c)
        w++, f ^= 1;
      else if (c == 'I')
        w = 1, f = 1;
      else if (c == 'O')
        w = 0, f = 0;

      if (w >= n * 2 + 1) {
        if (w % 2)
          ret++;
      }
    }

    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, m;
  string s, t2;
  while (cin >> n >> m >> s && n) {
    string t = "I";
    for (int i = 0; i < n; i++)
      t += "OI";
    int r = 0;
    for (int i = 0; i < m - n * 2 + 1; i++) {
      bool f = 1;
      t2.clear();
      for (int j = 0; j < n * 2 + 1; j++) {
        t2 += s[i + j];
      }
      if (t2 == t)
        r++;
    }
    cout << r << endl;
  }
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  string p = "IOI", str, oi = "OI";
  int count, n, m, z, j;

  while (cin >> n, n) {

    count = j = z = 0;
    p = "IOI";

    for (int i = 1; i < n; i++)
      p += oi;

    z = p.size();

    cin >> m;
    cin >> str;

    for (int i = 0; i < str.size() - z + 1; i++) {
      if (p == str.substr(j, i + z - j))
        count++;
      j = i + 1;
    }

    cout << count << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {

  string p = "I", str, oi = "OI";
  int count, n, m, z, j;

  while (cin >> n, n) {

    count = j = z = 0;
    p = "I";

    for (int i = 0; i < n; i++)
      p += oi;

    z = p.size();

    cin >> m;
    cin >> str;

    for (int i = 0; i < str.size() - z + 1; i++) {
      if (p == str.substr(j, i + z - j))
        count++;
      cout << str.substr(j, i + z - j) << endl;
      j = i + 1;
    }

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  while (true) {
    cin >> n;
    if (n == 0)
      return 0;
    cin >> m;
    string s;
    cin >> s;
    long long ans = 0;
    for (int i = 0; i < m; i++) {
      if (s[i] == 'I') {
        int cnt = 0;
        while (i + 2 < m && s[i + 1] == 'O' && s[i + 2] == 'I')
          i += 2, cnt++;
        ans += max(0, cnt - n + 1);
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  while (true) {
    cin >> n;
    if (n == 0)
      return 0;
    unsigned int m;
    cin >> m;
    string s;
    cin >> s;
    string t = "IOI";
    for (int i = 1; i < n; i++)
      t += "OI";
    int ans = 1;
    unsigned int now = s.find(t, 0);
    if (now == string::npos) {
      cout << 0 << endl;
      continue;
    }
    unsigned int end = now + (unsigned int)t.size();
    while (end < m) {
      if (s[end] == 'O' && s[end + 1] == 'I') {
        now += 2;
        end += 2;
      } else {
        now = s.find(t, end);
        if (now == string::npos)
          break;
        end = now + (unsigned int)t.size();
      }
      ans++;
    }
    cout << ans << endl;
  }
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    n = n * 2 + 1;
    int m;
    cin >> m;

    char bfr = ' ';
    int length = 0;
    int cnt = 0;
    for (int i = 0; i < m; i++) {
      char c;
      cin >> c;
      if (c != bfr) {
        length++;
      } else {
        length = 0;
      }
      if (length == 0) {
        if (c == 'I') {
          length = 1;
        }
      }
      if (c == 'I' && length >= n)
        cnt++;

      bfr = c;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    string io = "I";
    for (int i = 0; i < n; i++) {
      io += "OI";
    }
    int dummy;
    cin >> dummy;
    string str;
    cin >> str;

    int cnt = 0;
    for (int i = 0; i < str.length(); i++) {
      if (io == str.substr(i, io.length()))
        cnt++;
    }

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

#include <bits/stdc++.h>
using namespace std;
int ans, s1, s2, n, a;
int solve(int tl);
string text;

int main() {

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cin >> a >> text;
    s1 = text.size();
    ans = 0;

    while (s1 >= 2 * n + 1) {
      // cout <<" " << text.size()-s1 << endl;
      if (text[text.size() - s1] == 'I')
        ans += solve(text.size() - s1);
      s1--;
    }
    cout << ans << endl;
    ans = 0;
  }

  return 0;
}

int solve(int tl) {
  int i = 1;
  int ccc = 0;
  for (i = 1; i < 2 * n + 1; i += 2) {
    if (text[tl + i] == 'O' && text[tl + i + 1] == 'I')
      ;
    else
      return 0;
  }
  ccc++;
  while (i < s1) {
    if (text[tl + i] == 'O' && text[tl + i + 1] == 'I') {
      i += 2;
      s1 -= 2;
      ccc++;
    } else
      break;
  }
  return ccc;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int ans, s1, s2, n, a;
int solve(int tt, int pp);
string text, patn, sample = "OI";

int main() {

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cin >> a >> text;
    patn = "I";
    for (int i = 0; i < n; i++)
      patn += sample;
    // cout << patn << endl;
    s1 = text.size();
    s2 = patn.size();
    ans = 0;
    while (s1 >= s2) {
      // cout <<" " << text.size()-s1 << endl;
      if (text[text.size() - s1] == patn[0])
        ans += solve(text.size() - s1, s2);
      if (s1 != 0)
        s1--;
    }
    cout << ans << endl;
    ans = 0;
  }
  return 0;
}

int solve(int tl, int pl) {
  int i = 1;
  for (i = 1; i < pl; i += 2) {
    if (text[tl + i] == patn[i] && text[tl + i + 1] == patn[i + 1])
      ;
    else
      return 0;
  }
  if (i >= pl)
    return 1;
  else
    return 0;
}