Problem p03620 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  freopen("input.txt", "rt", stdin);
#endif

  string a, b;
  cin >> a >> b;
  swap(a, b);
  int n = a.size();
  if (a == string(n, '0') && b != string(n, '0')) {
    cout << -1 << '\n';
    return 0;
  }

  vi cl(n), cr(n);
  forn(i, n) {
    for (int j = i; a[j] != '1'; j = (j + n - 1) % n) {
      ++cl[i];
    }
    for (int j = i; a[j] != '1'; j = (j + 1) % n) {
      ++cr[i];
    }
  }

  int ans = 1e9;
  forn(d, n) {
    string ta = a;
    rotate(ta.begin(), ta.begin() + d, ta.end());
    int changes = 0;
    pii p;
    vi h(n);
    forn(i, n) if (b[i] != ta[i]) uax(h[cl[i]], cr[i]), ++changes;
    int ch = 0, cost = min(n + d, n + (n - d) % n);
    ford(j, n) {
      forn(k, 2) forn(l, 2) {
        int fin1 = k ? ch : -j, fin2 = l ? d : d - n;
        int res = j + ch + (k ? j : ch) + abs(fin1 - fin2);
        uin(cost, res);
      }
      uax(ch, h[j]);
    }
    uin(ans, changes + cost);
  }
  cout << ans << '\n';

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  freopen("input.txt", "rt", stdin);
#endif

  string a, b;
  cin >> a >> b;
  swap(a, b);
  int n = a.size();
  if (a == string(n, '0')) {
    cout << (b == string(n, '0') ? 0 : -1) << '\n';
    return 0;
  }

  vi cl(n), cr(n);
  forn(i, n) {
    for (int j = i; a[j] != '1'; j = (j + n - 1) % n) {
      ++cl[i];
    }
    for (int j = i; a[j] != '1'; j = (j + 1) % n) {
      ++cr[i];
    }
  }

  int ans = 1e9;
  forn(d, n) {
    string ta = a;
    rotate(ta.begin(), ta.begin() + d, ta.end());
    int changes = 0;
    pii p;
    vi h(n);
    forn(i, n) if (b[i] != ta[i]) uax(h[cl[i]], cr[i]), ++changes;
    int ch = 0, cost = min(n + d, n + (n - d) % n);
    ford(j, n) {
      forn(k, 2) forn(l, 2) {
        int fin1 = k ? ch : -j, fin2 = l ? d : d - n;
        int res = j + ch + (k ? j : ch) + abs(fin1 - fin2);
        uin(cost, res);
      }
      uax(ch, h[j]);
    }
    uin(ans, changes + cost);
  }
  cout << ans << '\n';

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}