Problem p02816 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vll = vector<ll>;
template <typename T> using uset = unordered_set<T>;
template <typename T1, typename T2> using umap = unordered_map<T1, T2>;

constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\n';
constexpr int mod = 1000000007;

template <typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template <typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

vint Z_algorithm(vint &W) {
  int i = 1, j = 0;
  vint Z(W.size());
  Z[0] = W.size();
  while (i < W.size()) {
    while (i + j < W.size() && W[i + j] == W[j])
      ++j;
    Z[i] = j;
    if (j == 0) {
      ++i;
      continue;
    }
    int k = 1;
    while (Z[k] + k < j)
      Z[i + k] = Z[k], ++k;
    i += k, j -= k;
  }
  return (Z);
}
int main() {
  int N;
  cin >> N;
  vint a(N), b(N);
  vint A(N), B(N);
  for (auto &v : a)
    cin >> v;
  for (auto &v : b)
    cin >> v;
  A = a, B = b;
  for (int i = 0; i < N; i++) {
    if (i > 0)
      A[i - 1] ^= A[i];
  }
  A[N] ^= a[0];
  for (int i = 0; i < N; i++) {
    if (i > 0)
      B[i - 1] ^= B[i];
  }
  B.pop_back();

  copy(begin(A), end(A), back_inserter(B));
  copy(begin(A), end(A), back_inserter(B));

  vint ans = Z_algorithm(B);
  int cnt = 0;
  for (int i = N - 1; i < 2 * N - 1; i++) {
    if (ans[i] >= N - 1) {
      cout << (i - N + 1) << " " << (a[i - N + 1] ^ b[0]) << el;
    }
  }

  return (0);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vll = vector<ll>;
template <typename T> using uset = unordered_set<T>;
template <typename T1, typename T2> using umap = unordered_map<T1, T2>;

constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\n';
constexpr int mod = 1000000007;

template <typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template <typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

vint Z_algorithm(vint &W) {
  int i = 1, j = 0;
  vint Z(W.size());
  Z[0] = W.size();
  while (i < W.size()) {
    while (i + j < W.size() && W[i + j] == W[j])
      ++j;
    Z[i] = j;
    if (j == 0) {
      ++i;
      continue;
    }
    int k = 1;
    while (Z[k] + k < j)
      Z[i + k] = Z[k], ++k;
    i += k, j -= k;
  }
  return (Z);
}
int main() {
  int N;
  cin >> N;
  vint a(N), b(N);
  vint A(N), B(N);
  for (auto &v : a)
    cin >> v;
  for (auto &v : b)
    cin >> v;
  A = a, B = b;
  for (int i = 0; i < N; i++) {
    if (i > 0)
      A[i - 1] ^= A[i];
  }
  A[N - 1] ^= a[0];
  for (int i = 0; i < N; i++) {
    if (i > 0)
      B[i - 1] ^= B[i];
  }
  B.pop_back();

  copy(begin(A), end(A), back_inserter(B));
  copy(begin(A), end(A), back_inserter(B));

  vint ans = Z_algorithm(B);
  int cnt = 0;
  for (int i = N - 1; i < 2 * N - 1; i++) {
    if (ans[i] >= N - 1) {
      cout << (i - N + 1) << " " << (a[i - N + 1] ^ b[0]) << el;
    }
  }

  return (0);
}