Problem p03788 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define LL long long
#define mp(x, y) make_pair(x, y)
#define pll pair<long long, long long>
#define pii pair<int, int>
using namespace std;
inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int stack[20];
template <typename T> inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int top = 0;
  while (x)
    stack[++top] = x % 10, x /= 10;
  while (top)
    putchar(stack[top--] + '0');
}
template <typename T> inline void pr1(T x) {
  write(x);
  putchar(' ');
}
template <typename T> inline void pr2(T x) {
  write(x);
  putchar('\n');
}
const int MAXN = 400005;
char ch[MAXN];
int n, K, cnt, len;
char fac(char a, int p) { return ((a - 'A') ^ p) + 'A'; }
deque<char> ans;
int main() {
  //	freopen("a.in","r",stdin);
  n = read();
  K = read();
  scanf("%s", ch + 1);
  len = n;
  int o = 0;
  if (K > n * 2 + 1)
    K = (K & 1) ? (n * 2 + 1) : (n * 2 + 2);
  int r = n, l = 1;
  for (int i = 1; i <= K; i++) {
    if (fac(ch[l], o) == 'A')
      ch[l] = fac('B', o);
    else
      ++l, o ^= 1, ch[++r] = fac('A', o);
  }
  for (;; ++l) {
    putchar(fac(ch[l], o));
    if (l == r)
      break;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define LL long long
#define mp(x, y) make_pair(x, y)
#define pll pair<long long, long long>
#define pii pair<int, int>
using namespace std;
inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int stack[20];
template <typename T> inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int top = 0;
  while (x)
    stack[++top] = x % 10, x /= 10;
  while (top)
    putchar(stack[top--] + '0');
}
template <typename T> inline void pr1(T x) {
  write(x);
  putchar(' ');
}
template <typename T> inline void pr2(T x) {
  write(x);
  putchar('\n');
}
const int MAXN = 800005;
char ch[MAXN];
int n, K, cnt, len;
char fac(char a, int p) { return ((a - 'A') ^ p) + 'A'; }
deque<char> ans;
int main() {
  //	freopen("a.in","r",stdin);
  n = read();
  K = read();
  scanf("%s", ch + 1);
  len = n;
  int o = 0;
  if (K > n * 2 + 1)
    K = (K & 1) ? (n * 2 + 1) : (n * 2 + 2);
  int r = n, l = 1;
  for (int i = 1; i <= K; i++) {
    if (fac(ch[l], o) == 'A')
      ch[l] = fac('B', o);
    else
      ++l, o ^= 1, ch[++r] = fac('A', o);
  }
  for (;; ++l) {
    putchar(fac(ch[l], o));
    if (l == r)
      break;
  }
  return 0;
}