Problem p03620 (C++)

Buggy submission (status: Runtime Error):
/*

                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                    prayer
*/

// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define show1d(v)                                                              \
  rep(i, v.size()) cout << " " << v[i];                                        \
  cout << endl << endl;
#define show2d(v)                                                              \
  rep(i, v.size()) {                                                           \
    rep(j, v[i].size()) cout << " " << v[i][j];                                \
    cout << endl;                                                              \
  }                                                                            \
  cout << endl;
using namespace std;
// kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<int,int,int> tp;
// typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
// template<class C>constexpr int size(const C &c){return (int)c.size();}
// template<class T,size_t N> constexpr int size(const T
// (&xs)[N])noexcept{return (int)N;}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
signed main() {
  string s, t;
  cin >> s >> t;
  t += t + t;
  vi a;
  a.pb(-10000);
  rep(i, t.size()) if (t[i] == '1') a.pb(i);
  a.pb(t.size() + 10000);
  if (a.size() == 2) {
    if (s.find('1') == string::npos)
      cout << 0 << endl;
    else
      cout << -1 << endl;
    return 0;
  }
  int out = inf;
  rep(i, s.size() * 2) {
    vvi in;
    int co = 0;
    rep(j, s.size()) if (s[j] - t[i + j]) {
      co++;
      int L = i + j, R = s.size() + j;
      int r = lower_bound(all(a), i + j) - a.begin(), l = r - 1;
      r = a[r];
      l = a[l];
      if ((L <= l && l <= R) || (L <= r && r <= R))
        ;
      else
        in.pb({L - l, r - R});
    }
    int mi = inf, ma = 0;
    sort(all(in));
    rep(i, in.size()) rep(j, 2) assert(0 <= in[i][0] && in[i][1] <= 7000),
        assert(0 <= in[i][0] && in[i][1] <= 7000);
    for (int j = (int)in.size() - 1; j >= 0; j--) {
      cmin(mi, ma + in[j][0]);
      cmax(ma, in[j][1]);
    }
    if (mi == inf)
      mi = 0;
    cmin(mi, ma);
    cmin(out, co + 2 * mi + abs((int)s.size() - i));
  }
  cout << out << endl;
}


Fixed submission:
/*

                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                    prayer
*/

// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
#define show1d(v)                                                              \
  rep(i, v.size()) cout << " " << v[i];                                        \
  cout << endl << endl;
#define show2d(v)                                                              \
  rep(i, v.size()) {                                                           \
    rep(j, v[i].size()) cout << " " << v[i][j];                                \
    cout << endl;                                                              \
  }                                                                            \
  cout << endl;
using namespace std;
// kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
// #define mt make_tuple
// typedef tuple<int,int,int> tp;
// typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
// template<class C>constexpr int size(const C &c){return (int)c.size();}
// template<class T,size_t N> constexpr int size(const T
// (&xs)[N])noexcept{return (int)N;}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
signed main() {
  string s, t;
  cin >> s >> t;
  t += t + t;
  vi a;
  a.pb(-10000);
  rep(i, t.size()) if (t[i] == '1') a.pb(i);
  a.pb(t.size() + 10000);
  if (a.size() == 2) {
    if (s.find('1') == string::npos)
      cout << 0 << endl;
    else
      cout << -1 << endl;
    return 0;
  }
  int out = inf;
  rep(i, s.size() * 2) {
    vvi in;
    int co = 0;
    rep(j, s.size()) if (s[j] - t[i + j]) {
      co++;
      int L = min(i + j, (int)s.size() + j), R = max(i + j, (int)s.size() + j);
      int r = lower_bound(all(a), i + j) - a.begin(), l = r - 1;
      r = a[r];
      l = a[l];
      if ((L <= l && l <= R) || (L <= r && r <= R))
        ;
      else
        in.pb({L - l, r - R});
    }
    int mi = inf, ma = 0;
    sort(all(in));
    rep(i, in.size()) rep(j, 2) assert(0 <= in[i][0] && in[i][1] <= 7000),
        assert(0 <= in[i][0] && in[i][1] <= 7000);
    for (int j = (int)in.size() - 1; j >= 0; j--) {
      cmin(mi, ma + in[j][0]);
      cmax(ma, in[j][1]);
    }
    if (mi == inf)
      mi = 0;
    cmin(mi, ma);
    cmin(out, co + 2 * mi + abs((int)s.size() - i));
  }
  cout << out << endl;
}