Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<vector<int>> mati;
typedef vector<vector<double>> matd;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef vector<pll> vpll;

#define FOR(i, x, y) for (ll i = (ll)x; i < (ll)y; ++i)
#define REP(i, y) FOR(i, 0, y)
#define RFOR(i, x, y) for (ll i = (ll)x; i >= (ll)y; --i)
#define RREP(i, x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back

inline void IN(void) { return; }

template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}

inline void OUT(void) {
  cout << "\n";
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
  cout << first << " ";
  OUT(rest...);
  return;
}

template <typename T> void vec_print(vector<T> VEC) {
  REP(i, VEC.size()) { cout << VEC[i] << " "; }
  cout << "\n";
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    REP(i, MAT.size()){REP(j, MAT[i].size()){cout << MAT[i][j] << " ";
}
cout << "\n";
}
}
;

template <typename CLASS1, typename CLASS2> class HOGE {
public:
  CLASS1 key;
  CLASS2 value;
  HOGE(void) { return; };
  HOGE(CLASS1 key, CLASS2 value) {
    this->key = key;
    this->value = value;
  };
  ~HOGE(void) { return; };

  void print(void) {
    cout << "key : " << key << ", value : " << value << "\n";
    return;
  };

  bool operator==(const HOGE &obj) { return (this->value == obj.value); };
  bool operator<(const HOGE &obj) { return (this->value < obj.value); };
  bool operator>(const HOGE &obj) { return (this->value > obj.value); };
};

template <typename CLASS1, typename CLASS2>
bool operator==(const HOGE<CLASS1, CLASS2> &hoge1,
                const HOGE<CLASS1, CLASS2> &hoge2) {
  return hoge1.value == hoge2.value;
};

template <typename CLASS1, typename CLASS2>
bool operator<(const HOGE<CLASS1, CLASS2> &hoge1,
               const HOGE<CLASS1, CLASS2> &hoge2) {
  return hoge1.value < hoge2.value;
};

template <typename CLASS1, typename CLASS2>
bool operator>(const HOGE<CLASS1, CLASS2> &hoge1,
               const HOGE<CLASS1, CLASS2> &hoge2) {
  return hoge1.value > hoge2.value;
};

constexpr int INF = (1 << 30);
constexpr ll INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9) + 7);

int main() {
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after
              // std::cin)
  ios::sync_with_stdio(
      false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  string s;
  IN(s);

  string s_copy = "";
  for (int i = 0; i < s.size(); ++i) {
    if (i == s.size() - 1) {
      s_copy += s[i];
    } else {
      if (s[i] == 'B' && s[i + 1] == 'C') {
        s_copy += 'D';
        i++;
      } else {
        s_copy += s[i];
      }
    }
  }

  ll ans = 0;
  for (int i = 0; i < s_copy.size() - 1; ++i) {
    if (s_copy[i] == 'A' && s_copy[i + 1] == 'D') {
      swap(s_copy[i], s_copy[i + 1]);
      ans++;
      i -= 2;
    }
    if (i < -1)
      i = -1;
  }

  OUT(ans);

  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<vector<int>> mati;
typedef vector<vector<double>> matd;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef vector<pll> vpll;

#define FOR(i, x, y) for (ll i = (ll)x; i < (ll)y; ++i)
#define REP(i, y) FOR(i, 0, y)
#define RFOR(i, x, y) for (ll i = (ll)x; i >= (ll)y; --i)
#define RREP(i, x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back

inline void IN(void) { return; }

template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}

inline void OUT(void) {
  cout << "\n";
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
  cout << first << " ";
  OUT(rest...);
  return;
}

template <typename T> void vec_print(vector<T> VEC) {
  REP(i, VEC.size()) { cout << VEC[i] << " "; }
  cout << "\n";
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    REP(i, MAT.size()){REP(j, MAT[i].size()){cout << MAT[i][j] << " ";
}
cout << "\n";
}
}
;

template <typename CLASS1, typename CLASS2> class HOGE {
public:
  CLASS1 key;
  CLASS2 value;
  HOGE(void) { return; };
  HOGE(CLASS1 key, CLASS2 value) {
    this->key = key;
    this->value = value;
  };
  ~HOGE(void) { return; };

  void print(void) {
    cout << "key : " << key << ", value : " << value << "\n";
    return;
  };

  bool operator==(const HOGE &obj) { return (this->value == obj.value); };
  bool operator<(const HOGE &obj) { return (this->value < obj.value); };
  bool operator>(const HOGE &obj) { return (this->value > obj.value); };
};

template <typename CLASS1, typename CLASS2>
bool operator==(const HOGE<CLASS1, CLASS2> &hoge1,
                const HOGE<CLASS1, CLASS2> &hoge2) {
  return hoge1.value == hoge2.value;
};

template <typename CLASS1, typename CLASS2>
bool operator<(const HOGE<CLASS1, CLASS2> &hoge1,
               const HOGE<CLASS1, CLASS2> &hoge2) {
  return hoge1.value < hoge2.value;
};

template <typename CLASS1, typename CLASS2>
bool operator>(const HOGE<CLASS1, CLASS2> &hoge1,
               const HOGE<CLASS1, CLASS2> &hoge2) {
  return hoge1.value > hoge2.value;
};

constexpr int INF = (1 << 30);
constexpr ll INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9) + 7);

int main() {
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after
              // std::cin)
  ios::sync_with_stdio(
      false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  string s;
  IN(s);

  string s_copy = "";
  for (int i = 0; i < s.size(); ++i) {
    if (i == s.size() - 1) {
      s_copy += s[i];
    } else {
      if (s[i] == 'B' && s[i + 1] == 'C') {
        s_copy += 'D';
        i++;
      } else {
        s_copy += s[i];
      }
    }
  }

  ll ans = 0;
  ll A_num = 0;
  for (int i = 0; i < s_copy.size(); ++i) {
    if (s_copy[i] == 'A')
      A_num++;
    else if (s_copy[i] == 'D')
      ans += A_num;
    else
      A_num = 0;
  }

  OUT(ans);

  return 0;
}