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

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N, c;
vi chara, tmp;

int solve() {
  int f = 1;
  while (f && !tmp.empty()) {
    f = 0;
    int n = tmp[0], c = 0;
    int i;
    for (i = 0; i < tmp.size(); i++) {
      if (tmp[i] == n)
        c++;
      else {
        if (c >= 4) {
          f = 1;
          break;
        } else {
          n = tmp[i];
          c = 1;
        }
      }
    }
    if (c >= 4) {
      tmp.erase(tmp.begin() + i - c, tmp.begin() + i);
    }
  }
  return (int)tmp.size();
}

int main() {
  while (scanf("%d", &N) && N) {
    chara.resize(N);
    rep(i, N) { scanf("%d", &chara[i]); }

    tmp.resize(N);
    int res = N;
    rep(i, N) REP(j, 1, 4) {
      if (chara[i] == j)
        continue;
      tmp = chara;
      tmp[i] = j;
      res = min(res, solve());
    }
    printf("%d\\n", res);
  }
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N, c;
vi chara, tmp;

int solve() {
  int f = 1;
  while (f && !tmp.empty()) {
    f = 0;
    int n = tmp[0], c = 0;
    int i;
    for (i = 0; i < N; i++) {
      if (tmp[i] == n)
        c++;
      else {
        if (c >= 4) {
          f = 1;
          break;
        } else {
          n = tmp[i];
          c = 1;
        }
      }
    }
    if (c >= 4) {
      tmp.erase(tmp.begin() + i - c, tmp.begin() + i);
    }
  }
  return (int)tmp.size();
}

int main() {
  while (scanf("%d", &N) && N) {
    chara.resize(N);
    rep(i, N) {
      scanf("%d", &chara[i]);
      //      chara.PB(c);
    }

    tmp.resize(N);
    int res = N;
    rep(i, N) REP(j, 1, 4) {
      if (chara[i] == j)
        continue;
      tmp = chara;
      tmp[i] = j;
      res = min(res, solve());
    }
    printf("%d\\n", res);
  }
  return 0;
}