Problem p03620 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

int BIT[2001];

void add(int A, int B) {
  A++;
  while (A > 0) {
    BIT[A] = max(BIT[A], B);
    A -= A & -A;
  }
}

int query(int A) {
  A++;
  int kotae = 0;
  while (A <= 2000) {
    kotae = max(BIT[A], kotae);
    A += A & -A;
  }
  return kotae;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string A, B;
  cin >> A >> B;

  int N = A.size();
  int L[2000], R[2000];
  rep(i, N) L[i] = 1e9;
  rep(i, N) R[i] = 1e9;
  rep(i, N) if (B[i] == '1') L[i] = 0;
  rep(i, N) if (B[i] == '1') R[i] = 0;
  rep(i, 2 * N) L[(i + 1) % N] = min(L[(i + 1) % N], L[i % N] + 1);
  for (int i = 2 * N; i > 0; i--)
    R[(i - 1) % N] = min(R[(i - 1) % N], R[i % N] + 1);

  if (L[0] > 1e8) {
    int zero = 1;
    rep(i, N) if (A[i] == '1') zero = 0;
    if (zero)
      co(0);
    else
      co(-1);
  }

  A += A;
  int kotae = 1e9;
  rep(i, N) {
    int m = 0;
    memset(BIT, 0, sizeof(BIT));
    rep(j, N) {
      if (A[i + j] != B[j]) {
        if (L[(i + j) % N])
          add(L[(i + j) % N] - 1, R[(i + j) % N]);
        m++;
      }
    }
    rep(j, N) {
      int k = query(j);
      kotae = min(kotae, m + j * 2 + k + min(abs(i + k), abs(N - i - k)));
      kotae = min(kotae, m + j + 2 * k + min(abs(i - j), abs(N - i + j)));
    }
  }
  co(kotae);

  Would you please return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

int BIT[2001];

void add(int A, int B) {
  A++;
  while (A > 0) {
    BIT[A] = max(BIT[A], B);
    A -= A & -A;
  }
}

int query(int A) {
  A++;
  int kotae = 0;
  while (A <= 2000) {
    kotae = max(BIT[A], kotae);
    A += A & -A;
  }
  return kotae;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string A, B;
  cin >> A >> B;

  int N = A.size();
  int L[2000], R[2000];
  rep(i, N) L[i] = 1e9;
  rep(i, N) R[i] = 1e9;
  rep(i, N) if (B[i] == '1') L[i] = 0;
  rep(i, N) if (B[i] == '1') R[i] = 0;
  rep(i, 2 * N) L[(i + 1) % N] = min(L[(i + 1) % N], L[i % N] + 1);
  for (int i = 2 * N; i > 0; i--)
    R[(i - 1) % N] = min(R[(i - 1) % N], R[i % N] + 1);

  if (L[0] > 1e8) {
    int zero = 1;
    rep(i, N) if (A[i] == '1') zero = 0;
    if (zero)
      co(0);
    else
      co(-1);
    return 0;
  }

  A += A;
  int kotae = 1e9;
  rep(i, N) {
    int m = 0;
    memset(BIT, 0, sizeof(BIT));
    rep(j, N) {
      if (A[i + j] != B[j]) {
        if (L[(i + j) % N])
          add(L[(i + j) % N] - 1, R[(i + j) % N]);
        m++;
      }
    }
    rep(j, N) {
      int k = query(j);
      kotae = min(kotae, m + j * 2 + k + min(abs(i + k), abs(N - i - k)));
      kotae = min(kotae, m + j + 2 * k + min(abs(i - j), abs(N - i + j)));
    }
  }
  co(kotae);

  Would you please return 0;
}