Problem p03049 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define rer(i, l, u) for (int(i) = (int)(l); (i) <= (int)(u); ++(i))
#define reu(i, l, u) for (int(i) = (int)(l); (i) < (int)(u); ++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r, v) auto r = (v)
#else
#define aut(r, v) __typeof(v) r = (v)
#endif
#define each(it, o) for (aut(it, (o).begin()); it != (o).end(); ++it)
#define ktya(x) sort(all(x))
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset(m, v, sizeof(m))
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define SIZE 200105
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef pair<int, ll> pill;
typedef pair<ll, int> plli;
typedef pair<double, int> pdi;
template <typename T, typename U> inline void amin(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}
typedef complex<double> P;
ll MOD = 1000000007;
// ll MOD=998244353;
typedef ll Weight;
struct Edge {
  int src, dst;
  Weight weight;
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
ll dp[114514] = {0};
ll sum[114514] = {0};

int N;
ll A, B;

int main() {
  cin >> N;
  string s[5000];
  rep(i, N) cin >> s[i];
  int ans = 0;
  int x = 0, y = 0, z = 0;
  rep(i, N) {
    int sz = s[i].size();
    rep(j, sz - 1) {
      if (s[i][j] == 'A' && s[i][j + 1] == 'B')
        ans++;
    }
    if (s[i][0] != 'B' && s[i][sz - 1] == 'A')
      x++;
    if (s[i][0] == 'B' && s[i][sz - 1] != 'A')
      y++;
    if (s[i][0] == 'B' && s[i][sz - 1] == 'A')
      z++;
  }
  if (z == 0) {
    ans += min(x, y);
  } else {
    ans += z - 1;
    if (x > 0) {
      x--;
      ans++;
    }
    if (y > 0) {
      y--;
      ans++;
    }
    ans += min(x, y);
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define rer(i, l, u) for (int(i) = (int)(l); (i) <= (int)(u); ++(i))
#define reu(i, l, u) for (int(i) = (int)(l); (i) < (int)(u); ++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r, v) auto r = (v)
#else
#define aut(r, v) __typeof(v) r = (v)
#endif
#define each(it, o) for (aut(it, (o).begin()); it != (o).end(); ++it)
#define ktya(x) sort(all(x))
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset(m, v, sizeof(m))
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define SIZE 200105
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef pair<int, ll> pill;
typedef pair<ll, int> plli;
typedef pair<double, int> pdi;
template <typename T, typename U> inline void amin(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}
typedef complex<double> P;
ll MOD = 1000000007;
// ll MOD=998244353;
typedef ll Weight;
struct Edge {
  int src, dst;
  Weight weight;
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
ll dp[114514] = {0};
ll sum[114514] = {0};

int N;
ll A, B;

int main() {
  cin >> N;
  string s[10005];
  rep(i, N) cin >> s[i];
  int ans = 0;
  int x = 0, y = 0, z = 0;
  rep(i, N) {
    int sz = s[i].size();
    rep(j, sz - 1) {
      if (s[i][j] == 'A' && s[i][j + 1] == 'B')
        ans++;
    }
    if (s[i][0] != 'B' && s[i][sz - 1] == 'A')
      x++;
    if (s[i][0] == 'B' && s[i][sz - 1] != 'A')
      y++;
    if (s[i][0] == 'B' && s[i][sz - 1] == 'A')
      z++;
  }
  if (z == 0) {
    ans += min(x, y);
  } else {
    ans += z - 1;
    if (x > 0) {
      x--;
      ans++;
    }
    if (y > 0) {
      y--;
      ans++;
    }
    ans += min(x, y);
  }
  cout << ans << endl;
}