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

#include <iostream>
#include <string>

using namespace std;

int main() {
  int n, m, cnt, res;
  string str;

  while (cin >> n && n) {
    cin >> m;
    cin >> str;

    res = 0;
    for (int i = 0; i < m; ++i)
      if (str[i] == 'I') {
        ++i;
        cnt = 0;
        while (i + 1 < m && str[i] == 'O' && str[i + 1] == 'I') {
          ++cnt;
          i += 2;
        }
        if (cnt >= n)
          res += (cnt - n + 1);
        if (cnt)
          i -= 3;
        else
          --i;
      }

    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

int main() {
  int n, m, c;
  string s1, s2;

  while (cin >> n && n) {
    cin >> m >> s2;
    s1 = "I";
    for (int i = 0; i < n; i++) {
      s1 += "OI";
    }
    n = 2 * n + 1;
    c = 0;
    for (int i = 0; i <= m - n; i++) {
      if (s1 == s2.substr(i, n)) {
        c++;
      }
    }

    cout << c << endl;
  }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
char str[1000011] = {0};
int dp[1000011] = {0};
using namespace std;
int main() {
  int n;
  while (scanf("%d", &n), n) {
    memset(str, 0, sizeof(str));
    int m;
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
      scanf(" %c", str + i + 1);
    }
    int ans = 0;
    memset(dp, 0, sizeof(dp));
    for (int i = 1; i <= m; i++) {
      if (str[i - 1] == 'I' && str[i] == 'O' && str[i + 1] == 'I') {
        dp[i] = dp[i - 2] + 1;
        if (dp[i] >= n)
          ans++;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
using namespace std;
char str[1000011] = {0};
int main() {
  int n;
  while (scanf("%d", &n), n) {
    memset(str, 0, sizeof(str));
    int m;
    scanf("%d", &m);
    short f = 0;
    int cnt = 0;
    int ans = 0;
    scanf("%s", str);
    for (int q = 0; q < m; q++) {
      if (str[q] == 'O')
        continue;
      for (int i = q + 1; i < n * 2 + q; i += 2) {
        if (str[i] == 'I') {
          cnt = 0;
          break;
        } else
          cnt++;
        if (cnt == n && str[i + 1] == 'I') {
          ans++;
          cnt = 0;
          break;
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <memory.h>
#include <string>
using namespace std;
int main() {
  int n, m, ans, c[1000001];
  string s;
  while (cin >> n >> m >> s, n) {
    memset(c, 0, sizeof(c));
    for (int i = m - 3; i >= 0; i--) {
      if (s.substr(i, 3) == "IOI")
        c[i] = c[i + 2] + 1;
    }
    ans = 0;
    for (int i = 0; i < m; i++)
      if (c[i] >= n)
        ans++;
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, m, ans;
  string s;
  while (cin >> n >> m >> s, n) {
    string p = "I";
    for (int i = 0; i < n; i++)
      p += "OI";
    ans = 0;
    int i = 0;
    while ((i = s.find(p, i) + 1) != string::npos + 1)
      ans++;
    cout << ans << endl;
  }
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

using namespace std;

void solve() {
  int n;
  while (cin >> n, n) {
    string tmpl;
    for (int i = 0; i < n; ++i) {
      tmpl += "IO";
    }
    tmpl += "I";
    int m;
    cin >> m;
    string s;
    cin >> s;
    int ans = 0;
    for (int i = 0; i < m; ++i) {
      if (s[i] == 'I' && i + tmpl.size() < m) {
        int same_num = 0;
        bool flag = true;
        for (int j = 1; j < tmpl.size(); ++j) {
          if (s[i + j] != tmpl[j]) {
            flag = false;
            break;
          }
          ++same_num;
        }
        if (flag) {
          ++ans;

        } else {
          i += same_num;
        }
      }
    }
    cout << ans << endl;
  }
}

int main() {
  solve();
  return (0);
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

void solve() {
  int n;
  while (cin >> n, n) {
    string tmpl;
    for (int i = 0; i < n; ++i) {
      tmpl += "IO";
    }
    tmpl += "I";
    int m;
    cin >> m;
    string s;
    cin >> s;
    int ans = 0;
    for (int i = 0; i < m; ++i) {
      if (s[i] == 'I' && i + tmpl.size() < m) {
        bool flag = true;
        for (int j = 1; j < tmpl.size(); ++j) {
          if (s[i + j] != tmpl[j]) {
            flag = false;
            break;
          }
        }
        if (flag) {
          ++ans;
        }
      }
    }
    cout << ans << endl;
  }
}

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

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, k, size, ans = 0;
  while (true) {
    int size = 0, ans = 0;
    string s;
    cin >> n;
    if (n == 0)
      return 0;
    cin >> k;
    cin >> s;
    for (int i = 0; i < k; i++) {
      if (s[i] == 'I') {
        size = 0;
        for (int j = 1; j < k; j += 2) {
          if (s[i + j] == 'O' && s[i + j + 1] == 'I') {
            size++;
          } else {
            break;
          }
        }
        if (size >= n) {
          ans += 1 + size - n;
        }
        i += size * 2;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
int main() {
  int i, j;
  int n;
  int ln, ct;
  string s;
  while (cin >> n && n) {
    cin >> ln >> s;
    ct = 0;
    for (i = 0; i < ln - 2; i = j + 1) {
      for (; i < ln - 2 && s[i] != 'I'; i++)
        ;
      for (j = i; j < ln - 2 && s[j + 1] == 'O' && s[j + 2] == 'I'; j += 2)
        ;
      if (j - i + 1 >= n * 2 + 1)
        ct += (j - i) / 2 - n + 1;
    }
    cout << ct << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
int main() {
  int i;
  int n;
  int ln, ct;
  string s, t;
  while (cin >> n && n) {
    cin >> ln >> s;
    t = "I";
    for (i = 0; i < n; i++)
      t += "OI";
    ct = 0;
    for (i = 0; (i = s.find(t, i)) != (int)string::npos; i++)
      ct++;
    cout << ct << endl;
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int N, M;
string s;
int main() {
  while (1) {
    cin >> N;
    if (!N)
      break;
    cin >> M;
    cin >> s;
    int co = 0;
    int res = 0;
    for (int i = 0; i < (int)s.size(); i++) {
      if (s[i] == 'I' && s[i + 1] == 'O' && s[i + 2] == 'I') {
        co++;
        i++;
      } else {
        res += max(0, (co - N + 1));
        co = 0;
      }
    }
    res += max(0, (co - N + 1));
    cout << res << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int N, M;
string s;
int main() {
  while (1) {
    cin >> N;
    if (!N)
      break;
    cin >> M;
    cin >> s;
    int co = 0;
    bool fs = false;
    int res = 0;
    for (int i = 0; i < (int)s.size(); i++) {

      //     cout << i <<" "<<co <<" fs:" << fs << "\\n";
      if (!fs && i + 1 >= (int)s.size())
        break;
      if (s[i] == 'I' && s[i + 1] == 'O' && !fs) {
        fs = true;
        co = 0;
        i++;
      } else if (s[i] == 'I' && s[i - 1] == 'O' && fs) {
        co++;
        i++;
      } else if (fs) {
        fs = false;
        res += (co - N + 1);
        i -= 3;
        co = 0;
        //	cout << "deleted" << endl;
      }
    }
    if (fs) {
      fs = false;
      res += (co - N + 1);
      co = 0;
      //      cout << "deleted" << endl;
    }
    cout << res << "\\n";
  }
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(void) {
  int n;
  while (scanf("%d", &n) && n != 0) {
    char temp;
    int m;
    int ans = 0;
    int d = -1;

    //		string iois = "I";
    //		string str = "";
    //		char iois[2000005] = {"I"};
    //		char str[2000000] = {0};

    /*
    for(int i = 0; i < n; i++){
            iois += "OI";
    }
    */
    /*
    for(int i = 0; i < n; i++){
            iois[1+i*2] = 'O';
            iois[2+i*2] = 'I';
    }
    iois[n*2+1] = '\\0';
    */

    scanf("%d", &m);
    getchar();
    bool flg = false;

    for (int i = 0; i < m; i++) {
      scanf("%c", &temp);
      if (d != -1 && temp == 'I' && flg) {
        d++;
        flg = false;
        if (n <= d)
          ans++;
      } else if (d != -1 && temp == 'O' && !flg) {
        flg = true;
      } else if (temp == 'I') {
        d = 0;
        flg = false;
      } else {
        d = -1;
        flg = false;
      }
    }

    /*
    for(int i = 0; i < m; i++){
            scanf("%c" ,str);
    }
    */
    /*
    cin >> str;
    for(int i = 0; i < m-n*2; i++){
            if(str.substr(i,n*2+1) == iois)ans++;
    }
    */
    /*
    getchar();
    for(int i = 0; i < m; i++){
            scanf("%c", &temp);
            str += temp;
            if(str.size() > n*2+1){
                    str = str.substr(1);
            }
            if(str == iois){
                    ans++;
            }
    }
    */
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(void) {
  int n;
  while (scanf("%d", &n) && n != 0) {
    string iois = "I";
    char temp;
    int m;
    int ans = 0;
    string str = "";

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

    scanf("%d", &m);

    cin >> str;

    for (int i = 0; i < m - n * 2; i++) {
      if (str.substr(i, n * 2 + 1) == iois)
        ans++;
    }

    /*
    getchar();
    for(int i = 0; i < m; i++){
            scanf("%c", &temp);
            str += temp;
            if(str.size() > n*2+1){
                    str = str.substr(1);
            }
            if(str == iois){
                    ans++;
            }
    }
    */
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    int m;
    cin >> m;
    string s;
    cin >> s;
    string tmp = "I";
    rep(i, n) tmp += "OI";
    int co = 0;
    rep(i, s.size() - 2 * n) if (s.substr(i, 2 * n + 1) == tmp) co++;
    cout << co << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;
int main() {

  string in;
  int n, m;
  while (cin >> n, n) {
    cin >> m;
    cin >> in;
    short int co = 0;
    rep(i, m - n * 2) {
      bool han = true;
      if (!in[i] == 'I')
        continue;
      else {
        rep(j, n) {
          if (in[i + 2 * j - 1] != 'I') {
            han = false;
            break;
          }
          if (in[i + 2 * j] != 'O') {
            han = false;
            break;
          }
        }
        if (han == true)
          co++;
      }
    }
    cout << co << endl;
  }
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n;
  while (scanf("%d", &n), n) {
    int m;
    scanf("%d", &m);
    string s;
    cin >> s;
    int ans = 0;
    char want = 'I';
    int cnt = 0;
    rep(i, m) {
      if (s[i] == want) {
        cnt++;
        if (s[i] == 'I')
          want = 'O';
        else
          want = 'I';
      } else {
        if (cnt > 2 * n)
          ans += (cnt - 2 * n + 1) / 2;
        want = 'I';
        cnt = 0;
        if (s[i] == 'I')
          i--;
      }
    }
    if (cnt > 2 * n)
      ans += (cnt - 2 * n + 1) / 2;
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

char s[1000001];
int main() {
  int n;
  while (scanf("%d", &n), n) {
    int m;
    scanf("%d", &m);
    if (1 + 2 * n > m) {
      puts("0");
      return 0;
    }
    scanf("%s", s);
    string t = "I";
    rep(i, n) t += "OI";
    int cnt = 0;
    rep(i, m - t.size() + 1) {
      bool flag = true;
      rep(j, t.size()) {
        if (s[i + j] != t[j]) {
          flag = false;
          break;
        }
      }
      if (flag)
        cnt++;
    }
    printf("%d\\n", cnt);
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define vi vector<int>
#define VS vector<string>
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back

using namespace std;

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

    string ioi = "I";
    for (int i = 0; i < n; i++)
      ioi += "OI";
    string s;
    cin >> s;

    int count = 0;
    for (int i = 0; i < s.size() - ioi.size(); i++) {
      if (s.substr(i, ioi.size()) == ioi)
        count++;
    }

    cout << count << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define vi vector<int>
#define VS vector<string>
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back

using namespace std;

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

    string ioi = "I";
    for (int i = 0; i < n; i++)
      ioi += "OI";
    string s;
    cin >> s;

    int count = 0;
    while (s.find(ioi) != string::npos) {
      count++;

      s = s.substr(s.find(ioi) + 1);
    }
    cout << count << endl;
  }
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define str to_string
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()

#define MAX_M 1000001

int n, m;
char s[MAX_M];

// O(m)
int solve() {
  if (2 * n + 1 >= m)
    return 0;
  // p: (IO*n + I)
  // count p in s
  int count = 0;
  int cont_a = 0;
  int cont_b = 0;
  for (int i = 0; i < m; i++) {
    char c = s[i];
    // a
    if (i % 2 == 0 && c == 'I')
      cont_a++;
    else if (i % 2 != 0 && c == 'O')
      ; // ok
    else {
      if (cont_a >= n + 1) {
        count += cont_a - n;
      }
      cont_a = 0;
    }
  }

  for (int i = 1; i < m; i++) {
    char c = s[i];
    // b
    if (i % 2 == 0 && c == 'O')
      ;
    else if (i % 2 != 0 && c == 'I')
      cont_b++;
    else {
      if (cont_b >= n + 1) {
        count += cont_b - n;
      }
      cont_b = 0;
    }
  }
  return count;
}

int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    cin >> m;
    cin >> s;
    cout << solve() << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define str to_string
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()

#define MAX_M 1000001

int n, m;
char s[MAX_M];

// O(n)
bool eval(int offset) {
  rep(j, 2 * n + 1) {
    if (offset + j >= m)
      return false;
    char c = s[offset + j];
    if (j % 2 == 0) {
      if (c != 'I')
        return false;
    } else {
      if (c != 'O')
        return false;
    }
  }
  return true;
}

// O(nm)
int solve() {
  // p: (IO*n + I)
  // count p in s
  int count = 0;
  rep(i, m) {
    if (eval(i))
      count++;
  }
  return count;
}

int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    cin >> m;
    cin >> s;
    cout << solve() << "\\n";
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, m, ans;
  char c[3] = "IO";
  string s;
  while (1) {
    cin >> n;
    if (!n)
      break;
    cin >> m >> s;
    ans = 0;
    for (int i = 0, p = 0; i < m; ++i) {
      if (c[p % 2] == s[i]) {
        p++;
        if (p % 2 && p >= 2 * n + 1) {
          ans++;
        }
      } else
        p = (c[0] == s[i] ? 1 : 0);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m, ans;
  unsigned int idx;
  string s, p;
  while (1) {
    cin >> n;
    if (!n)
      break;
    cin >> m >> s;
    p.clear();
    for (int i = 0; i < n; ++i)
      p += "IO";
    p += "I";
    ans = 0;
    idx = -1;
    while (1) {
      idx = s.find(p, idx + 1);
      if (idx == string::npos)
        break;
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main() {
  int n, m;
  string str;

  while (cin >> n, n) {
    cin >> m >> str;

    int cnt = 0;
    int IO[m];
    memset(IO, 0, sizeof(IO));

    for (int i = 2; i < m; i++) {
      if (str[i - 2] == 'I' && str[i - 1] == 'O' && str[i] == 'I') {
        IO[i] += IO[i - 2] + 1;
      }
    }

    for (int i = 2; i < m; i++) {
      if (IO[i] >= n)
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, m;
  string s;

  while (cin >> n, n) {
    cin >> m >> s;

    int cnt = 0;
    string str = "", tmp;

    int len = n * 2 + 1;

    for (int i = 0; i < n; i++) {
      str += "IO";
    }
    str += 'I';

    for (int i = 0; i < m; i++) {
      if (s[i] != 'I' || s[i + n * 2] != 'I')
        continue;

      else {
        tmp.assign(s, i, len);
        if (strcmp(str.c_str(), tmp.c_str()) == 0)
          cnt++;
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00461 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<PP, bitset<8100>> Pv;

static const double EPS = 1e-8;

int tx[] = {-1, 0, 1, 0};
int ty[] = {0, 1, 0, -1};

char buf[1000000];

int main() {
  int m, n;

  bitset<1000000> mask;

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

    deque<char> que;
    int tmp = 0;
    int res = 0;
    int state = -1;
    for (int i = 0; i < m; i++) {
      que.push_back(buf[i]);

      if (que.size() == 3) {
        char f = que[0];
        char m = que[1];
        char r = que[2];

        if (f == 'I' && m == 'O' && r == 'I') {
          tmp++;
          if (tmp >= n)
            res++;
          que.pop_front();
          que.pop_front();
        } else {
          tmp = 0;
          que.pop_front();
        }
      }
    }
    printf("%d\\n", res);
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<PP, bitset<8100>> Pv;

static const double EPS = 1e-8;

int tx[] = {-1, 0, 1, 0};
int ty[] = {0, 1, 0, -1};

int main() {
  int m, n;
  cin.tie(0);
  ios::sync_with_stdio(false);

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

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

    int res = 0;
    for (int i = 0; i + expr.size() <= str.size(); i++) {
      if (str.substr(i, expr.size()) == expr) {
        res++;
      }
    }

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

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

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

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

int main() {

  int n, len;
  string s;

  while (cin >> n, n) {
    cin >> len >> s;

    int ans = 0;
    for (int i = 0; i < len; i++) {
      if (s[i] == 'I') {
        int cnt = 0;
        for (; i < len; i += 2) {
          if (s[i + 1] == 'O' && s[i + 2] == 'I')
            cnt++;
          else
            break;
        }
        ans += max(0, cnt - n + 1);
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {

  int n, len;
  string s;
  while (cin >> n, n) {
    int cnt = 0, j;
    string cmp = "I";
    cin >> len >> s;

    for (int i = 0; i < n; i++)
      cmp += "OI";
    for (int i = 0; i < len - (n * 2); i += max(1, j - 2)) {
      // cout << i << endl;
      int j;
      for (j = 0; j < n * 2 + 1; j++) {
        if (s[i + j] != cmp[j]) {
          break;
        }
      }
      if (j == n * 2 + 1)
        cnt++;
    }
    cout << cnt << endl;
  }
}