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

#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000

int main() {

  //??\\???
  string s;
  cin >> s;
  // cout << s << endl;
  bool flag = true;
  while (1) {

    //????????????
    if (s.size() < 3) {
      flag = false;
      break;
    }

    //???ABC"?????????????????§??????
    if (s == "ABC") {
      flag = true;
      break;
    }

    //"ABC"??????X"??????????????????
    vector<int> v;
    flag = false;
    for (int i = 0; i < s.size() - 2; i++) {
      if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
        v.push_back(i); // A??????????????????
        flag = true;
      }
    }

    if (!flag)
      break;

    for (int i = 0; i < v.size(); i++) {
      s.erase(s.begin() + v[i] - 2 * i, s.begin() + v[i] - 2 * i + 3);
      s.insert(s.begin() + v[i] - 2 * i, 'X');
    }

    bool f[3] = {0, 0, 0};
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == 'A')
        f[0] = true;
      if (s[i] == 'B')
        f[1] = true;
      if (s[i] == 'C')
        f[2] = true;
    }

    if (f[0] && f[1] && f[2]) {
      flag = false;
      break;
    }

    char x;
    //????????????????±???????

    for (int i = 0; i < s.size(); i++) {
      if (s[i] == 'X') {
        if (i == 0) {
          x = 'A';
        } else {
          if (s[i - 1] == 'A') {
            x = 'B';
          } else if (s[i - 1] == 'B') {
            x = 'C';
          } else {
            flag = false;
          }
        }
        break;
      }
    }

    if (!flag)
      break;

    flag = false;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == 'X') {
        s[i] = x;
        flag = true;
      }
    }

    if (!flag)
      break;
    // cout << s << endl;
    // int input; cin >> input;
  }

  if (flag)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000

int main() {

  //??\\???
  string s;
  cin >> s;
  char v[3] = {'A', 'B', 'C'};

  bool flag;
  while (1) {

    //???ABC"?????????????????§??????
    if (s == "ABC") {
      flag = 1;
      break;
    }

    //"ABC"??????X"??????????????????
    vector<int> v;
    for (int i = 0; i < s.size() - 2; i++) {
      if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
        v.push_back(i); // A??????????????????
      }
    }

    for (int i = 0; i < v.size(); i++) {
      s.erase(s.begin() + v[i] - 2 * i, s.begin() + v[i] - 2 * i + 3);
      s.insert(s.begin() + v[i] - 2 * i, 'X');
    }

    char x;
    // X??????????????????????????????
    if (s[0] == 'B' || s[0] == 'C') {
      flag = 0;
      break;
    } else if (s[0] == 'X') {
      x = 'A';
    } else {
      if (s[1] == 'A' || s[1] == 'C') {
        flag = 0;
        break;
      } else if (s[1] == 'X') {
        x = 'B';
      } else {
        if (s[2] == 'A' || s[2] == 'C') {
          flag = 0;
          break;
        } else if (s[2] == 'X') {
          x = 'C';
        } else {
          if (s == "ABC") {
            flag = 1;
            break;
          } else {
            flag = 0;
            break;
          }
        }
      }
    }

    for (int i = 0; i < s.size(); i++) {
      if (s[i] == 'X') {
        s[i] = x;
      }
    }

    // cout << s << endl;
  }

  if (flag)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}