alexjercan/bugnet huggingface dataset:
Problem p01811 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;
string s;
int cnt[3];
bool ok(string s, int a, int b, int c) {
  if (s == "ABC")
    return true;
  string nxt;
  char to;
  int abc;
  if (a < b && a < c)
    to = 'A', abc = a, b -= a, c -= a;
  else if (b < a && b < c)
    to = 'B', abc = b, a -= b, c -= b;
  else if (c < a && c < b)
    to = 'C', abc = c, a -= c, b -= c;
  else
    return false;
  if (abc == 0)
    return false;
  int p = 0;
  while (p < s.size()) {
    if (p + 2 < s.size() && s[p] == 'A' && s[p + 1] == 'B' && s[p + 2] == 'C') {
      nxt += to;
      p += 3;
      abc--;
    } else {
      nxt += s[p];
      p++;
    }
  }
  if (abc != 0)
    return false;
  return ok(nxt, a, b, c);
}

int main() {
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    cnt[s[i] - 'A']++;
  }
  if (ok(s, cnt[0], cnt[1], cnt[2]))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;
string s;
int cnt[3];
bool ok(string s, int a, int b, int c) {
  if (s == "ABC")
    return true;
  string nxt;
  char to;
  int abc;
  if (a < b && a < c)
    to = 'A', abc = a, b -= a, c -= a;
  else if (b < a && b < c)
    to = 'B', abc = b, a -= b, c -= b;
  else if (c < a && c < b)
    to = 'C', abc = c, a -= c, b -= c;
  else
    return false;
  int p = 0;
  while (p < s.size()) {
    if (p + 2 < s.size() && s[p] == 'A' && s[p + 1] == 'B' && s[p + 2] == 'C') {
      nxt += to;
      p += 3;
      abc--;
    } else {
      nxt += s[p];
      p++;
    }
  }
  if (abc != 0)
    return false;
  return ok(nxt, a, b, c);
}

int main() {
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    cnt[s[i] - 'A']++;
  }
  if (ok(s, cnt[0], cnt[1], cnt[2]))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}
Problem p01811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;

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

  for (int i = 0; i < 3; i++) {
    string T = "";
    bool OK = false;
    for (int j = 0; j < (int)S.size(); j++) {
      if ((j + 2 < (int)S.size()) && S.substr(j, 3) == "ABC") {
        T += ('A' + i);
        j += 2;
      } else {
        T += S[j];
        if (S[j] == ('A' + i))
          OK = true;
      }
    }
    if (OK == true)
      continue;
    if (S.size() == T.size())
      break;
    bool ret = dfs(T);
    if (ret == true)
      return true;
  }
  return false;
}

string S;

int main() {
  cin >> S;
  bool ans = dfs(S);
  if (ans == true)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

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

  for (int i = 0; i < 3; i++) {
    string T = "";
    for (int j = 0; j < (int)S.size(); j++) {
      if ((j + 2 < (int)S.size()) && S.substr(j, 3) == "ABC") {
        T += ('A' + i);
        j += 2;
      } else {
        T += S[j];
      }
    }
    if (S.size() == T.size())
      continue;
    bool ret = dfs(T);
    if (ret == true)
      return true;
  }
  return false;
}

string S;

int main() {
  cin >> S;
  bool ans = dfs(S);
  if (ans == true)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}
Problem p01811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

void replace(string &s, char c) {
  for (size_t p = s.find("ABC"); p != string::npos; p = s.find("ABC", p + 2)) {
    s[p] = c;
    s[p + 1] = s[p + 2] = '@';
  }
  s.erase(remove(s.begin(), s.end(), '@'), s.end());
}

string replace2(string &s, char c) {
  string res;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == c)
      res += "ABC";
    else
      res += s[i];
  }
  return res;
}

bool solve(string s) {
  string t = s;
  string log;
  while (t != "ABC" && t.find("ABC") != string::npos) {
    if (t.substr(0, 3) == "ABC") {
      replace(t, 'A');
      log += 'A';
    } else if (t.substr(t.size() - 3, 3) == "ABC") {
      replace(t, 'C');
      log += 'C';
    } else {
      replace(t, 'B');
      log += 'B';
    }
  }
  if (t != "ABC")
    return false;
  reverse(log.begin(), log.end());
  for (int i = 0; i < log.size(); i++) {
    t = replace2(t, log[i]);
  }
  return s == t;
}

int main() {
  string s;
  while (cin >> s) {
    puts(solve(s) ? "Yes" : "No");
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

void replace(string &s, char c) {
  for (size_t p = s.find("ABC"); p != string::npos; p = s.find("ABC", p + 2)) {
    s[p] = c;
    s[p + 1] = s[p + 2] = '@';
  }
  s.erase(remove(s.begin(), s.end(), '@'), s.end());
}

int main() {
  string s;
  cin >> s;
  while (s.size() > 3) {
    if (s.substr(0, 3) == "ABC") {
      replace(s, 'A');
    } else if (s.substr(s.size() - 3, 3) == "ABC") {
      replace(s, 'C');
    } else {
      replace(s, 'B');
    }
  }
  cout << (s == "ABC" ? "Yes" : "No") << endl;
}
Problem p01811 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

char c[][2]{"A", "B", "C"};
int main() {
  string s;
  cin >> s;
  unordered_map<string, bool> mp;
  queue<string> que;
  que.push(s);
  while (!que.empty()) {
    string u = que.front();
    que.pop();
    if (u == "ABC") {
      puts("Yes");
      return 0;
    }
    rep(i, 3) {
      string v = u;
      int d = 0, k = 0;
      while (~(d = (int)v.find("ABC", d))) {
        v.replace(d, 3, c[i]);
        d++;
        k++;
      }
      if (k != count(v.begin(), v.end(), 'A' + i))
        continue;
      if (!mp[v]) {
        mp[v] = true;
        que.push(v);
      }
    }
  }
  puts("No");
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

char C[] = "ABC";
int main() {
  string s;
  cin >> s;
  unordered_map<string, bool> mp;
  queue<string> que;
  que.push("ABC");
  while (!que.empty()) {
    string u = que.front();
    que.pop();
    if (u == s) {
      puts("Yes");
      return 0;
    }
    if (u.size() + 2 > s.size())
      continue;
    for (char c : C) {
      string v = u;
      int d = 0;
      while (~(d = (int)v.find(c, d))) {
        v.replace(d, 1, "ABC");
        d += 3;
      }
      if (v == s) {
        puts("Yes");
        return 0;
      }
      if (v.size() == s.size())
        continue;
      if (!mp[v]) {
        mp[v] = true;
        que.push(v);
      }
    }
  }
  puts("No");
}
Problem p01811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
bool used[3];
string s;

bool check() {
  while (1) {
    memset(used, 0, sizeof(used));
    int flag = 0;
    for (int i = 0; i < s.size(); i++)
      if (i < s.size() - 2 && s[i] + 1 == s[i + 1] &&
          s[i + 1] + 1 == s[i + 2]) {
        i += 2;
        flag = 1;
      } else
        used[s[i] - 'A'] = true;
    if (used[0] + used[1] + used[2] == 2 && flag) {
      string t;
      for (int i = 0; i < 3; i++) {
        if (used[i])
          continue;
        for (int j = 0; j < s.size(); j++)
          if (j < s.size() - 2 && s[j] + 1 == s[j + 1] &&
              s[j + 1] + 1 == s[j + 2]) {
            t += i + 'A';
            j += 2;
          } else
            t += s[j];
      }
      s = t;
    } else if (s == "ABC")
      return true;
    else
      break;
  }
  return false;
}

int main() {
  cin >> s;
  if (check())
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
bool used[3];
string s;

bool check() {
  while (1) {
    memset(used, 0, sizeof(used));
    for (int i = 0; i < s.size(); i++)
      if (i < s.size() - 2 && s[i] + 1 == s[i + 1] && s[i + 1] + 1 == s[i + 2])
        i += 2;
      else
        used[s[i] - 'A'] = true;
    if (used[0] + used[1] + used[2] == 2) {
      string t;
      for (int i = 0; i < 3; i++) {
        if (used[i])
          continue;
        for (int j = 0; j < s.size(); j++)
          if (j < s.size() - 2 && s[j] + 1 == s[j + 1] &&
              s[j + 1] + 1 == s[j + 2]) {
            t += i + 'A';
            j += 2;
          } else
            t += s[j];
      }
      s = t;
    } else if (s == "ABC")
      return true;
    else
      break;
  }
  return false;
}

int main() {
  cin >> s;
  if (check())
    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 Int = long long;
signed main() {
  regex ABC(R"(ABC)"), A(R"(A)"), B(R"(B)"), C(R"(C)");
  Int flg = 0;
  function<void(string)> dfs = [&](string s) {
    if (s == "ABC")
      flg = 1;
    if (flg)
      return;
    string sa = regex_replace(s, ABC, "A");
    if (sa.size() < s.size() && regex_replace(sa, A, "ABC") == s)
      dfs(sa);
    string sb = regex_replace(s, ABC, "B");
    if (sb.size() < s.size() && regex_replace(sb, B, "ABC") == s)
      dfs(sb);
    string sc = regex_replace(s, ABC, "C");
    if (sc.size() < s.size() && regex_replace(sc, C, "ABC") == s)
      dfs(sc);
  };
  string t;
  cin >> t;
  dfs(t);
  cout << (flg ? "Yes" : "No") << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed main() {
  string t;
  cin >> t;
  regex A(R"(A)"), B(R"(B)"), C(R"(C)");
  Int flg = 0;
  Int cnt[3] = {};
  for (char c : t)
    cnt[c - 'A']++;
  function<void(string)> dfs = [&](string s) {
    if (s == t)
      flg = 1;
    if (flg)
      return;
    if (s.size() >= t.size())
      return;
    Int tmp[3] = {};
    for (char c : s)
      tmp[c - 'A']++;
    for (Int i = 0; i < 3; i++)
      if (tmp[i] > cnt[i])
        return;
    dfs(regex_replace(s, A, "ABC"));
    dfs(regex_replace(s, B, "ABC"));
    dfs(regex_replace(s, C, "ABC"));
  };
  dfs("ABC");
  cout << (flg ? "Yes" : "No") << endl;
  return 0;
}
Problem p01811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

bool flag;

void dfs(string str) {
  if (str == "ABC") {
    flag = true;
  }
  if (flag)
    return;
  if (str.size() < 3)
    return;
  if (str.substr(0, 3) == "ABC") {
    string next = "";
    for (int j = 0; j < str.size(); j++) {
      string tmp = "";
      for (int k = 0; (k < 3 && j + k < str.size()); k++) {
        tmp += str[j + k];
      }
      if (tmp == "ABC") {
        next += 'A';
        j += 2;
      } else {
        if (tmp[0] == 'A')
          return;
        next += str[j];
      }
    }
    dfs(next);
  } else if (str.substr(str.size() - 3, 3) == "ABC") {
    string next = "";
    for (int j = 0; j < str.size(); j++) {
      string tmp = "";
      for (int k = 0; (k < 3 && j + k < str.size()); k++) {
        tmp += str[j + k];
      }
      if (tmp == "ABC") {
        next += 'C';
        j += 2;
      } else {
        if (tmp.size() >= 3 && tmp[2] == 'C')
          return;
        next += str[j];
      }
    }
    dfs(next);
  } else {
    string next = "";
    bool change = false;
    for (int j = 0; j < str.size(); j++) {
      string tmp = "";
      for (int k = 0; (k < 3 && j + k < str.size()); k++) {
        tmp += str[j + k];
      }
      if (tmp == "ABC") {
        next += 'B';
        j += 2;
        change = true;
      } else {
        if (tmp.size() >= 2 && tmp[1] == 'B')
          return;
        next += str[j];
      }
    }
    if (change)
      dfs(next);
  }
}

int main(void) {
  string str;
  cin >> str;
  flag = false;
  dfs(str);
  printf("%s\\n", flag ? "Yes" : "No");
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

bool flag;
set<string> data;

void dfs(string str) {
  data.insert(str);
  if (str == "ABC") {
    flag = true;
  }
  if (flag)
    return;
  for (int i = 0; i < 3; i++) {
    string next = "";
    bool change = false;
    for (int j = 0; j < str.size(); j++) {
      string tmp = "";
      for (int k = 0; (k < 3 && j + k < str.size()); k++) {
        tmp += str[j + k];
      }
      if (tmp == "ABC") {
        next += ('A' + i);
        j += 2;
        change = true;
      } else {
        next += str[j];
      }
    }
    if (change && data.find(next) == data.end())
      dfs(next);
  }
}

int main(void) {
  string str;
  cin >> str;
  flag = false;
  dfs(str);
  printf("%s\\n", flag ? "Yes" : "No");
  return 0;
}
Problem p01811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2708&lang=jp
using namespace std;
string S;

void Replace(string &s, char c) {
  for (int i = 0; i < s.length() - 2; i++) {
    if (s.substr(i, 3) == "ABC") {
      s.replace(i, 3, "#");
    }
  }
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '#')
      s[i] = c;
  }
}

void Replace2(string &s, char c) {
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == c) {
      s[i] = '#';
    }
  }
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '#')
      s.replace(i, 1, "ABC");
  }
}

bool check(string &s, vector<char> &abc) {
  while (s != "ABC" && s.find("ABC") != string::npos) {
    if (s.substr(0, 3) == "ABC") {
      Replace(s, 'A');
      abc.push_back('A');
      continue;
    }
    if (s.substr(s.length() - 3, 3) == "ABC") {
      Replace(s, 'C');
      abc.push_back('C');
      continue;
    }
    Replace(s, 'B');
    abc.push_back('B');
    continue;
  }
  if (s != "ABC")
    return false;
  for (int i = abc.size() - 1; i >= 0; i--) {
    Replace2(s, abc[i]);
  }
  if (s == S)
    return true;
  else
    return false;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> S;
  string tmp = S;
  vector<char> abc;
  cout << ((check(tmp, abc) ? "Yes" : "No")) << endl;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2708&lang=jp
using namespace std;
string S;

void Replace(string &s, char c) {
  for (int i = 0; i < s.length() - 3; i++) {
    if (s.substr(i, 3) == "ABC") {
      s.replace(i, 3, "#");
    }
  }
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '#')
      s[i] = c;
  }
  // cout << s << endl;
}

void Replace2(string &s, char c) {
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == c) {
      s[i] = '#';
    }
  }
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '#')
      s.replace(i, 1, "ABC");
  }
  // cout << s << endl;
}

bool check(string &s, vector<char> &abc) {
  while (s != "ABC" && s.find("ABC") != string::npos) {
    if (s.substr(0, 3) == "ABC") {
      Replace(s, 'A');
      abc.push_back('A');
      continue;
    }
    if (s.substr(s.length() - 3, 3) == "ABC") {
      Replace(s, 'C');
      abc.push_back('C');
      continue;
    }
    Replace(s, 'B');
    abc.push_back('B');
    continue;
  }
  if (s != "ABC")
    return false;
  for (int i = abc.size() - 1; i >= 0; i--) {
    Replace2(s, abc[i]);
  }
  if (s == S)
    return true;
  else
    return false;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> S;
  string tmp = S;
  vector<char> abc;
  cout << ((check(tmp, abc) ? "Yes" : "No")) << endl;
}
Problem p01811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
  string S;
  cin >> S;
  map<char, int> M;
  while (S.size() > 3) {
    if (!(S.size() % 2)) {
      cout << "No" << endl;
      return 0;
    }
    for (char c = 'A'; c < 'D'; ++c)
      M[c] = 0;
    int abc = 0;
    int l = S.size();
    for (int i = 0; i < l; ++i) {
      M[S[i]] += 1;
      if (S[i] == 'A' && i < l - 2 && S[i + 1] == 'B' && S[i + 2] == 'C')
        abc += 1;
    }

    char s = 'A';
    bool f = true;
    for (; s < 'D'; ++s) {
      if (!(M[s] - abc)) {
        f = false;
        break;
      }
    }
    if (f) {
      cout << "No" << endl;
      return 0;
    }
    string T = S;
    S = "";
    for (int i = 0; i < l; ++i) {
      if (i < l - 2 && T[i] == 'A' && T[i + 1] == 'B' && T[i + 2] == 'C') {
        S += s;
        i += 2;
      } else
        S += T[i];
    }
  }
  if (S == "ABC")
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  ;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
  string S;
  cin >> S;
  map<char, int> M;
  while (S.size() > 3) {
    for (char c = 'A'; c < 'D'; ++c)
      M[c] = 0;
    int abc = 0;
    int l = S.size();
    for (int i = 0; i < l; ++i) {
      M[S[i]] += 1;
      if (S[i] == 'A' && i < l - 2 && S[i + 1] == 'B' && S[i + 2] == 'C')
        abc += 1;
    }

    char s = 'A';
    bool f = true;
    for (; s < 'D'; ++s) {
      if (!(M[s] - abc)) {
        f = false;
        break;
      }
    }
    if (f) {
      cout << "No" << endl;
      return 0;
    }
    string T = S;
    S = "";
    for (int i = 0; i < l; ++i) {
      if (i < l - 2 && T[i] == 'A' && T[i + 1] == 'B' && T[i + 2] == 'C') {
        S += s;
        i += 2;
      } else
        S += T[i];
    }
  }
  if (S == "ABC")
    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;

bool check(string &a, int i) {
  if (i + 3 > (int)a.size())
    return 0;
  return a[i] == 'A' && a[i + 1] == 'B' && a[i + 2] == 'C';
}

string ABC = "ABC";
bool dfs(string sta) {
  if (sta == ABC)
    return 1;
  if (sta.size() <= 3)
    return 0;

  bool used[3] = {1, 1, 1};
  for (int i = 0; i < sta.size();)
    if (check(sta, i))
      i += 3;
    else
      used[sta[i++] - 'A'] = 0;

  for (int i = 0; i < 3; i++) {
    if (!used[i])
      continue;
    string nx = "";
    int j = 0;
    while (j < (int)sta.size())
      if (check(sta, j))
        nx += ABC[i], j += 3;
      else
        nx += sta[j++];
    if (nx.size() == sta.size())
      return 0;
    if (dfs(nx))
      return 1;
  }
  return 0;
}

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string str, ABC = "ABC";

bool ans = 0;
void dfs(string &sta) {
  if (sta == ABC)
    ans = 1;
  if (ans)
    return;

  for (int i = 0; i < 3; i++) {
    string nx;
    for (int j = 0; j < sta.size(); j++)
      if (j + 3 <= sta.size() && sta.substr(j, 3) == ABC)
        nx += ABC[i], j += 2;
      else
        nx += sta[j];
    if (nx != sta)
      dfs(nx);
  }
}

int main() {
  cin >> str;
  dfs(str);
  cout << (ans ? "Yes" : "No") << endl;
  return 0;
}
Problem p01811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
string a;
bool dfs(string s) {
  if (s.size() <= 3)
    return s == "ABC";
  string s1, s2, s3;
  int v[3] = {};
  r(i, s.size())

      if (i + 2 < s.size())

          if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') s1 += "A",
      s2 += "B", s3 += "C", i += 2;

  else s1 += s[i], s2 += s[i], s3 += s[i], v[s[i] - 'A']++;

  else s1 += s[i], s2 += s[i], s3 += s[i], v[s[i] - 'A']++;

  if (s != s1) {
    if (!v[0])
      if (dfs(s1))
        return 1;
    if (!v[1])
      if (dfs(s2))
        return 1;
    if (!v[2])
      if (dfs(s3))
        return 1;
  }
  return 0;
}
main() {
  cin >> a;
  cout << (dfs(a) ? "Yes" : "No") << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
string a;
bool dfs(string s) {
  if (s.size() > a.size())
    return 0;
  if (s == a)
    return 1;
  string s1, s2, s3;
  r(i, s.size()) if (s[i] == 'A') s1 += "ABC";
  else s1 += s[i];
  r(i, s.size()) if (s[i] == 'B') s2 += "ABC";
  else s2 += s[i];
  r(i, s.size()) if (s[i] == 'C') s3 += "ABC";
  else s3 += s[i];
  if (dfs(s1))
    return 1;
  if (dfs(s2))
    return 1;
  if (dfs(s3))
    return 1;
  return 0;
}
int main() {
  cin >> a;
  if (dfs("ABC"))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p01811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF (1e9 + 1)
// #define INF (1LL<<59)

int main() {
  string s;
  cin >> s;

  while (1) {
    if (s == "ABC")
      break;

    string nxt = "";
    int count = 0;
    int c[3] = {};
    for (int i = 0; i < s.size();) {
      if (i + 2 < s.size() && s.substr(i, 3) == "ABC") {
        count++;
        i += 3;
      } else {
        c[s[i] - 'A']++;
        i++;
      }
    }
    int zero = 0, pos = -1;
    rep(i, 3) if (c[i] == 0) zero++, pos = i;
    if (zero != 1) {
      cout << "No" << endl;
      return 0;
    }
    if (count == 0) {
      cout << "No" << endl;
      return 0;
    }

    for (int i = 0; i < s.size();) {
      if (i + 2 < s.size() && s.substr(i, 3) == "ABC") {
        nxt += ('A' + pos);
        i += 3;
      } else {
        nxt += s[i];
        i++;
      }
    }
    s = nxt;
  }
  cout << "Yes" << endl;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF (1e9 + 1)
// #define INF (1LL<<59)

int main() {
  string s;
  cin >> s;

  while (1) {
    if (s == "ABC")
      break;

    string nxt = "";
    int c[3] = {};
    for (int i = 0; i < s.size();) {
      if (i + 2 < s.size() && s.substr(i, 3) == "ABC") {
        i += 3;
      } else {
        c[s[i] - 'A']++;
        i++;
      }
    }
    int zero = 0, pos = -1;
    rep(i, 3) if (c[i] == 0) zero++, pos = i;
    if (zero != 1) {
      cout << "No" << endl;
      return 0;
    }

    for (int i = 0; i < s.size();) {
      if (i + 2 < s.size() && s.substr(i, 3) == "ABC") {
        nxt += ('A' + pos);
        i += 3;
      } else {
        nxt += s[i];
        i++;
      }
    }
    s = nxt;
  }
  cout << "Yes" << endl;
}
Problem p01811 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

bool dfs(string s) {
  string t[3];
  if (s.size() < 4)
    return s == "ABC";
  bool u[3] = {};
  for (int i = 0; i < s.size(); i++) {
    bool f = i + 2 < s.size();
    for (int j = 0; f && j < 3; j++)
      if (s[i + j] != 'A' + j)
        f = 0;
    for (int j = 0; j < 3; j++)
      t[j] += f ? 'A' + j : s[i];
    if (!f)
      u[s[i] - 'A'] = 1;
    i += f * 2;
  }
  if (s.size() != t[0].size())
    for (int i = 0; i < 3; i++)
      if (!u[i] && dfs(t[i])) {
        return 1;
      }
  return 0;
}

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


Error: MLE

Fixed submission:

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

bool dfs(string &s) {
  string t[3];
  if (s.size() < 4)
    return s == "ABC";
  for (int i = 0; i < s.size(); i++) {
    bool f = i + 2 < s.size();
    for (int j = 0; f && j < 3; j++)
      if (s[i + j] != 'A' + j)
        f = 0;
    for (int j = 0; j < 3; j++)
      t[j] += f ? 'A' + j : s[i];
    i += f * 2;
  }
  for (int i = 0; i < 3; i++)
    if (dfs(t[i]))
      return 1;
  return 0;
}

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