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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define INF 1e+8
#define EPS 1e-8
#define PB push_back
#define fi first
#define se second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
typedef pair<int, int> Pii;
typedef vector<int> vi;
bool check(int a, string str) {
  if (a % 15 == 0)
    return str == "FizzBuzz";
  if (a % 5 == 0)
    return str == "Buzz";
  if (a % 3 == 0)
    return str == "Fizz";
  return a == atoi(str.c_str());
}

int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    set<int> player;

    rep(i, n) { player.insert(i); }

    set<int>::iterator itr = player.begin();

    rep(i, m) {
      string str;
      int len;
      do {
        cin >> str;
        len = str.size();
        if (str[len - 1] == '\\n') {
          str[len - 1] = '\\0';
          --len;
        }
      } while (len == 0);
      if (player.size() > 1) {
        if (check(i + 1, str)) {
          ++itr;
        } else {
          player.erase(itr++);
        }
        if (itr == player.end()) {
          itr = player.begin();
        }
      }
    }

    int roop = 0;
    for (itr = player.begin(); itr != player.end(); ++itr, ++roop) {
      printf("%d%c", *itr + 1, roop + 1 == player.size() ? '\\n' : ' ');
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
#define INF 1e+8
#define EPS 1e-10
#define PB push_back
#define fi first
#define se second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
typedef pair<int, int> Pii;
typedef vector<int> vi;
int main() {
  int m, n;
  while (scanf("%d%d", &m, &n), (m | n)) {
    bool flg[10001] = {false};
    int member = 0;
    int cnt = 0;
    rep(i, n) {
      int ori = i + 1;
      int act;
      string str;
      cin >> str;
      int b = 0;
      while (true) {
        b++;
        if (flg[member] == 1) {
          if (b > m) {
            printf("%d\\n", m);
            goto skip;
          }
          if (++member == m)
            member = 0;
          continue;
        }
        break;
      }
      if (str == "Fizz") {
        act = -1;
      } else if (str == "Buzz") {
        act = -2;
      } else if (str == "FizzBuzz") {
        act = -3;
      } else {
        act = atoi(str.c_str());
      }

      if (ori % 15 == 0 && act != -3) {
        flg[member] = true;
        cnt++;
      } else if (ori % 5 == 0 && act != -2) {
        flg[member] = true;
        cnt++;
      } else if (ori % 3 == 0 && act != -1) {
        flg[member] = true;
        cnt++;
      }
      if (ori % 15 != 0 && act == -3) {
        flg[member] = true;
        cnt++;
      } else if (ori % 5 != 0 && act == -2) {
        flg[member] = true;
        cnt++;
      } else if (ori % 3 != 0 && act == -1) {
        flg[member] = true;
        cnt++;
      }
      if (act != ori && act > 0) {
        flg[member] = true;
        cnt++;
      }
      if (++member == m)
        member = 0;
    }
    cnt = m - cnt;
    rep(i, m) {
      if (!flg[i]) {
        if (--cnt == 0) {
          printf("%d\\n", i + 1);
        } else {
          printf("%d ", i + 1);
        }
      }
    }
  skip:;
  }
  return 0;
}