Problem p00221 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef vector<xy_t> polygon;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 29;
const double EPS = 1e-10;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define repD(i, n) for (int i = n; i >= 0; i--)

bool fail[1000];
int nxt[1000];

bool check(string s, int n) {
  if (n % 3 == 0 && n % 5 == 0)
    return s == "FizzBuzz";
  if (n % 3 == 0)
    return s == "Fizz";
  if (n % 5 == 0)
    return s == "Buzz";
  if (!isdigit(s[0]))
    return false;
  return n == atoi(s.c_str());
}

int main() {
  int m, n;
  int rest;
  string s;
  while (cin >> m >> n && (m || n)) {
    getline(cin, s);
    memset(fail, false, sizeof(fail));
    rep(i, m) { nxt[i] = (i + 1) % m; }
    int pos = m - 1;
    rest = m;
    rep(i, n) {
      getline(cin, s);

      if (rest == 1)
        continue;
      if (!check(s, i + 1)) {
        int tmp = nxt[pos];
        nxt[pos] = nxt[tmp];
        fail[tmp] = true;
        rest--;
      } else {
        pos = nxt[pos];
      }
    }

    vector<int> res;
    rep(i, m) if (!fail[i]) res.push_back(i + 1);
    rep(i, (int)res.size()) {
      if (i != 0)
        cout << " ";
      cout << res[i];
    }
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

const int mmax = 1000;

char nums[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

inline string get_s(int n) {
  string s;
  int i = 1;
  while (i * 10 <= n)
    i = i * 10;
  while (i > 0) {
    s.push_back(nums[n / i]);
    i = i / 10;
  }
  return s;
}

int main() {
  string s;
  int m, n;
  list<int> member;
  while (cin >> m >> n) {
    getline(cin, s);
    int count = 0;
    if (m == 0 && n == 0)
      break;
    for (int i = 1; i <= m; i++) {
      member.push_back(i);
    }
    list<int>::iterator iter = member.begin();
    for (int i = 1; i <= n; i++) {
      getline(cin, s);
      if (i % 3 != 0 && i % 5 != 0) {
        if (s != get_s(i))
          iter = member.erase(iter);
        else
          iter++;
      } else if (i % 3 == 0 && i % 5 != 0) {
        if (s != "Fizz")
          iter = member.erase(iter);
        else
          iter++;
      } else if (i % 3 != 0 && i % 5 == 0) {
        if (s != "Buzz")
          iter = member.erase(iter);
        else
          iter++;
      } else {
        if (s != "FizzBuzz")
          iter = member.erase(iter);
        else
          iter++;
      }
      if (iter == member.end())
        iter = member.begin();
    }
    list<int>::iterator it = member.begin();
    cout << *it;
    it++;
    while (it != member.end())
      cout << " " << *(it++);
    cout << endl;
    member.clear();
  }
  return 0;
}