Problem p03788 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MAXN 600005
template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
int n, k;
bool a[MAXN], x[MAXN], y[MAXN];
char s[MAXN];
void print(bool *a) {
  for (int i = 1; i <= n; i++)
    if (a[i])
      printf("A");
    else
      printf("B");
  printf("\n");
}
int main() {
  read(n), read(k);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++)
    a[i] = s[i] == 'A';
  bool tag = false;
  int start = 1;
  if (k <= 2 * n) {
    for (int i = 1; i <= k; i++) {
      if (tag ^ a[start]) {
        a[start] = !a[start];
        continue;
      }
      tag = !tag;
      a[start + n] = !tag;
      start++;
    }
    for (int i = start; i < start + n; i++)
      if (tag ^ a[i])
        printf("A");
      else
        printf("B");
    printf("\n");
  }
  for (int i = 1; i <= 2 * n + 1; i++) {
    if (tag ^ a[start]) {
      a[start] = !a[start];
      continue;
    }
    tag = !tag;
    a[start + n] = !tag;
    start++;
  }
  for (int i = start; i < start + n; i++)
    x[i - start + 1] = tag ^ a[i];
  for (int i = 2 * n + 2; i <= 2 * n + 2; i++) {
    if (tag ^ a[start]) {
      a[start] = !a[start];
      continue;
    }
    tag = !tag;
    a[start + n] = !tag;
    start++;
  }
  for (int i = start; i < start + n; i++)
    y[i - start + 1] = tag ^ a[i];
  k -= 2 * n;
  if (k & 1)
    print(x);
  else
    print(y);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAXN 600005
template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
int n, k;
bool a[MAXN], x[MAXN], y[MAXN];
char s[MAXN];
void print(bool *a) {
  for (int i = 1; i <= n; i++)
    if (a[i])
      printf("A");
    else
      printf("B");
  printf("\n");
}
int main() {
  read(n), read(k);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++)
    a[i] = s[i] == 'A';
  bool tag = false;
  int start = 1;
  if (k <= 2 * n) {
    for (int i = 1; i <= k; i++) {
      if (tag ^ a[start]) {
        a[start] = !a[start];
        continue;
      }
      tag = !tag;
      a[start + n] = !tag;
      start++;
    }
    for (int i = start; i < start + n; i++)
      if (tag ^ a[i])
        printf("A");
      else
        printf("B");
    printf("\n");
    return 0;
  }
  for (int i = 1; i <= 2 * n + 1; i++) {
    if (tag ^ a[start]) {
      a[start] = !a[start];
      continue;
    }
    tag = !tag;
    a[start + n] = !tag;
    start++;
  }
  for (int i = start; i < start + n; i++)
    x[i - start + 1] = tag ^ a[i];
  for (int i = 2 * n + 2; i <= 2 * n + 2; i++) {
    if (tag ^ a[start]) {
      a[start] = !a[start];
      continue;
    }
    tag = !tag;
    a[start + n] = !tag;
    start++;
  }
  for (int i = start; i < start + n; i++)
    y[i - start + 1] = tag ^ a[i];
  k -= 2 * n;
  if (k & 1)
    print(x);
  else
    print(y);
  return 0;
}