Problem p00221 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair<INT, INT> II;
typedef vector<II> VII;

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  for (typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
    os << *it_i << ", ";
  }
  return os;
}

const string FIZZBUZZ = "FizzBuzz";
const string FIZZ = "Fizz";
const string BUZZ = "Buzz";
bool used[1001];
int cur;
int remains;
int m, n;

void init() {
  fill(used, used + 1001, false);
  remains = m;
  cur = 0;
}

VI get() {
  VI res;
  for (int i = 0; i < m; ++i) {
    if (!used[i])
      res.push_back(i);
  }
  return res;
}

void next() {
  do {
    cur = (cur + 1) % m;
  } while (used[cur]);
}

bool check(int real, string s) {
  if (real % 15 == 0) {
    if (s != FIZZBUZZ)
      return false;
  } else if (real % 5 == 0) {
    if (s != BUZZ)
      return false;
  } else if (real % 3 == 0) {
    if (s != FIZZ)
      return false;
  } else {
    OSS oss;
    oss << real;
    if (s != oss.str())
      return false;
  }
  return true;
}

void output() {
  VI v = get();
  for (VI::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
    cout << (*it_i + 1);
    if (it_i + 1 != v.end())
      cout << " ";
  }
  cout << endl;
}

int main() {
  while (cin >> m >> n && m) {
    init();
    for (int i = 0; i < n; ++i) {
      int real = i + 1;
      string s;
      cin >> s;
      if (remains == 1)
        continue;
      if (!check(real, s)) {
        used[cur] = true;
        remains--;
      }
      next();
    }
    output();
  }
  return 0;
}

Error: TLE

Fixed submission:

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

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

  while (cin >> m >> n && m && n) {
    vector<int> R(m, 0);
    int curIndex = 0;
    int curNumber = 1;
    for (int i = 0; i < n; i++) {
      cin >> s;

      if (s == "FizzBuzz" && ((curNumber % 3 != 0) || (curNumber % 5 != 0))) {
        R[curIndex] = 1;
      } else if (s == "Fizz" && (curNumber % 3 != 0)) {
        R[curIndex] = 1;
      } else if (s == "Buzz" && (curNumber % 5 != 0)) {
        R[curIndex] = 1;
      } else {
        int number;
        istringstream is(s);
        is >> number;

        if (curNumber % 3 == 0 && curNumber % 5 == 0 && s == "FizzBuzz") {
          R[curIndex] = 0;
        } else if (curNumber % 3 == 0 && s == "Fizz") {
          R[curIndex] = 0;
        } else if (curNumber % 5 == 0 && s == "Buzz") {
          R[curIndex] = 0;
        } else if (number % 3 == 0 && number % 5 == 0 && s != "FizzBuzz") {
          R[curIndex] = 1;
        } else if (number % 3 == 0 && s != "Fizz") {
          R[curIndex] = 1;
        } else if (number % 5 == 0 && s != "Buzz") {
          R[curIndex] = 1;
        } else if (number != curNumber) {
          R[curIndex] = 1;
        }
      }

      do {
        curIndex = (curIndex + 1) % m;
      } while (R[curIndex] == 1);
      curNumber++;
    }

    vector<int> L;
    for (int i = 0; i < n; i++) {
      if (R[i] == 0)
        L.push_back(i + 1);
    }

    sort(L.begin(), L.end());
    for (vector<int>::iterator it = L.begin(); it != L.end(); it++) {
      cout << *it;
      if (it + 1 != L.end())
        cout << " ";
      else
        cout << endl;
    }
  }

  return 0;
}