Problem p02816 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define Endl endl
#define mp make_pair
#define ll long long
#define ull unsinged long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define over(A)                                                                \
  {                                                                            \
    cout << A << endl;                                                         \
    exit(0);                                                                   \
  }
#define all(A) A.begin(), A.end()
#define ceil(a, b) ((a - 1) / b + 1)
#define srand()                                                                \
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<int>(l, r)(rng)
template <typename T1, typename T2> inline void chmin(T1 &x, T2 b) {
  if (b < x)
    x = b;
}
template <typename T1, typename T2> inline void chmax(T1 &x, T2 b) {
  if (b > x)
    x = b;
}
const int inf = 1039074182;
using namespace std;
int n;
int a[200005];
int b[200005];
vector<bool> vec[200005];
bool pattern[200005];
bool c[400005];
int fail[200005];

void build(bool c[], int n) {
  int j = fail[0] = -1;
  for (int i = 1; i <= n; i++) {
    while (j >= 0 && c[i - 1] != c[j])
      j = fail[j];
    fail[i] = ++j;
  }
}

vector<pair<int, int>> solve(int bit) {
  bit = 1 << bit;
  for (int i = 0; i < n; i++) {
    pattern[i] = b[i] & bit;
  }
  for (int i = 0; i < n; i++) {
    c[i] = a[i] & bit;
  }
  for (int i = n; i < n + n; i++) {
    c[i] = c[i - n];
  }
  build(pattern, n);
  vector<pair<int, int>> res;
  for (int rev = 0; rev < 2; rev++) {
    for (int i = 0, j = 0; i < n + n; i++) {
      while (j >= 0 && c[i] != pattern[j])
        j = fail[j];
      j++;
      if (j == n)
        res.push_back(mp(i - n + 1, rev)), j = fail[j];
    }
    for (int i = 0; i < n + n; i++) {
      c[i] = c[i] ^ 1;
    }
  }
  return res;
}

int main() {
  freopen("input.txt", "r", stdin);
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < 30; i++) {
    for (auto x : solve(i)) {
      vec[x.first].push_back(x.second);
    }
  }
  for (int i = 0; i < n; i++) {
    if (vec[i].size() != 30)
      continue;
    int res = 0;
    for (int j = 0; j < 30; j++) {
      res |= (vec[i][j] << j);
    }
    cout << i << ' ' << res << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define Endl endl
#define mp make_pair
#define ll long long
#define ull unsinged long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define over(A)                                                                \
  {                                                                            \
    cout << A << endl;                                                         \
    exit(0);                                                                   \
  }
#define all(A) A.begin(), A.end()
#define ceil(a, b) ((a - 1) / b + 1)
#define srand()                                                                \
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<int>(l, r)(rng)
template <typename T1, typename T2> inline void chmin(T1 &x, T2 b) {
  if (b < x)
    x = b;
}
template <typename T1, typename T2> inline void chmax(T1 &x, T2 b) {
  if (b > x)
    x = b;
}
const int inf = 1039074182;
using namespace std;
int n;
int a[200005];
int b[200005];
vector<bool> vec[200005];
bool pattern[200005];
bool c[400005];
int fail[200005];

void build(bool c[], int n) {
  int j = fail[0] = -1;
  for (int i = 1; i <= n; i++) {
    while (j >= 0 && c[i - 1] != c[j])
      j = fail[j];
    fail[i] = ++j;
  }
}

vector<pair<int, int>> solve(int bit) {
  bit = 1 << bit;
  for (int i = 0; i < n; i++) {
    pattern[i] = b[i] & bit;
  }
  for (int i = 0; i < n; i++) {
    c[i] = a[i] & bit;
  }
  for (int i = n; i < n + n; i++) {
    c[i] = c[i - n];
  }
  build(pattern, n);
  vector<pair<int, int>> res;
  for (int rev = 0; rev < 2; rev++) {
    for (int i = 0, j = 0; i < n + n; i++) {
      while (j >= 0 && c[i] != pattern[j])
        j = fail[j];
      j++;
      if (j == n)
        res.push_back(mp(i - n + 1, rev)), j = fail[j];
    }
    for (int i = 0; i < n + n; i++) {
      c[i] = c[i] ^ 1;
    }
  }
  return res;
}

int main() {
  //	freopen("input.txt","r",stdin);
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < 30; i++) {
    for (auto x : solve(i)) {
      vec[x.first].push_back(x.second);
    }
  }
  for (int i = 0; i < n; i++) {
    if (vec[i].size() != 30)
      continue;
    int res = 0;
    for (int j = 0; j < 30; j++) {
      res |= (vec[i][j] << j);
    }
    cout << i << ' ' << res << endl;
  }
  return 0;
}