Problem p00461 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>

using namespace std;

int cont;
int kaisu[1000000] = {0};

int main() {

  for (;;) {
    string str;
    int n;
    int m;
    bool flg = true;
    int cont = 0;

    char temp = -1;

    cin >> n;

    if (n == 0) {
      break;
    }

    cin >> m;
    cin >> str;

    for (int i = 0; i < m; i++) {
      kaisu[i] = 0;
    }

    // ある位置から何回IOIを繰り返せるかを調べる
    /*
    for(int i = 0; i < m; i++){
            if(str[i] == 'I')
            {
                    for(int j = i;; j++){
                            if(str[j] == temp && temp != -1){
                                    break;
                            }
                            if(str[j] == 'O'){
                                    flg = true;
                            }
                            if(flg == true && str[j] == 'I' && temp != -1){
                                    flg = false;
                                    cont++;
                            }
                            temp = str[j];
//				printf("%d文字目:今の文字:%c
続いている回数:%d\n",j,str[j], cont);
//				getchar();
                    }
                    kaisu[i] = cont;
                    flg = false;
                    cont = 0;
                    temp = -1;
            }
    }
    */

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

    cont = 0;
    for (int i = 0; i < m; i++) {
      cout << kaisu[i] << endl;
      if (kaisu[i] >= n) {
        cont++;
      }
    }

    cout << cont << endl;
  }
}

Fixed submission:
#include <iostream>
#include <string>

using namespace std;

int cont;
int kaisu[1000000] = {0};

int main() {

  for (;;) {
    string str;
    int n;
    int m;
    bool flg = true;
    int cont = 0;

    char temp = -1;

    cin >> n;

    if (n == 0) {
      break;
    }

    cin >> m;
    cin >> str;

    for (int i = 0; i < m; i++) {
      kaisu[i] = 0;
    }

    // ある位置から何回IOIを繰り返せるかを調べる
    /*
    for(int i = 0; i < m; i++){
            if(str[i] == 'I')
            {
                    for(int j = i;; j++){
                            if(str[j] == temp && temp != -1){
                                    break;
                            }
                            if(str[j] == 'O'){
                                    flg = true;
                            }
                            if(flg == true && str[j] == 'I' && temp != -1){
                                    flg = false;
                                    cont++;
                            }
                            temp = str[j];
//				printf("%d文字目:今の文字:%c
続いている回数:%d\n",j,str[j], cont);
//				getchar();
                    }
                    kaisu[i] = cont;
                    flg = false;
                    cont = 0;
                    temp = -1;
            }
    }
    */

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

    cont = 0;
    for (int i = 0; i < m; i++) {
      //			cout << kaisu[i] << endl;
      if (kaisu[i] >= n) {
        cont++;
      }
    }

    cout << cont << endl;
  }
}