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

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

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;
static const int INF = (int)1e8;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOREQ(i, k, n) for (int i = (k); i <= (int)(n); ++i)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FIND(m, w) ((m).find(w) != (m).end())

int main(void) {
  int m, n;
  while (cin >> m >> n, m) {
    vector<string> word(n);
    REP(i, n) { cin >> word[i]; }
    vector<int> member(m);
    REP(i, m) member[i] = i + 1;
    int idx = 0;
    REP(i, n) {
      if (member.size() == 1)
        break;

      int number = i + 1;
      string answord;
      if (number % 15 == 0) {
        answord = "FizzBuzz";
      } else if (number % 5 == 0) {
        answord = "Buzz";
      } else if (number % 3 == 0) {
        answord = "Fizz";
      } else {
        stringstream ss;
        ss << number;
        answord = ss.str();
      }

      if (answord == word[i]) {
        idx = (idx + 1) % member.size();
      } else {
        member.erase(member.begin() + idx);
        if (idx == member.size())
          idx = 0;
      }
    }

    REP(i, member.size()) {
      if (i)
        cout << " ";
      cout << member[i];
    }
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string IntToString(int number) {
  stringstream ss;
  ss << number;
  return ss.str();
}
int keta(int n) {
  int count = 0;
  while (n != 0) {
    count++;
    n /= 10;
  }
  return count;
}
int main(void) {
  int m, n;
  bool exist[10000];
  while (cin >> m >> n) {
    if (!m && !n)
      break;
    cin.ignore();
    for (int i = 0; i < m; i++) {
      exist[i] = true;
    }
    int count = 1;
    int member = 0;
    for (int i = 0; i < n; i++) {
      string say;
      getline(cin, say);
      if ((say == IntToString(count) && count % 3 != 0 && count % 5 != 0) ||
          (say == "Fizz" && count % 3 == 0 && count % 5 != 0) ||
          (say == "Buzz" && count % 5 == 0 && count % 3 != 0) ||
          (say == "FizzBuzz" && count % 3 == 0 && count % 5 == 0)) {
        count++;
        // cout<<"O"<<endl;
      } else {
        count++;
        int n = atoi(say.c_str());
        if (n != 0 && keta(n) == say.size()) {
          count = n + 1;
        }
        exist[member] = false;
        // cout<<"X"<<endl;
      }
      do {
        member = (member + 1) % m;
      } while (!exist[member]);
    }
    bool first = true;
    for (int i = 0; i < m; i++) {
      if (exist[i]) {
        if (first) {
          printf("%d", i + 1);
          first = false;
        } else {
          printf(" %d", i + 1);
        }
      }
    }
    cout << endl;
  }
  return 0;
}