Problem p03620 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define fs first
#define sc second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
typedef vector<pii> vpii;
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    ost;

const int N = 2100;
char a[N], b[N];
int nl[N], nr[N], wl[N], wr[N];

int main() {
  int i, j, k, kl, kr, n, d, diff, ans;
  scanf("%s%s", a, b);
  n = strlen(a);
  for (i = 0; i < n && a[i] == '0'; i++)
    ;
  for (j = 0; j < n && b[j] == '0'; j++)
    ;
  if (j == n) {
    printf("%d\n", i == n ? 0 : -1);
    return 0;
  }
  for (d = k = 0; k < n; k++) {
    i = (j + k) % n;
    if (b[i] == '1')
      d = 0;
    else
      d++;
    nl[i] = d;
  }
  for (d = k = 0; k < n; k++) {
    i = (j + n - k) % n;
    if (b[i] == '1')
      d = 0;
    else
      d++;
    nr[i] = d;
  }
  ans = 2 * n;
  for (d = 0; d < n; d++) {
    for (i = 0; i < n; i++)
      wl[i] = wr[i] = 0;
    diff = 0;
    for (i = 0; i < n; i++) {
      j = (d + i) % n;
      if (b[i] == a[j])
        continue;
      diff++;
      if (nl[j] > d)
        wl[nl[j] - d - 1] = max(wl[nl[j] - d - 1], nr[j]);
      if (nr[j] > n - d)
        wr[nr[j] - d - 1] = max(wr[nr[j] - n + d - 1], nl[j]);
    }
    ans = min(ans, diff + n + min(d, n - d));
    for (kl = kr = 0, i = n - 1; i >= 0; i--) {
      kl = max(kl, wl[i]);
      kr = max(kr, wr[i]);
      ans = min(ans, diff + d + 2 * i + 2 * kl);
      ans = min(ans, diff + n - d + 2 * i + 2 * kr);
    }
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define fs first
#define sc second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
typedef vector<pii> vpii;
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    ost;

const int N = 2100;
char a[N], b[N];
int nl[N], nr[N], wl[N], wr[N];

int main() {
  int i, j, k, kl, kr, n, d, diff, ans;
  scanf("%s%s", a, b);
  n = strlen(a);
  for (i = 0; i < n && a[i] == '0'; i++)
    ;
  for (j = 0; j < n && b[j] == '0'; j++)
    ;
  if (j == n) {
    printf("%d\n", i == n ? 0 : -1);
    return 0;
  }
  for (d = k = 0; k < n; k++) {
    i = (j + k) % n;
    if (b[i] == '1')
      d = 0;
    else
      d++;
    nl[i] = d;
  }
  for (d = k = 0; k < n; k++) {
    i = (j + n - k) % n;
    if (b[i] == '1')
      d = 0;
    else
      d++;
    nr[i] = d;
  }
  ans = 2 * n;
  for (d = 0; d < n; d++) {
    for (i = 0; i < n; i++)
      wl[i] = wr[i] = 0;
    diff = 0;
    for (i = 0; i < n; i++) {
      j = (d + i) % n;
      if (b[i] == a[j])
        continue;
      diff++;
      if (nl[j] > d)
        wl[nl[j] - d - 1] = max(wl[nl[j] - d - 1], nr[j]);
      if (nr[j] > n - d)
        wr[nr[j] - n + d - 1] = max(wr[nr[j] - n + d - 1], nl[j]);
    }
    ans = min(ans, diff + n + min(d, n - d));
    for (kl = kr = 0, i = n - 1; i >= 0; i--) {
      kl = max(kl, wl[i]);
      kr = max(kr, wr[i]);
      ans = min(ans, diff + d + 2 * i + 2 * kl);
      ans = min(ans, diff + n - d + 2 * i + 2 * kr);
    }
  }
  printf("%d\n", ans);
  return 0;
}