Problem p03049 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define endl '\n'
#define eb emplace_back
#define fst first
#define scd second
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl

constexpr int MOD = 1000000007;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<ll> vl;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

// value
int N;
string s[10010];

void solve() {

  int a = 0;
  int b = 0;
  int ab = 0;
  int abNum = 0;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> s[i];
    for (int j = 0; j < s[i].length(); j++) {
      if (s[i].substr(j, 2) == "AB") {
        abNum++;
      }
    }
    if (s[i].back() == 'A') {
      if (s[i][0] == 'B')
        ab++;
      else
        a++;
    } else if (s[i][0] == 'B')
      b++;
  }

  int tmp;
  while (ab > 0) {
    tmp = min(a, ab);
    abNum += tmp;
    ab -= tmp;
    tmp = min(b, ab);
    abNum += tmp;
    ab -= tmp;
  }

  tmp = min(a, b);
  abNum += tmp;
  a -= tmp;
  b -= tmp;

  cout << abNum << endl;

  return;
}

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  solve();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define endl '\n'
#define eb emplace_back
#define fst first
#define scd second
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl

constexpr int MOD = 1000000007;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<ll> vl;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

// value
int N;
string s[10010];

void solve() {

  int a = 0;
  int b = 0;
  int ab = 0;
  int abNum = 0;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> s[i];
    for (int j = 0; j < s[i].length(); j++) {
      if (s[i].substr(j, 2) == "AB") {
        abNum++;
      }
    }
    if (s[i].back() == 'A') {
      if (s[i][0] == 'B')
        ab++;
      else
        a++;
    } else if (s[i][0] == 'B')
      b++;
  }

  int tmp;
  tmp = min(a, ab);
  abNum += tmp;
  ab -= tmp;
  tmp = min(b, ab);
  abNum += tmp;
  ab -= tmp;

  if (ab > 0) {
    abNum += ab - 1;
    ab = 1;
    if (a > 0 || b > 0) {
      abNum++;
      ab = 0;
    }
  }

  tmp = min(a, b);
  abNum += tmp;
  a -= tmp;
  b -= tmp;

  cout << abNum << endl;

  return;
}

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  solve();

  return 0;
}