Language: C++

#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")

#include <iostream>
#include <string>
#include <vector>
#include <cassert>

#define pb push_back
#define fi first
#define se second
#define all(v) v.begin(), v.end()

using namespace std;
using ll = int64_t;

struct MinShift {
  int left, right;
};

string A, B;
int N;
vector<int> sums;
vector<MinShift> min_shifts;
vector<int> lefts, rights;
vector<int> maxRight;

inline int doShift(int i, int shift) {
  i += shift;
  while (i < 0) i += N;
  while (i >= N) i -= N;
  return i;
}

inline int sign(int i) {
  if (i > 0) return 1;
  if (i < 0) return -1;
  return 0;
}

bool covered(int left, int right) {
  assert(left <= right);
  return (sums[right] - (left == 0 ? 0 : sums[left - 1])) > 0;
}

bool covered(int initialPos, int finalPos, int direction) {
  if (direction == 1) {
    if (finalPos <= initialPos) {
      return covered(initialPos, N - 1) || covered(0, finalPos);
    } else {
      return covered(initialPos, finalPos);
    }
  }

  if (direction == -1) {
    if (finalPos >= initialPos) {
      return covered(0, initialPos) || covered(finalPos, N - 1);
    } else {
      return covered(finalPos, initialPos);
    }
  }

  assert(direction == 0);
  assert(initialPos == finalPos);

  return false;
}

int getFirstLeft(int pos) {
  return lefts[pos];
}

int getFirstRight(int pos) {
  return rights[pos];
}

MinShift getMinShift(int initialPos, int finalPos, int direction) {
  if (direction == 1) {
    return MinShift{getFirstLeft(initialPos), getFirstRight(finalPos)};
  }

  if (direction == -1) {
    return MinShift{getFirstLeft(finalPos), getFirstRight(initialPos)};
  }

  assert(direction == 0);
  assert(initialPos == finalPos);

  return MinShift{getFirstLeft(initialPos), getFirstRight(initialPos)};
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> A >> B;
  N = A.size();

  sums.assign(N, 0);
  for (int i = 0; i < N; i++) {
    if (i > 0) sums[i] = sums[i - 1];
    sums[i] += (B[i] - '0');
  }

  lefts.assign(N, -1);
  rights.assign(N, -1);

  for (int i = 0; i < N; i++) {
    for (int shift = 0; shift <= N; shift++) {
      int j = doShift(i, -shift);
      if (B[j] == '1' && lefts[i] == -1) {
        lefts[i] = shift;
      }
      j = doShift(i, shift);
      if (B[j] == '1' && rights[i] == -1) {
        rights[i] = shift;
      }
    }
  }

  int result = -1;

  for (int shift = -N; shift <= N; shift++) {
    //cerr << "check: " << shift << endl;
    bool fail = false;
    min_shifts.clear();
    int direction = sign(shift);
    int baseResult = abs(shift);
    for (int finalPos = 0; finalPos < N; finalPos++) {
      int initialPos = doShift(finalPos, -shift);
      char targetChar = B[finalPos];
      char finalChar = A[initialPos];
      //cerr << "chars: " << initialPos << " " << finalPos << " " << targetChar << " " << finalChar << endl;
      if (targetChar != finalChar) {
        ++baseResult;
        if (covered(initialPos, finalPos, direction)) {
          //cerr << "covered: " << initialPos << " " << finalPos << endl;
        } else {
          min_shifts.push_back(getMinShift(initialPos, finalPos, direction));
          //cerr << "shift: " << initialPos << " " << finalPos << " " << min_shifts.back().left << " " << min_shifts.back().right << endl;
        }
      }
    }
    for (auto shift : min_shifts) {
      if (shift.left == -1 && shift.right == -1) {
        fail = true;
      }
    }
    if (!fail) {
      //cerr << "not fail" << endl;
      sort(all(min_shifts), [](MinShift lhs, MinShift rhs) {
        return lhs.left < rhs.left;
      });
      int M = min_shifts.size();
      maxRight.assign(M, 0);
      for (int i = M - 1; i >= 0; i--) {
        maxRight[i] = min_shifts[i].right;
        if (i + 1 < M)
          maxRight[i] = max(maxRight[i], maxRight[i + 1]);
      }
      int addition = 0;
      if (!min_shifts.empty()) {
        addition = 2 * N;
        for (int i = 0; i < M; i++) {
          int currentAddition = min_shifts[i].left;
          if (i + 1 < M)
            currentAddition += maxRight[i + 1];
          addition = min(addition, currentAddition);
        }
      }
      if (result == -1)
        result = baseResult + addition;
      else
        result = min(result, baseResult + addition);
      //cerr << "result: " << baseResult + addition << endl;
    }
  }

  cout << result << endl;

  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:151:7: error: 'sort' was not declared in this scope; did you mean 'short'?
  151 |       sort(all(min_shifts), [](MinShift lhs, MinShift rhs) {
      |       ^~~~
      |       short