Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/stack:16777216")
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define ITER(it, a)                                                            \
  for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(A, value) memset(A, value, sizeof(A))

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair
const double PI = acos(-1.0);

typedef long long Int;
typedef long long LL;
typedef unsigned long long UINT;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;

const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL)INF;

const int MAX = 500000;
const int MAX2 = 20007;
const int IT = 18;
const int BASE = 1000000000;
const int CNT = 300;
const int MOD = 1000000007;

int n;
char S[MAX];

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);

  scanf("%s", S);
  n = strlen(S);

  int res = 0;
  for (int i = 0; i + 2 < n; ++i) {
    if (S[i] == 'A' && S[i + 1] == 'B' && S[i + 2] == 'C') {
      S[i] = 'B';
      S[i + 1] = 'C';
      S[i + 2] = 'A';
      i -= 2;
      i = max(i, -1);
      ++res;
    }
  }

  cout << res << endl;

  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/stack:16777216")
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define ITER(it, a)                                                            \
  for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(A, value) memset(A, value, sizeof(A))

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair
const double PI = acos(-1.0);

typedef long long Int;
typedef long long LL;
typedef unsigned long long UINT;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;

const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL)INF;

const int MAX = 500000;
const int MAX2 = 20007;
const int IT = 18;
const int BASE = 1000000000;
const int CNT = 300;
const int MOD = 1000000007;

int n;
char S[MAX];

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);

  scanf("%s", S);
  n = strlen(S);

  Int res = 0, cnt = 0;
  for (int i = 0; i + 1 < n; ++i) {
    if (S[i] == 'B' && S[i + 1] == 'C') {
      res += cnt;
      ++i;
    } else if (S[i] == 'A')
      ++cnt;
    else
      cnt = 0;
  }

  cout << res << endl;

  return 0;
}