Language: C++

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

string s, t;
int n, ans = 2e9, need[2005], can[2005];

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

    cin >> s >> t; n = s.size();
    int was1 = 0;
    for (int i = 0; i < n; ++i)
        was1 |= t[i] - 48;
    if (!was1) { cout << -1 << endl; return 0; }
    for (int i = 0; i < n; ++i) { /// left
        for (int j = 0; j < n; ++j)
            need[j] = can[j] = 0;
        int cur = i;
        vector<pii> v;
        for (int j = 0; j < n; ++j) {
            if (t[j] == '1') {
                int L = j;
                int R = (j + i) % n;
                if (L <= R)
                v.push_back({L, -1}), v.push_back({R, 1});
                else
                v.push_back({L, -1}) , v.push_back({n - 1, 1}) , v.push_back({0, -1}),v.push_back({R, 1}) ;
            }
        }
        sort(v.begin(), v.end());
        int balance = 0;
        int prv = 0;
        for (int i = 0; i < v.size(); ++i) {
            int prv_balance = balance;
            balance -= v[i].second;
            if (prv_balance == 0 && balance == 1)
                prv = v[i].first;
            else if (prv_balance == 1 && balance == 0) {
                for (int z = prv; z <= v[i].first; ++z)
                    can[z] = 1;
            }
        }
        string q = s.substr(i, n - i) + substr(0, i);
        int good = 1, to_change = 0;
        for (int i = 0; i < n; ++i) {
            if (q[i] != t[i]) {
                good &= can[i];
                to_change++;
            }
        }
        if (good)
            ans = min(ans, i + to_change);
    }
    for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j)
            need[j] = can[j] = 0;
        int cur = i;
        vector<pii> v;
        for (int j = 0; j < n; ++j) {
            if (t[j] == '1') {
                int R = j;
                int L = ((j - i) % n + n) % n;
                if (L <= R)
                v.push_back({L, -1}), v.push_back({R, 1});
                else
                v.push_back({L, -1}) , v.push_back({n - 1, 1}) , v.push_back({0, -1}),v.push_back({R, 1}) ;
            }
        }
        sort(v.begin(), v.end());
        int balance = 0;
        int prv = 0;
        for (int i = 0; i < v.size(); ++i) {
            int prv_balance = balance;
            balance -= v[i].second;
            if (prv_balance == 0 && balance == 1)
                prv = v[i].first;
            else if (prv_balance == 1 && balance == 0) {
                for (int z = prv; z <= v[i].first; ++z)
                    can[z] = 1;
            }
        }
        string q = s.substr(n - i, i) + substr(0, n - i);
        int good = 1, to_change = 0;
        for (int i = 0; i < n; ++i) {
            if (q[i] != t[i]) {
                good &= can[i];
                to_change++;
            }
        }
        if (good)
            ans = min(ans, i + to_change);
    }
    cout << ans << endl;

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:48:41: error: 'substr' was not declared in this scope; did you mean 'strstr'?
   48 |         string q = s.substr(i, n - i) + substr(0, i);
      |                                         ^~~~~~
      |                                         strstr
a.cc:87:41: error: 'substr' was not declared in this scope; did you mean 'strstr'?
   87 |         string q = s.substr(n - i, i) + substr(0, n - i);
      |                                         ^~~~~~
      |                                         strstr