Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
// Doing Some Optimization

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

#define int long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define FF first
#define SS second
#define sz(x) (int)x.size()
#define endl '\n'
// #define hell          1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
// mp::cpp_int

const int hell = 1000000009;

int power(int a, int b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  int t = power(a, b / 2);
  t %= hell;
  t *= t;
  t %= hell;
  if (b % 2)
    return (t * a) % hell;
  else
    return t;
}

int gcd(int a, int b) {

  if (b == 0)
    return a;
  return gcd(b - a % b, a % b);
}

int digitsum(int x) {
  while (x > 9) {
    int ans = 0;
    while (x != 0) {
      ans += x % 10;
      x /= 10;
    }
    x = ans;
  }
  return x;
}

int pw(int a, int b) {
  int x = a;
  rep(i, 1, b) { a = a * x; }
  return a;
}

int solve() {

  string s;
  cin >> s;
  int n = s.size();
  int count = 0;
  int ans = 0;
  for (int i = 0; i < n - 2; i++) {
    if (s[i] == 'A')
      count++;
    else
      count = 0;
    if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
      ans += count;
      s[i - count + 1] = 'B';
      s[i - count + 2] = 'C';
      int x = count;
      for (int j = i + 3 - count; x-- && j < n; j++)
        s[j] = 'A';
      // cout<<s<<endl;

      i -= count;
      count = 0;
    }
  }
  cout << ans;

  return 0;
}

signed main() {
  int t = 1;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin.exceptions(cin.failbit);

  clock_t time_req;

  // Without using pow function

  // cin>>t;

  int k = 1;
  while (t--) {
    // cout<<"case #"<<k<<":"<<" ";k++;
    solve();
  }

  time_req = clock() - time_req;
  // cout <<endl<< "Processor time taken is "
  //<< (double)time_req/CLOCKS_PER_SEC << " seconds" << endl;

  return 0;
}


Fixed submission:
// Doing Some Optimization

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

#define int long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define FF first
#define SS second
#define sz(x) (int)x.size()
#define endl '\n'
// #define hell          1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
// mp::cpp_int

const int hell = 1000000009;

int power(int a, int b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  int t = power(a, b / 2);
  t %= hell;
  t *= t;
  t %= hell;
  if (b % 2)
    return (t * a) % hell;
  else
    return t;
}

int gcd(int a, int b) {

  if (b == 0)
    return a;
  return gcd(b - a % b, a % b);
}

int digitsum(int x) {
  while (x > 9) {
    int ans = 0;
    while (x != 0) {
      ans += x % 10;
      x /= 10;
    }
    x = ans;
  }
  return x;
}

int pw(int a, int b) {
  int x = a;
  rep(i, 1, b) { a = a * x; }
  return a;
}

int solve() {

  string s;
  cin >> s;
  int n = s.size();
  int count = 0;
  int ans = 0;
  for (int i = 0; i < n - 2; i++) {
    if (s[i] == 'A')
      count++;
    else
      count = 0;
    if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
      ans += count;
      s[i + 2] = 'A';
      // cout<<s<<" "<<count<<endl;
      i += 1;
      count--;
    }
  }
  cout << ans;

  return 0;
}

signed main() {
  int t = 1;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin.exceptions(cin.failbit);

  clock_t time_req;

  // Without using pow function

  // cin>>t;

  int k = 1;
  while (t--) {
    // cout<<"case #"<<k<<":"<<" ";k++;
    solve();
  }

  time_req = clock() - time_req;
  // cout <<endl<< "Processor time taken is "
  //<< (double)time_req/CLOCKS_PER_SEC << " seconds" << endl;

  return 0;
}