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

#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

bool check_encode(string str, char c, string sn) {
  string ss = "";
  for (int i = 0; i < str.length(); ++i) {
    if (str[i] == c) {
      ss += "ABC";
    } else {
      ss += str[i];
    }
  }
  cout << str << " " << ss << " " << sn << endl;
  if (ss == sn) {
    return false;
  } else {
    return true;
  }
}

int main(void) {
  string str;
  cin >> str;

  string sn = str;
  queue<string> gene;
  gene.push(str);
  bool find = false;

  while (sn.length() > 2 && !gene.empty()) {
    sn = gene.front();
    gene.pop();

    if (sn == "ABC") {
      find = true;
      break;
    }

    vector<bool> seq(sn.length(), false);
    bool cs = false;
    // ABC??????
    for (int i = 0; i < sn.length() - 2; ++i) {
      if (sn[i] == 'A' && sn[i + 1] == 'B' && sn[i + 2] == 'C') {
        seq[i] = true;
        cs = true;
      }
    }

    bool fa, fb, fc;
    fa = fb = fc = false;
    //???????????????
    if (cs) {
      string astr, bstr, cstr;
      astr = bstr = cstr = "";
      for (int i = 0; i < sn.length(); ++i) {
        if (seq[i]) {
          astr += "A";
          bstr += "B";
          cstr += "C";
          i += 2;
        } else {
          astr += sn[i];
          bstr += sn[i];
          cstr += sn[i];
          if (sn[i] == 'A') {
            fa = true;
          } else if (sn[i] == 'B') {
            fb = true;
          } else if (sn[i] == 'C') {
            fc = true;
          }
        }
      }

      if (!fa) {
        gene.push(astr);
      }
      if (!fb) {
        gene.push(bstr);
      }
      if (!fc) {
        gene.push(cstr);
      }
      // cout << astr << " " << bstr << " " << cstr << endl;
    }
  }

  if (find) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

bool check_encode(string str, char c, string sn) {
  string ss = "";
  for (int i = 0; i < str.length(); ++i) {
    if (str[i] == c) {
      ss += "ABC";
    } else {
      ss += str[i];
    }
  }
  cout << str << " " << ss << " " << sn << endl;
  if (ss == sn) {
    return false;
  } else {
    return true;
  }
}

int main(void) {
  string str;
  cin >> str;

  string sn = str;
  queue<string> gene;
  gene.push(str);
  bool find = false;

  while (!gene.empty()) {
    sn = gene.front();
    gene.pop();

    if (sn == "ABC") {
      find = true;
      break;
    }

    vector<bool> seq(sn.length(), false);
    bool cs = false;
    // ABC??????
    for (int i = 0; i < sn.length() - 2; ++i) {
      if (sn[i] == 'A' && sn[i + 1] == 'B' && sn[i + 2] == 'C') {
        seq[i] = true;
        cs = true;
      }
    }

    bool fa, fb, fc;
    fa = fb = fc = false;
    //???????????????
    if (cs) {
      string astr, bstr, cstr;
      astr = bstr = cstr = "";
      for (int i = 0; i < sn.length(); ++i) {
        if (seq[i]) {
          astr += "A";
          bstr += "B";
          cstr += "C";
          i += 2;
        } else {
          astr += sn[i];
          bstr += sn[i];
          cstr += sn[i];
          if (sn[i] == 'A') {
            fa = true;
          } else if (sn[i] == 'B') {
            fb = true;
          } else if (sn[i] == 'C') {
            fc = true;
          }
        }
      }

      if (!fa) {
        gene.push(astr);
      }
      if (!fb) {
        gene.push(bstr);
      }
      if (!fc) {
        gene.push(cstr);
      }
      // cout << astr << " " << bstr << " " << cstr << endl;
    }
  }

  if (find) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}