alexjercan/bugnet huggingface dataset:
Problem p01811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define INF 1e9
#define llINF 1e18
#define MOD 1e9 + 7
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
using namespace std;
bool ans = false;
void dfs(string str) {
  if (ans)
    return;
  // cout<<str<<endl;
  if (str.size() < 3)
    return;
  if (str.compare("ABC") == 0) {
    ans = true;
    return;
  }
  int cnta = 0, cntb = 0, cntc = 0;
  string sa = "", sb = "", sc = "";
  bool flag2 = true;
  int i = 0;
  for (int i = 0; i < str.size(); i++) {
    if (str[i] == 'A')
      cnta++;
    else if (str[i] == 'B')
      cntb++;
    else
      cntc++;
  }
  int cnt = 0;
  if (cnta <= cntb && cnta <= cntc)
    cnt++;
  if (cntb <= cnta && cntb <= cntc)
    cnt++;
  if (cntc <= cntb && cntc <= cnta)
    cnt++;
  if (cnt > 1)
    return;
  for (i = 0; i < str.size() - 2; i++) {
    if (str.substr(i, 3).compare("ABC") == 0) {
      sa += 'A';
      sb += 'B';
      sc += 'C';
      flag2 = false;
      i += 2;
    } else {
      sa += str[i];
      sb += str[i];
      sc += str[i];
    }
  }
  sa += str.substr(i, str.size() - i);
  sb += str.substr(i, str.size() - i);
  sc += str.substr(i, str.size() - i);
  if (flag2)
    return;
  dfs(sa);
  dfs(sb);
  dfs(sc);
}

int main() {
  string str;
  cin >> str;
  dfs(str);
  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define INF 1e9
#define llINF 1e18
#define MOD 1e9 + 7
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
using namespace std;
bool ans = false;
string ss;
void dfs(string str) {
  // cout<<str<<endl;
  if (ans)
    return;
  if (ss.size() < str.size()) {
    return;
  }
  if (ss.compare(str) == 0) {
    ans = true;
    return;
  }
  string sa = "", sb = "", sc = "";
  for (int i = 0; i < str.size(); i++) {
    if (str[i] == 'A') {
      sa += "ABC";
      sb += 'A';
      sc += 'A';
    } else if (str[i] == 'B') {
      sa += 'B';
      sb += "ABC";
      sc += 'B';
    } else {
      sa += 'C';
      sb += 'C';
      sc += "ABC";
    }
  }
  dfs(sa);
  dfs(sb);
  dfs(sc);
}
int main() {
  cin >> ss;
  dfs("ABC");
  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}
Problem p01811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

bool dfs(string s) {
  if (s == "ABC")
    return true;

  int S = s.size();

  bool ret = false;
  bool ng[3] = {};

  int idx = 0;
  while (idx < S) {
    if (idx < S - 2 && s[idx] == 'A' && s[idx + 1] == 'B' && s[idx + 2] == 'C')
      idx += 3;
    else
      ng[s[idx++] - 'A'] = true;
  }

  rep(i, 3) if (!ng[i]) {
    string t = "";
    idx = 0;
    while (idx < S) {
      if (idx < S - 2 && s[idx] == 'A' && s[idx + 1] == 'B' &&
          s[idx + 2] == 'C') {
        t += 'A' + i;
        idx += 3;
      } else {
        t += s[idx];
        ++idx;
      }
    }
    if (t != s)
      ret |= dfs(t);
  }

  return ret;
}

int main() {
  string s;
  cin >> s;
  cout << (dfs(s) ? "Yes" : "No") << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

string s;
int S;

bool dfs(string x) {
  int X = x.size();
  if (X == S)
    return (x == s);

  bool ret = false;

  int ct[3] = {};
  rep(i, X)++ ct[x[i] - 'A'];

  rep(i, 3) if (X + ct[i] * 2 <= S) {
    string nx = "";
    rep(j, x.size()) {
      if (x[j] == 'A' + i)
        nx += "ABC";
      else
        nx += x[j];
    }

    ret |= dfs(nx);
  }
  return ret;
}

int main() {
  cin >> s;
  S = s.size();
  cout << (dfs("ABC") ? "Yes" : "No") << endl;
  return 0;
}
Problem p01811 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MAX_N 40
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
const long long int MOD = 1000000007;
const long long int INF = 100000000000000;

string str;

bool dfs(string s) {
  if (s == "ABC")
    return true;
  if (s.size() <= 4)
    return false;
  string pre;
  int vari = 0;
  rep(i, s.size()) {
    if (i + 2 >= s.size()) {
      pre.push_back(s[i]);
      vari |= (1 << (s[i] - 'A'));
      continue;
    }
    if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
      pre.push_back('_');
      i += 2;
    } else {
      pre.push_back(s[i]);
      vari |= (1 << (s[i] - 'A'));
    }
  }
  if (vari == 3)
    vari = 2;
  else if (vari == 5)
    vari = 1;
  else if (vari == 6)
    vari = 0;
  else
    return false;
  string now;
  rep(j, pre.size()) {
    if (pre[j] != '_') {
      now.push_back(pre[j]);
    } else {
      if (vari == 0)
        now.push_back('A');
      if (vari == 1)
        now.push_back('B');
      if (vari == 2)
        now.push_back('C');
    }
  }
  return dfs(now);
}

int main() {
  cin >> str;
  if (dfs(str))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MAX_N 40
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
const long long int MOD = 1000000007;
const long long int INF = 100000000000000;

string str;
char pre[10000];

string dfs(string s) {
  if (s == "ABC")
    return "Yes";
  if (s.size() <= 4)
    return "No";
  int num = 0;
  rep(i, s.size()) {
    if (i + 2 >= s.size()) {
      pre[num++] = s[i];
      continue;
    }
    if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
      pre[num++] = '_';
      i += 2;
    } else {
      pre[num++] = s[i];
    }
  }
  rep(i, 3) {
    string now;
    string tmp;
    rep(j, num) {
      if (pre[j] != '_') {
        now.push_back(pre[j]);
      } else {
        if (i == 0)
          now.push_back('A');
        if (i == 1)
          now.push_back('B');
        if (i == 2)
          now.push_back('C');
      }
    }
    tmp = dfs(now);
    if (tmp == "Yes")
      return tmp;
  }
  return "No";
}

int main() {
  cin >> str;
  cout << dfs(str) << endl;
}
Problem p01811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

bool dfs(string s) {
  if (s == "ABC")
    return true;
  else if (s.find("ABC") == string::npos)
    return false;

  string ABC = "A";
  bool res = false;
  rep(i, 3) {
    string tmp = s;
    string::size_type pos = tmp.find("ABC");
    string::size_type ppos;
    int cnt = 0;
    while (pos != string::npos) {
      tmp.replace(pos, 3, ABC);
      pos = tmp.find("ABC", pos + 1);
      cnt++;
    }
    rep(j, tmp.size()) {
      if (tmp[j] == ABC[0])
        cnt--;
    }
    if (cnt == 0)
      res |= dfs(tmp);
    ABC[0]++;
  }
  return res;
}

int main() {
  string s;
  cin >> s;
  if (dfs(s))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

bool f = false;

void dfs(string);
void call(string, string);

void call(string tmp, string x) {
  while (true) {
    int a = static_cast<int>(tmp.find("ABC"));
    if (a == -1)
      break;
    tmp.replace(a, 3, x);
  }
  dfs(tmp);
}

void dfs(string s) {
  if (s.size() < 3)
    f = true;
  if (s.size() == 3 && s != "ABC")
    return;
  if (f)
    return;

  call(s, "A");
  call(s, "B");
  call(s, "C");

  // vector<int> it;
  // string tmp;
  // rep(i,s.size()){
  //     if(i + 3 < s.size() && s.substr(i,3) == "ABC"){
  //         it.emplace_back(i);
  //         i+=2;
  //         tmp+='o';
  //     }else{
  //         tmp+=s[i];
  //     }
  // }
  // if(tmp == s) return;

  // string a = tmp;
  // rep(i,tmp.size()) if(a[i] == 'o') a[i] = 'A';
  // dfs(a);
  // a = tmp;
  // rep(i,tmp.size()) if(a[i] == 'o') a[i] = 'B';
  // dfs(a);
  // a = tmp;
  // rep(i,tmp.size()) if(a[i] == 'o') a[i] = 'B';
  // dfs(a);
}

int main() {
  string s;
  cin >> s;
  if (s.size() >= 3)
    dfs(s);
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p01811 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
bool DFS(string q) {
  if (q == "ABC")
    return 1;
  if (q.size() < 5)
    return 0;
  string x = "";
  char z = 'B';
  if (q.substr(0, 3) == "ABC")
    z = 'A';
  if (q.substr(q.size() - 3, 3) == "ABC")
    z = 'C';
  int i = 0;
  while (i < q.size()) {
    if (q.substr(i, 3) == "ABC") {
      x.push_back(z);
      i += 3;
    } else {
      if (q[i] == z)
        return 0;
      x.push_back(q[i]);
      i++;
    }
  }
  if (x.size() < q.size())
    return DFS(x);
  return 0;
}
int main() {
  string s;
  cin >> s;
  cout << (DFS(s) ? "Yes" : "No") << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
bool t[5010];
bool DFS(string s) {
  if (s == "ABC")
    return true;
  memset(t, 0, s.size());
  for (int i = 0; i < s.size() - 2; i++)
    t[i] = (s.substr(i, 3) == "ABC");
  string x[3] = {"", "", ""};
  for (int i = 0; i < s.size(); i++) {
    bool ok = t[i];
    if (i)
      ok |= t[i - 1];
    if (i > 1)
      ok |= t[i - 2];
    for (int j = 0; j < 3; j++) {
      if (!ok || j + 'A' == s[i])
        x[j].push_back(s[i]);
    }
  }
  bool ok = false;
  for (int i = 0; i < 3; i++) {
    if (x[i].size() < s.size())
      ok |= DFS(x[i]);
  }
  return ok;
}
int main() {
  string s;
  cin >> s;
  cout << (DFS(s) ? "Yes" : "No") << endl;
  return 0;
}
Problem p01811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stack>
#include <string>
#include <unordered_set>

std::string replaceAll(const std::string &s, const std::string &find,
                       const std::string &to);
int count_char(const std::string &s, char find);

int main() {
  std::string s;
  std::cin >> s;
  if (s.size() < 3 || s.size() % 2 == 0) {
    std::cout << "No" << std::endl;
    return 0;
  }

  std::stack<std::string> stack;
  std::unordered_set<std::string> visited;
  stack.push(s);
  visited.insert(s);

  while (!stack.empty()) {
    auto str = stack.top();
    stack.pop();
    if (str == "ABC") {
      std::cout << "Yes" << std::endl;
      return 0;
    }
    for (auto c : {'A', 'B', 'C'}) {
      auto next = str;
      int correct_count = count_char(next, c);
      next = replaceAll(next, "ABC", {c});
      if (next.size() != str.size() - correct_count * 2) {
        continue;
      }
      if (!visited.count(next)) {
        visited.insert(next);
        stack.push(next);
      }
    }
  }
  std::cout << "No" << std::endl;
  return 0;
}

std::string replaceAll(const std::string &s, const std::string &find,
                       const std::string &to) {
  std::string::size_type pos = 0;
  std::string result = s;
  while ((pos = result.find(find, pos)) != std::string::npos) {
    result.replace(pos, find.length(), to);
    pos += to.length();
  }
  return result;
}

int count_char(const std::string &s, char find) {
  int count = 0;
  for (auto c : s) {
    if (c == find)
      count++;
  }
  return count;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <stack>
#include <string>
#include <unordered_set>

std::string replaceAll(const std::string &s, const std::string &find,
                       const std::string &to);

int main() {
  std::string s;
  std::cin >> s;
  if (s.size() < 3 || s.size() % 2 == 0) {
    std::cout << "No" << std::endl;
    return 0;
  }
  if (s == "ABC") {
    std::cout << "Yes" << std::endl;
    return 0;
  }

  std::stack<std::string> stack;
  std::unordered_set<std::string> visited;
  stack.push("ABC");
  visited.insert("ABC");

  while (!stack.empty()) {
    auto str = stack.top();
    stack.pop();
    for (auto c : {'A', 'B', 'C'}) {
      auto next = str;
      next = replaceAll(next, {c}, "ABC");
      if (!visited.count(next) && next.size() <= s.size()) {
        if (next == s) {
          std::cout << "Yes" << std::endl;
          return 0;
        }
        visited.insert(next);
        stack.push(next);
      }
    }
  }
  std::cout << "No" << std::endl;
  return 0;
}

std::string replaceAll(const std::string &s, const std::string &find,
                       const std::string &to) {
  std::string::size_type pos = 0;
  std::string result = s;
  while ((pos = result.find(find, pos)) != std::string::npos) {
    result.replace(pos, find.length(), to);
    pos += to.length();
  }
  return result;
}
Problem p01811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using LL = long long;
using P = pair<int, int>;
using Tapris = tuple<int, int, int>;

#define int long long
#define REP(i, n) for (LL i = 0; i < n; ++i)
#define FOR(i, a, n) for (LL i = a; i < n; ++i)
#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e15;
const int MOD = 1e9 + 7;

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

bool dfs(string s) {
  if (s == "ABC")
    return true;
  else if (s.find("ABC") == string::npos)
    return false;
  vector<bool> valid(3, true);

  string t;
  int i = 0;
  while (i < s.size()) {
    if (i <= (int)s.size() - 3 && s.substr(i, 3) == "ABC") {
      i += 3;
    } else {
      t += s[i];
      i++;
    }
  }
  REP(i, t.size()) valid[t[i] - 'A'] = false;

  bool ret = false;
  REP(k, 3) {
    if (!valid[k])
      continue;
    string t;
    int i = 0;
    while (i < s.size()) {
      if (i <= (int)s.size() - 3 && s.substr(i, 3) == "ABC") {
        t += 'A' + k;
        i += 3;
      } else {
        t += s[i];
        i++;
      }
    }
    ret |= dfs(t);
  }

  return ret;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  cin >> s;
  cout << (dfs(s) ? "Yes" : "No") << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using LL = long long;
using P = pair<int, int>;
using Tapris = tuple<int, int, int>;

#define REP(i, n) for (LL i = 0; i < n; ++i)
#define FOR(i, a, n) for (LL i = a; i < n; ++i)
#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e15;
const int MOD = 1e9 + 7;

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

#define int long long

/*************** using variables ***************/
/**********************************************/

bool dfs(string s) {
  if (s == "ABC")
    return true;
  else if (s.find("ABC") == string::npos)
    return false;
  vector<bool> valid(3, true);
  {
    string t;
    int i = 0;
    while (i < (int)s.size()) {
      if (i <= (int)s.size() - 3 && s.substr(i, 3) == "ABC") {
        i += 3;
      } else {
        t += s[i];
        i++;
      }
      REP(i, t.size()) valid[t[i] - 'A'] = false;
    }
  }
  bool ret = false;
  REP(j, 3) {
    if (!valid[j])
      continue;
    string t;
    int i = 0;
    while (i < (int)s.size()) {
      if (i <= (int)s.size() - 3 && s.substr(i, 3) == "ABC") {
        t += 'A' + j;
        i += 3;
      } else {
        t += s[i];
        i++;
      }
    }
    ret |= dfs(t);
  }

  return ret;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string str;
  cin >> str;
  cout << (dfs(str) ? "Yes" : "No") << endl;
}
Problem p01811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long int llint;
#define pub push_back

// A
// BABA
int main(void) {
  string st, iti;
  int i, j;
  int ia, ib, ic;
  cin >> st;
  for (j = 0; j < 5000; j++) {
    if (st == "ABC") {
      printf("Yes\\n");
      break;
    }
    for (i = 0; i < st.size(); i++) {
      iti.pub(st[i]);
      if (3 <= iti.size() && iti[iti.size() - 3] == 'A' &&
          iti[iti.size() - 2] == 'B' && iti[iti.size() - 1] == 'C') {
        iti.pop_back();
        iti.pop_back();
        iti.pop_back();
        iti.push_back('X');
      }
    }
    ia = 1;
    ib = 1;
    ic = 1;
    for (i = 0; i < iti.size(); i++) {
      if (iti[i] == 'A') {
        ia = 0;
      }
      if (iti[i] == 'B') {
        ib = 0;
      }
      if (iti[i] == 'C') {
        ic = 0;
      }
    }
    if (ia + ib + ic != 1) {
      printf("No\\n");
      break;
    }
    st.clear();
    for (i = 0; i < iti.size(); i++) {
      if (iti[i] != 'X') {
        st.pub(iti[i]);
      } else if (ia == 1) {
        st.pub('A');
      } else if (ib == 1) {
        st.pub('B');
      } else if (ic == 1) {
        st.pub('C');
      }
    }
    iti.clear();
  }
  if (j == 5000) {
    printf("No\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long int llint;
#define pub push_back

// A
// BABA
int main(void) {
  string st, iti;
  int i, j;
  int ia, ib, ic;
  cin >> st;
  while (-1) {
    if (st == "ABC") {
      printf("Yes\\n");
      break;
    }
    for (i = 0; i < st.size(); i++) {
      iti.pub(st[i]);
      if (3 <= iti.size() && iti[iti.size() - 3] == 'A' &&
          iti[iti.size() - 2] == 'B' && iti[iti.size() - 1] == 'C') {
        iti.pop_back();
        iti.pop_back();
        iti.pop_back();
        iti.push_back('X');
      }
    }
    ia = 1;
    ib = 1;
    ic = 1;
    for (i = 0; i < iti.size(); i++) {
      if (iti[i] == 'A') {
        ia = 0;
      }
      if (iti[i] == 'B') {
        ib = 0;
      }
      if (iti[i] == 'C') {
        ic = 0;
      }
    }
    if (ia + ib + ic != 1) {
      printf("No\\n");
      break;
    }
    st.clear();
    for (i = 0; i < iti.size(); i++) {
      if (iti[i] != 'X') {
        st.pub(iti[i]);
      } else if (ia == 1) {
        st.pub('A');
      } else if (ib == 1) {
        st.pub('B');
      } else if (ic == 1) {
        st.pub('C');
      }
    }
    iti.clear();
  }
  return 0;
}
Problem p01811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

using namespace std;

char abc[3] = {'A', 'B', 'C'};

bool dfs(string s) {
  if (s == "ABC")
    return true;

  int i = 0;
  string t = "";
  int cnt[3] = {0};
  bool ret_flag = true;
  while (i < int(s.length())) {
    if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
      t += 'X';
      ret_flag = false;
      i += 3;
    } else {
      cnt[s[i] - 'A']++;
      t += s[i++];
    }
  }
  if (ret_flag)
    return false;

  char c[3] = {'A', 'B', 'C'};
  bool ret = false;
  for (int i = 0; i < 3; i++) {
    if (cnt[i])
      continue;
    string tt(t);
    for (int j = 0; j < int(t.length()); j++) {
      if (t[j] == 'X')
        tt[j] = c[i];
    }
    ret |= dfs(tt);
  }
  return ret;
}

int main() {
  string s;
  cin >> s;
  cout << (dfs(s) ? "Yes" : "No") << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

string s;
char abc[3] = {'A', 'B', 'C'};

bool dfs(string t) {
  if (t == s)
    return true;
  if (t.length() > s.length())
    return false;
  bool ret = false;
  for (int i = 0; i < 3; i++) {
    string next = "";
    for (int j = 0; j < int(t.length()); j++) {
      if (t[j] == abc[i])
        next += "ABC";
      else
        next += t[j];
    }
    ret |= dfs(next);
  }
  return ret;
}

int main() {
  cin >> s;
  cout << (dfs("ABC") ? "Yes" : "No") << endl;
}
Problem p01811 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

string expand(string const &value, char ch) {
  stringstream s;
  for (size_t i = 0; i < value.size();) {
    auto pos = value.find(ch, i);
    auto substr = value.substr(i, pos - i);
    s << substr;
    if (pos == string::npos) {
      break;
    }
    s << "ABC";
    i = pos + 1;
  }
  return s.str();
}

bool dfs(string const &value) {
  if (value == "ABC") {
    return true;
  }

  for (char ch = 'A'; ch <= 'C'; ch++) {
    stringstream s;
    bool done = false;
    for (size_t i = 0; i < value.size();) {
      auto pos = value.find("ABC", i);
      auto substr = value.substr(i, pos - i);
      s << substr;
      if (pos == string::npos) {
        break;
      }
      done = true;
      s << ch;
      i = pos + 3;
    }
    auto prev = s.str();
    if (done && expand(prev, ch) == value && dfs(prev)) {
      return true;
    }
  }

  return false;
}

int main() {
  string line;
  cin >> line;
  if (dfs(line)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

bool dfs(string const &value) {
  if (value == "ABC") {
    return true;
  }
  if (value.size() <= 3) {
    return false;
  }

  stringstream a, b, c;
  bool done = false;
  for (size_t i = 0; i < value.size();) {
    auto pos = value.find("ABC", i);
    auto substr = value.substr(i, pos);
    a << substr;
    b << substr;
    c << substr;
    if (pos == string::npos) {
      break;
    }
    done = true;
    a << 'A';
    b << 'B';
    c << 'C';
    i = pos + 3;
  }

  auto a_str = a.str(), b_str = b.str(), c_str = c.str();

  return done && (dfs(a_str) || dfs(b_str) || dfs(c_str));
}

int main() {
  string line;
  cin >> line;
  if (dfs(line)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}