Problem p03336 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>

namespace x_x {
const int N = 201000;

int s[N];
int n, k;

void initialize() {
  static char str[N];

  int len0, len1;

  scanf("%d%d%d", &len0, &len1, &k);

  scanf("%s", str);
  for (int i = 1; i <= len0; i++)
    s[i] |= (str[len0 - i] - '0');
  scanf("%s", str);
  for (int i = 1; i <= len1; i++)
    s[i] |= (str[len1 - i] - '0') << 1;

  n = std::max(len0, len1) + k;
}

std::stack<int> A, B;

void work(int pos) {
  if (!k)
    return;

  int cnt = k - 1, x = 3, flag = 0;

  for (int i = pos; i <= n && x > 0; i++) {
    if (x == 3) {
      int fk = std::min(A.top() - i, cnt);
      cnt -= fk, i += fk;
    }

    int a = s[i] ^ x, b = s[i] & x;
    s[i] = a, x = b;

    if (s[i] == 3) {
      if (cnt == 0)
        flag = 1;
      else
        s[i] = 0, x = 3, cnt--;
    }

    if (s[i] != 0) {
      if (A.top() != i)
        B.push(i);
      else
        A.pop(), B.push(i);
    } else {
      if (A.top() == i)
        A.pop();
    }

    if (flag)
      break;
  }

  while (!B.empty())
    A.push(B.top()), B.pop();
}

void output() {
  bool flag = 0;
  for (int i = n; i; i--) {
    flag |= s[i] & 1;
    if (flag)
      printf("%d", s[i] & 1);
  }
  printf("\n");
  flag = 0;
  for (int i = n; i; i--) {
    flag |= (s[i] >> 1) & 1;
    if (flag)
      printf("%d", (s[i] >> 1) & 1);
  }
  printf("\n");
}

void solve() {
  initialize();

  A.push(n + 1);

  for (int i = n; i; i--) {
    if (s[i] > 0)
      A.push(i);
    if (s[i] == 3)
      work(i);
  }

  output();
}
} // namespace x_x

int main() {
  x_x::solve();
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>

namespace x_x {
const int N = 2010000;

int s[N];
int n, k;

void initialize() {
  static char str[N];

  int len0, len1;

  scanf("%d%d%d", &len0, &len1, &k);

  scanf("%s", str);
  for (int i = 1; i <= len0; i++)
    s[i] |= (str[len0 - i] - '0');
  scanf("%s", str);
  for (int i = 1; i <= len1; i++)
    s[i] |= (str[len1 - i] - '0') << 1;

  n = std::max(len0, len1) + k;
}

std::stack<int> A, B;

void work(int pos) {
  if (!k)
    return;

  int cnt = k - 1, x = 3, flag = 0;

  for (int i = pos; i <= n && x > 0; i++) {
    if (x == 3) {
      int fk = std::min(A.top() - i, cnt);
      cnt -= fk, i += fk;
    }

    int a = s[i] ^ x, b = s[i] & x;
    s[i] = a, x = b;

    if (s[i] == 3) {
      if (cnt == 0)
        flag = 1;
      else
        s[i] = 0, x = 3, cnt--;
    }

    if (s[i] != 0) {
      if (A.top() != i)
        B.push(i);
      else
        A.pop(), B.push(i);
    } else {
      if (A.top() == i)
        A.pop();
    }

    if (flag)
      break;
  }

  while (!B.empty())
    A.push(B.top()), B.pop();
}

void output() {
  bool flag = 0;
  for (int i = n; i; i--) {
    flag |= s[i] & 1;
    if (flag)
      printf("%d", s[i] & 1);
  }
  printf("\n");
  flag = 0;
  for (int i = n; i; i--) {
    flag |= (s[i] >> 1) & 1;
    if (flag)
      printf("%d", (s[i] >> 1) & 1);
  }
  printf("\n");
}

void solve() {
  initialize();

  A.push(n + 1);

  for (int i = n; i; i--) {
    if (s[i] > 0)
      A.push(i);
    if (s[i] == 3)
      work(i);
  }

  output();
}
} // namespace x_x

int main() {
  x_x::solve();
  return 0;
}