Problem p01811 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  string s;
  cin >> s;
  while (1) {
    if (s == "ABC") {
      cout << "Yes" << endl;
      return 0;
    }
    int n = s.size();
    vector<bool> v(n);
    for (int i = 1; i < n - 1; i++) {
      if (s[i - 1] == 'A' && s[i] == 'B' && s[i + 1] == 'C') {
        v[i - 1] = 1;
        v[i] = 1;
        v[i + 1] = 1;
      }
    }
    int a = 0, b = 0, c = 0;
    for (int i = 0; i < n; i++) {
      if (!v[i]) {
        if (s[i] == 'A') {
          a++;
        } else if (s[i] == 'B') {
          b++;
        } else {
          c++;
        }
      }
    }

    string t;
    if (a == 0) {
      if (b != 0 && c != 0) {
        int p = 0;
        while (1) {
          if (p == n)
            break;
          if (v[p] == 1) {
            t.push_back('A');
            p += 3;
          } else {
            t.push_back(s[p]);
            p++;
          }
        }
      } else {
        cout << "No" << endl;
        return 0;
      }
    }
    if (b == 0) {
      if (a != 0 && c != 0) {
        int p = 0;
        while (1) {
          if (p == n)
            break;
          if (v[p] == 1) {
            t.push_back('B');
            p += 3;
          } else {
            t.push_back(s[p]);
            p++;
          }
        }
      } else {
        cout << "No" << endl;
        return 0;
      }
    }
    if (c == 0) {
      if (b != 0 && a != 0) {
        int p = 0;
        while (1) {
          if (p == n)
            break;
          if (v[p] == 1) {
            t.push_back('C');
            p += 3;
          } else {
            t.push_back(s[p]);
            p++;
          }
        }
      } else {
        cout << "No" << endl;
        return 0;
      }
    }
    s = t;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  string s;
  cin >> s;
  while (1) {
    if (s == "ABC") {
      cout << "Yes" << endl;
      return 0;
    }
    if (s.size() <= 3) {
      cout << "No" << endl;
      return 0;
    }
    int n = s.size();
    vector<bool> v(n);
    for (int i = 1; i < n - 1; i++) {
      if (s[i - 1] == 'A' && s[i] == 'B' && s[i + 1] == 'C') {
        v[i - 1] = 1;
        v[i] = 1;
        v[i + 1] = 1;
      }
    }
    int a = 0, b = 0, c = 0;
    for (int i = 0; i < n; i++) {
      if (!v[i]) {
        if (s[i] == 'A') {
          a++;
        } else if (s[i] == 'B') {
          b++;
        } else {
          c++;
        }
      }
    }

    string t;
    if (a == 0) {
      if (b != 0 && c != 0) {
        int p = 0;
        while (1) {
          if (p == n)
            break;
          if (v[p] == 1) {
            t.push_back('A');
            p += 3;
          } else {
            t.push_back(s[p]);
            p++;
          }
        }
      } else {
        cout << "No" << endl;
        return 0;
      }
    }
    if (b == 0) {
      if (a != 0 && c != 0) {
        int p = 0;
        while (1) {
          if (p == n)
            break;
          if (v[p] == 1) {
            t.push_back('B');
            p += 3;
          } else {
            t.push_back(s[p]);
            p++;
          }
        }
      } else {
        cout << "No" << endl;
        return 0;
      }
    }
    if (c == 0) {
      if (b != 0 && a != 0) {
        int p = 0;
        while (1) {
          if (p == n)
            break;
          if (v[p] == 1) {
            t.push_back('C');
            p += 3;
          } else {
            t.push_back(s[p]);
            p++;
          }
        }
      } else {
        cout << "No" << endl;
        return 0;
      }
    }
    s = t;
  }
  return 0;
}