Language: C++

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#define mod 1000000007
#define N 300010
using namespace std;
set<int>S;
set<int>::iterator it;
char s1[2100], s2[2100];
int next[2100][2];
int main()
{
    int l, p1, p2, ans, cnt1, cnt2, i, j, t;
    scanf("%s%s", s1, s2);
    l = strlen(s1);
    p1 = p2 = 0;
    for(i = 0; i < l; ++i)
    {
        if(s1[i] == '1')
            p1 = 1;
        if(s2[i] == '1')
            p2 = 1;
    }
    if(p1 && !p2)
    {
        printf("-1\n");
        return 0;
    }
    for(i = 0; i < l; ++i)
    {
        if(s2[i] == '1')
            S.insert(i);
    }
    for(i = 0; i < l; ++i)
    {
        it = S.lower_bound(i);
        if(it == S.end())
        {
            it = S.begin();
            next[i][0] = *it + l - i;
        }else
        {
            next[i][0] = *it - i;
        }
        it = S.upper_bound(i);
        if(it == S.begin())
        {
            it = S.end();
            next[i][1] = i + l - *it;
        }else
        {
            it--;
            next[i][1] = i - *it;
        }
    }

    ans = 1000000000;
    for(i = 0; i < l; ++i)
    {
        cnt1 = i;
        cnt2 = l - i;
        for(j = 0; j < l; ++j)
        {
            t = j + i;
            if(t >= l) t -= l;
            if(s1[t] != s2[j])
            {
                if(next[j][0] > i)
                {
                    cnt1 = max(cnt1, min(2 * next[j][0] - i, 2 * next[j][1] + i));
                }
            }
        }

        for(j = 0; j < l; ++j)
        {
            t = j + i;
            if(t >= l) t -= l;
            if(s1[t] != s2[j])
            {
                if(next[t][1] > l - i)
                {
                    cnt2 = max(cnt2, min(2 * next[j][0] - (l - i), 2 * next[j][1] + (l - i)));
                }
            }
        }

        for(j = 0; j < l; ++j)
        {
            t = j + i;
            if(t >= l) t -= l;
            if(s1[t] != s2[j])
            {
                cnt1++;
                cnt2++;
            }
        }
        ans = min(ans, cnt1);
        ans = min(ans, cnt2);
    }
    printf("%d\n", ans);
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:42:13: error: reference to 'next' is ambiguous
   42 |             next[i][0] = *it + l - i;
      |             ^~~~
In file included from /usr/include/c++/14/string:47,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:5: note:                 'int next [2100][2]'
   12 | int next[2100][2];
      |     ^~~~
a.cc:45:13: error: reference to 'next' is ambiguous
   45 |             next[i][0] = *it - i;
      |             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:5: note:                 'int next [2100][2]'
   12 | int next[2100][2];
      |     ^~~~
a.cc:51:13: error: reference to 'next' is ambiguous
   51 |             next[i][1] = i + l - *it;
      |             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:5: note:                 'int next [2100][2]'
   12 | int next[2100][2];
      |     ^~~~
a.cc:55:13: error: reference to 'next' is ambiguous
   55 |             next[i][1] = i - *it;
      |             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:5: note:                 'int next [2100][2]'
   12 | int next[2100][2];
      |     ^~~~
a.cc:70:20: error: reference to 'next' is ambiguous
   70 |                 if(next[j][0] > i)
      |                    ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:5: note:                 'int next [2100][2]'
   12 | int next[2100][2];
      |     ^~~~
a.cc:72:46: error: reference to 'next' is ambiguous
   72 |                     cnt1 = max(cnt1, min(2 * next[j][0] - i, 2 * next[j][1] + i));
      |                                              ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:5: note:                 'int next [2100][2]'
   12 | int next[2100][2];
      |     ^~~~
a.cc:72:66: error: reference to 'next' is ambiguous
   72 |                     cnt1 = max(cnt1, min(2 * next[j][0] - i, 2 * next[j][1] + i));
      |                                                                  ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:5: note:                 'int next [2100][2]'
   12 | int next[2100][2];
      |     ^~~~
a.cc:83:20: error: reference to 'next' is ambiguous
   83 |                 if(next[t][1] > l - i)
      |                    ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:5: note:                 'int next [2100][2]'
   12 | int next[2100][2];
      |     ^~~~
a.cc:85:46: error: reference to 'next' is ambiguous
   85 |                     cnt2 = max(cnt2, min(2 * next[j][0] - (l - i), 2 * next[j][1] + (l - i)));
      |                                              ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:5: note:                 'int next [2100][2]'
   12 | int next[2100][2];
      |     ^~~~
a.cc:85:72: error: reference to 'next' is ambiguous
   85 |                     cnt2 = max(cnt2, min(2 * next[j][0] - (l - i), 2 * next[j][1] + (l - i)));
      |                                                                        ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:5: note:                 'int next [2100][2]'
   12 | int next[2100][2];
      |     ^~~~