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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define INF 999999999
#define all(v) begin(v), end(v)
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int n;

bool check(vector<int> &color) {
  int m = color.size();
  if (color.size() < 4)
    return false;

  int cnt = 1;
  FOR(i, 1, m) {
    if (color[i - 1] == color[i])
      cnt++;
    else {
      if (cnt >= 4 && i >= cnt) {
        color.erase(color.begin() + (i - cnt), color.begin() + i);
        return true;
      }

      cnt = 1;
    }
  }

  if (cnt >= 4) {
    color.erase(color.begin() + (m - cnt), color.end());
  }

  return false;
}

int solve() {
  vector<int> v(n);
  REP(i, n) cin >> v[i];

  int ans = INF;
  vector<int> color;
  REP(i, n) {
    color = v;
    if (color[i] == 1)
      color[i] = 2;
    else if (color[i] == 2)
      color[i] = 3;
    else
      color[i] = 1;
    while (check(color)) {
    }
    if (ans > color.size())
      ans = color.size();

    color = v;
    if (color[i] == 1)
      color[i] = 3;
    else if (color[i] == 2)
      color[i] = 1;
    else
      color[i] = 2;
    while (check(color)) {
    }
    if (ans > color.size())
      ans = color.size();
  }

  cout << ans << endl;
}

int main() {
  while (true) {
    cin >> n;
    if (!n)
      return 0;
    solve();
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define INF 999999999
#define all(v) begin(v), end(v)
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int n;

bool check(vector<int> &color) {
  if (color.size() < 4)
    return false;

  int cnt = 1;
  FOR(i, 1, n) {
    if (color[i - 1] == color[i])
      cnt++;
    else {
      if (cnt >= 4) {
        color.erase(color.begin() + (i - cnt), color.begin() + i);
        return true;
      }

      cnt = 1;
    }
  }

  if (cnt >= 4) {
    color.erase(color.begin() + (n - cnt), color.end());
  }

  return false;
}

int solve() {
  vector<int> v(n);
  REP(i, n) cin >> v[i];

  int ans = INF;
  vector<int> color;
  REP(i, n) {
    color = v;
    if (color[i] == 1)
      color[i] = 2;
    else if (color[i] == 2)
      color[i] = 3;
    else
      color[i] = 1;
    while (check(color)) {
    }
    if (ans > color.size())
      ans = color.size();

    color = v;
    if (color[i] == 1)
      color[i] = 3;
    else if (color[i] == 2)
      color[i] = 1;
    else
      color[i] = 2;
    while (check(color)) {
    }
    if (ans > color.size())
      ans = color.size();
  }

  cout << ans << endl;
}

int main() {
  while (true) {
    cin >> n;
    if (!n)
      return 0;
    solve();
  }
}