Problem p03620 (C++)

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

char a[2005], b[2005];
int n;

vector<pair<int, int>> solsRL;

vector<int> occs[2], maxRL;

int getRight(int ind) {
  if (occs[1].empty())
    return -1;
  vector<int>::iterator it = lower_bound(occs[1].begin(), occs[1].end(), ind);
  if (it == occs[1].end())
    return n - ind + (*occs[1].begin());
  return (*it) - ind;
}
int getLeft(int ind) {
  if (occs[1].empty())
    return -1;
  vector<int>::iterator it = lower_bound(occs[1].begin(), occs[1].end(), ind);
  if (it == occs[1].begin())
    return ind + 1 + (n - 1) - *occs[1].rbegin();
  --it;
  return ind - (*it);
}
void process(vector<pair<int, int>> &v, vector<int> &m) {
  m[int(v.size()) - 1] = v.back().second;
  for (int i = int(v.size()) - 2; i >= 0; i--)
    m[i] = max(v[i].second, m[i + 1]);
}

int main() {
  ios::sync_with_stdio(false);

  scanf("%s", a);
  scanf("%s", b);
  n = strlen(a);

  for (int i = 0; i < n; i++)
    occs[b[i] - '0'].push_back(i);

  int best = -1;
  for (int startPos = 0; startPos < n; startPos++) {

    int ans = 0;
    solsRL.clear();
    maxRL.clear();
    for (int i = 0; i < n; i++) {
      char c = a[(startPos + i) % n];
      char d = b[i];
      if (c == d)
        continue;
      ans++;
      int left = getLeft((startPos + i) % n);
      int right = getRight((startPos + i) % n);
      if (left == -1 && right == -1) {
        cout << -1 << endl;
        return 0;
      }

      solsRL.push_back(make_pair(right, left));
      maxRL.push_back(0);
    }
    sort(solsRL.begin(), solsRL.end());
    process(solsRL, maxRL);

    // hanet7arrak shwaya left we shwaya right
    for (int moveRight = 0; moveRight < n; moveRight++) {
      int sz = solsRL.size();

      int ind = lower_bound(solsRL.begin(), solsRL.end(),
                            make_pair(moveRight + 1, -1)) -
                solsRL.begin();
      int moveLeft = ind >= sz ? 0 : maxRL[ind];

      int tmp = 2 * moveRight + moveLeft + ans;
      tmp += abs(moveLeft - startPos);

      if (best == -1 || tmp < best)
        best = tmp;

      tmp = 2 * moveLeft + moveRight + ans;
      tmp += abs(moveRight - ((n - startPos) % n));
      if (best == -1 || tmp < best)
        best = tmp;
    }
  }
  cout << best << endl;

  return 0;
}


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

char a[2005], b[2005];
int n;

vector<pair<int, int>> solsRL;

vector<int> occs[2], maxRL;

int getRight(int ind) {
  if (occs[1].empty())
    return -1;
  vector<int>::iterator it = lower_bound(occs[1].begin(), occs[1].end(), ind);
  if (it == occs[1].end())
    return n - ind + (*occs[1].begin());
  return (*it) - ind;
}
int getLeft(int ind) {
  if (occs[1].empty())
    return -1;
  vector<int>::iterator it = lower_bound(occs[1].begin(), occs[1].end(), ind);
  if (it == occs[1].begin())
    return ind + 1 + (n - 1) - *occs[1].rbegin();
  --it;
  return ind - (*it);
}
void process(vector<pair<int, int>> &v, vector<int> &m) {
  if (v.empty())
    return;
  m[int(v.size()) - 1] = v.back().second;
  for (int i = int(v.size()) - 2; i >= 0; i--)
    m[i] = max(v[i].second, m[i + 1]);
}

int main() {
  ios::sync_with_stdio(false);

  scanf("%s", a);
  scanf("%s", b);
  n = strlen(a);

  for (int i = 0; i < n; i++)
    occs[b[i] - '0'].push_back(i);

  int best = -1;
  for (int startPos = 0; startPos < n; startPos++) {

    int ans = 0;
    solsRL.clear();
    maxRL.clear();
    for (int i = 0; i < n; i++) {
      char c = a[(startPos + i) % n];
      char d = b[i];
      if (c == d)
        continue;
      ans++;
      int left = getLeft((startPos + i) % n);
      int right = getRight((startPos + i) % n);
      if (left == -1 && right == -1) {
        cout << -1 << endl;
        return 0;
      }

      solsRL.push_back(make_pair(right, left));
      maxRL.push_back(0);
    }
    sort(solsRL.begin(), solsRL.end());
    process(solsRL, maxRL);

    // hanet7arrak shwaya left we shwaya right
    for (int moveRight = 0; moveRight < n; moveRight++) {
      int sz = solsRL.size();

      int ind = lower_bound(solsRL.begin(), solsRL.end(),
                            make_pair(moveRight + 1, -1)) -
                solsRL.begin();
      int moveLeft = ind >= sz ? 0 : maxRL[ind];

      int tmp = 2 * moveRight + moveLeft + ans;
      tmp += abs(moveLeft - startPos);

      if (best == -1 || tmp < best)
        best = tmp;

      tmp = 2 * moveLeft + moveRight + ans;
      tmp += abs(moveRight - ((n - startPos) % n));
      if (best == -1 || tmp < best)
        best = tmp;
    }
  }
  cout << best << endl;

  return 0;
}