Problem p02816 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, M = 2e6;
const int NB = 30;
typedef long long ll;
const ll mod = (1e9 + 7);
int n, a[N], b[N], cnt = 1, g[M][2], fail[M], val[N], track[N][NB];
void newtrie() {
  cnt = 0;
  g[0][0] = g[0][1] = val[0] = -1;
}
int newnode() {
  ++cnt;
  g[cnt][0] = g[cnt][1] = val[cnt] = -1;
  return cnt;
}
void addstr(const string &s, int vval) {
  int cur = 0;
  for (char x : s) {
    int lnk = (x == '0' ? 0 : 1);
    if (g[cur][lnk] == -1)
      g[cur][lnk] = newnode();
    cur = g[cur][lnk];
  }
  val[cur] = vval;
}
void mklink() {
  queue<int> q;
  for (int i = 0; i < 2; ++i)
    if (g[0][i] != -1) {
      fail[g[0][i]] = 0;
      q.push(g[0][i]);
    }
  while (!q.empty()) {
    int s = q.front();
    q.pop();
    for (int i = 0; i < 2; ++i)
      if (g[s][i] != -1) {
        int state = fail[s];
        while (state > 0 && g[state][i] == -1)
          state = fail[state];
        state = (g[state][i] == -1 ? state : g[state][i]);
        fail[g[s][i]] = state;
        q.push(g[s][i]);
      }
  }
}
void aho(string text, vector<int> &x0, vector<int> &x1) {
  x0.clear();
  x1.clear();
  for (int i = 0; i < n - 1; ++i)
    text += text[i];
  int cur = 0, i = 0;
  for (char x : text) {
    ++i;
    int v = (x == '0' ? 0 : 1);
    while (cur > 0 && g[cur][v] == -1)
      cur = fail[cur];
    cur = (g[cur][v] == -1 ? cur : g[cur][v]);
    if (val[cur] == 0)
      x0.push_back(i - n);
    if (val[cur] == 1)
      x1.push_back(i - n);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  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 < n; ++i)
    fill(track[i], track[i] + NB, -1);
  for (int k = 0; k < NB; ++k) {
    string A, B, nB;
    for (int i = 0; i < n; ++i) {
      A += ((a[i] & (1 << k)) ? '1' : '0');
      B += ((b[i] & (1 << k)) ? '1' : '0');
      nB += ((b[i] & (1 << k)) ? '0' : '1');
    }
    newtrie();
    addstr(B, 0);
    addstr(nB, 1);
    mklink();
    vector<int> x0, x1;
    aho(A, x0, x1);
    for (int x : x0)
      track[x][k] = 0;
    for (int x : x1)
      track[x][k] = 1;
  }
  for (int k = 0; k < n; ++k) {
    bool emotional = true;
    for (int j = 0; j < NB; ++j)
      if (track[k][j] == -1) {
        emotional = false;
        break;
      }
    if (emotional) {
      int x = 0;
      for (int j = 0; j < NB; ++j)
        if (track[k][j])
          x |= (1 << j);
      cout << k << " " << x << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10, M = 2e6;
const int NB = 31;
typedef long long ll;
const ll mod = (1e9 + 7);
int n, a[N], b[N], cnt = 1, g[M][2], fail[M], val[N], track[N][NB];
void newtrie() {
  cnt = 0;
  g[0][0] = g[0][1] = val[0] = -1;
}
int newnode() {
  ++cnt;
  g[cnt][0] = g[cnt][1] = val[cnt] = -1;
  return cnt;
}
void addstr(const string &s, int vval) {
  int cur = 0;
  for (char x : s) {
    int lnk = (x == '0' ? 0 : 1);
    if (g[cur][lnk] == -1)
      g[cur][lnk] = newnode();
    cur = g[cur][lnk];
  }
  val[cur] = vval;
}
void mklink() {
  queue<int> q;
  for (int i = 0; i < 2; ++i)
    if (g[0][i] != -1) {
      fail[g[0][i]] = 0;
      q.push(g[0][i]);
    }
  while (!q.empty()) {
    int s = q.front();
    q.pop();
    for (int i = 0; i < 2; ++i)
      if (g[s][i] != -1) {
        int state = fail[s];
        while (state > 0 && g[state][i] == -1)
          state = fail[state];
        state = (g[state][i] == -1 ? state : g[state][i]);
        fail[g[s][i]] = state;
        q.push(g[s][i]);
      }
  }
}
void aho(string text, vector<int> &x0, vector<int> &x1) {
  x0.clear();
  x1.clear();
  for (int i = 0; i < n - 1; ++i)
    text += text[i];
  int cur = 0, i = 0;
  for (char x : text) {
    ++i;
    int v = (x == '0' ? 0 : 1);
    while (cur > 0 && g[cur][v] == -1)
      cur = fail[cur];
    cur = (g[cur][v] == -1 ? cur : g[cur][v]);
    if (val[cur] == 0)
      x0.push_back(i - n);
    if (val[cur] == 1)
      x1.push_back(i - n);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  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 < n; ++i)
    fill(track[i], track[i] + NB, -1);
  for (int k = 0; k < NB; ++k) {
    string A, B, nB;
    for (int i = 0; i < n; ++i) {
      A += ((a[i] & (1 << k)) ? '1' : '0');
      B += ((b[i] & (1 << k)) ? '1' : '0');
      nB += ((b[i] & (1 << k)) ? '0' : '1');
    }
    newtrie();
    addstr(B, 0);
    addstr(nB, 1);
    mklink();
    vector<int> x0, x1;
    aho(A, x0, x1);
    for (int x : x0)
      track[x][k] = 0;
    for (int x : x1)
      track[x][k] = 1;
  }
  for (int k = 0; k < n; ++k) {
    bool emotional = true;
    for (int j = 0; j < NB; ++j)
      if (track[k][j] == -1) {
        emotional = false;
        break;
      }
    if (emotional) {
      int x = 0;
      for (int j = 0; j < NB; ++j)
        if (track[k][j])
          x |= (1 << j);
      cout << k << " " << x << endl;
    }
  }
}