Problem p01811 (C++)

Buggy submission (status: Runtime Error):
// テ・ツ淞コテヲツ慊ャテ」ツδ?」ツδウテ」ツδ療」ツδャテ」ツδシテ」ツδ?(テァツクツョテ・ツーツ湘ァツ可?

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
typedef pair<int, int> pii;
typedef long long ll;
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

// s テ」ツ?ォテ・ツ青ォテ」ツ?セテ」ツつ古」ツ?ヲテ」ツ??」ツつ?ABC
// テ」ツつ津・ツ?ィテ」ツ?ヲ u テ」ツ?ォテ・ツ、ツ嘉」ツ?暗」ツつ?
string replace_user(string s, string u) {
  string t = "CBA", orig = s;
  int N = s.length(), M = t.length(), P = u.length();
  if (N < M)
    return s;

  string ret = "", temp = "";
  // cout << "reversed: " << t << endl;
  rep(i, 0, N) {
    if (temp.length() < M) {
      temp = s[i] + temp;
      if (temp == t) {
        ret += u;
        temp = "";
      }
    } else {
      char c = temp[M - 1];
      ret += c;
      temp.pop_back();
      temp = s[i] + temp;
      if (temp == t) {
        ret += u;
        temp = "";
      }
    }
    // cout << "ret = " << ret << ", temp = " << temp << endl;
  }
  reverse(temp.begin(), temp.end());
  // cout << "ret = " << ret << ", temp = " << temp << endl;
  s = ret + temp;
  return s;
}

map<string, int> m;

void solve(string s) {
  if (m.count(s))
    return;
  m[s]++;
  s = replace_user(s, "X");
  int cnt[3] = {};
  rep(i, 0, s.length()) cnt[s[i] - 'A']++;
  rep(i, 0, 3) {
    if (cnt[i])
      continue;
    replace(s.begin(), s.end(), 'X', (char)('A' + i));
    if (!m.count(s))
      solve(s);
  }
}

signed main() {
  string s;
  cin >> s;
  solve(s);
  if (m.count("ABC"))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

Fixed submission:
// テ・ツ淞コテヲツ慊ャテ」ツδ?」ツδウテ」ツδ療」ツδャテ」ツδシテ」ツδ?(テァツクツョテ・ツーツ湘ァツ可?

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
typedef pair<int, int> pii;
typedef long long ll;
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

// s テ」ツ?ォテ・ツ青ォテ」ツ?セテ」ツつ古」ツ?ヲテ」ツ??」ツつ?ABC
// テ」ツつ津・ツ?ィテ」ツ?ヲ u テ」ツ?ォテ・ツ、ツ嘉」ツ?暗」ツつ?
string replace_user(string s, string u) {
  string t = "CBA", orig = s;
  int N = s.length(), M = t.length(), P = u.length();
  if (N < M)
    return s;

  string ret = "", temp = "";
  // cout << "reversed: " << t << endl;
  rep(i, 0, N) {
    if (temp.length() < M) {
      temp = s[i] + temp;
      if (temp == t) {
        ret += u;
        temp = "";
      }
    } else {
      char c = temp[M - 1];
      ret += c;
      temp.pop_back();
      temp = s[i] + temp;
      if (temp == t) {
        ret += u;
        temp = "";
      }
    }
    // cout << "ret = " << ret << ", temp = " << temp << endl;
  }
  reverse(temp.begin(), temp.end());
  // cout << "ret = " << ret << ", temp = " << temp << endl;
  s = ret + temp;
  return s;
}

map<string, int> m;

void solve(string s) {
  if (m.count(s))
    return;
  m[s]++;
  s = replace_user(s, "X");
  int cnt[3] = {};
  rep(i, 0, s.length()) if (s[i] != 'X') cnt[s[i] - 'A']++;
  rep(i, 0, 3) {
    if (cnt[i])
      continue;
    replace(s.begin(), s.end(), 'X', (char)('A' + i));
    if (!m.count(s))
      solve(s);
  }
}

signed main() {
  string s;
  cin >> s;
  solve(s);
  if (m.count("ABC"))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}