Problem p01811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 28;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

const string A = "ABC";

string s;

int main() {
  cin >> s;
  int cnt = 0;
  while (s != A) {
    int S = s.size();
    vi a(S), b(3, 1);
    for (int i = 0; i < S; i++) {
      if (s.substr(i, 3) == A) {
        a[i]++;
        i += 2;
      } else
        b[s[i] - 'A'] = 0;
    }
    if (b[0] + b[1] + b[2] != 1 || cnt > 3000) {
      cout << "No" << endl;
      return 0;
    }
    char c = 'A';
    for (int i = 0; i < 3; i++)
      if (b[i])
        c = c + i;
    string t;
    for (int i = 0; i < S; i++) {
      if (a[i]) {
        t += c;
        i += 2;
      } else
        t += s[i];
    }
    cnt++;
    s = t;
  }
  cout << "Yes" << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 28;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

const string A = "ABC";

string s;

int main() {
  cin >> s;
  while (s != A) {
    int S = s.size();
    vi a(S), b(3, 1);
    for (int i = 0; i < S; i++) {
      if (s.substr(i, 3) == A) {
        a[i]++;
        i += 2;
      } else
        b[s[i] - 'A'] = 0;
    }
    if (b[0] + b[1] + b[2] > 1) {
      cout << "No" << endl;
      return 0;
    }
    char c = 'A';
    for (int i = 0; i < 3; i++)
      if (b[i])
        c = c + i;
    string t;
    for (int i = 0; i < S; i++) {
      if (a[i]) {
        t += c;
        i += 2;
      } else
        t += s[i];
    }
    s = t;
  }
  cout << "Yes" << endl;
}