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


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

string toRight(int i) {
  if (i % 3 == 0 && i % 5 == 0)
    return "FizzBuzz";

  if (i % 3 == 0)
    return "Fizz";

  if (i % 5 == 0)
    return "Buzz";

  stringstream ss;
  ss << i;

  return ss.str();
}

int main() {
  int n, m;
  while (cin >> n >> m && n) {
    vint v(n);
    rep(i, n) v[i] = i + 1;

    int p = 0;
    bool flag = n != 1;
    rep(i, m) {
      string in;
      cin >> in;

      if (flag && in != toRight(i + 1)) {
        v.erase(v.begin() + p);

        flag = v.size() != 1;
      } else
        p++;

      p %= v.size();
    }

    rep(i, v.size()) cout << v[i] << (i + 1 == v.size() ? "\\n" : " ");
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

string toRight(int i) {
  if (i % 3 == 0 && i % 5 == 0)
    return "FizzBuzz";
  if (i % 3 == 0)
    return "Fizz";
  if (i % 5 == 0)
    return "Buzz";

  stringstream ss;
  ss << i;
  return ss.str();
}

int main() {
  int n, m;
  while (cin >> n >> m && n) {
    vint v(n);
    rep(i, n) v[i] = i + 1;

    int p = 0;
    bool flag = true;
    rep(i, m) {
      string in;
      cin >> in;

      if (flag && in != toRight(i + 1)) {
        v.erase(v.begin() + p);

        flag == v.size() != 1;
      } else
        p++;

      p %= v.size();
    }

    rep(i, v.size()) cout << v[i] << (i + 1 == v.size() ? "\\n" : " ");
  }
}