Problem p03620 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int dist(int a, int n) { return min((a + n + n) % n, (n + n + n - a) % n); }
int main() {
  string a, b;
  cin >> a >> b;
  int n = a.size();
  int ok = 0;
  int oka = 1;
  for (int i = 0; i < n; i++) {
    if (b[i] == '1')
      ok = 1;
    if (a[i] != b[i])
      oka = 0;
  }

  if (!ok) {
    if (oka) {
      cout << 0 << endl;
      return 0;
    }
    cout << -1 << endl;
    return 0;
  }
  map<int, int> vis;
  int nvis = 0;
  for (int i = 0; i < n; i++) {
    vis[i] = 0;
    if (a[i] == '0') {
      vis[i] = 1000000;
      nvis++;
    }
  }
  int e = 0;
  int ans = 1000000;
  vector<int> oks, oke;
  for (int s = 0; s < n; s++) {
    // using [s,e) ok
    while (nvis < n || e <= s) {
      for (int i = 0; i < n; i++) {
        nvis -= (vis[(i + e) % n] > 0);
        vis[(i + e) % n] += (b[i] == '1');
        nvis += (vis[(i + e) % n] > 0);
      }
      e++;
    }
    for (int e2 = e; e2 <= s + n + 2; e2++) {
      oks.push_back(s);
      oke.push_back(e2 - 1);
    }
    // cout << s << " " << e << " " << p << endl;
    for (int i = 0; i < n; i++) {
      nvis -= (vis[(i + s) % n] > 0);
      vis[(i + s) % n] -= (b[i] == '1');
      nvis += (vis[(i + s) % n] > 0);
    }
  }
  int diff[n];
  for (int s = 0; s < n; s++) {
    diff[s] = 0;
    for (int i = 0; i < n; i++) {
      if (a[(i + s) % n] != b[i])
        diff[s]++;
    }
  }

  for (int c = 0; c < oks.size(); c++) {
    int zs = oks[c];
    int ze = oke[c];
    // cout << s << " " << ze << endl;
    for (int z2 = 0; z2 < n; z2++) {
      int p = dist(ze % n, n) + (ze - zs) + dist(zs - z2, n) + diff[z2 % n];
      int q = dist(zs % n, n) + (ze - zs) + dist(ze - z2, n) + diff[z2 % n];
      ans = min(ans, min(p, q));
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int dist(int a, int n) { return min((a + n + n) % n, (n + n + n - a) % n); }
int main() {
  string a, b;
  cin >> a >> b;
  int n = a.size();
  int ok = 0;
  int oka = 1;
  for (int i = 0; i < n; i++) {
    if (b[i] == '1')
      ok = 1;
    if (a[i] != b[i])
      oka = 0;
  }

  if (!ok) {
    if (oka) {
      cout << 0 << endl;
      return 0;
    }
    cout << -1 << endl;
    return 0;
  }
  map<int, int> vis;
  int nvis = 0;
  for (int i = 0; i < n; i++) {
    vis[i] = 0;
    if (a[i] == '0') {
      vis[i] = 1000000;
      nvis++;
    }
  }
  int e = 0;
  int ans = 1000000;
  vector<int> oks, oke;
  for (int s = 0; s < n; s++) {
    // using [s,e) ok
    while (nvis < n || e <= s) {
      for (int i = 0; i < n; i++) {
        nvis -= (vis[(i + e) % n] > 0);
        vis[(i + e) % n] += (b[i] == '1');
        nvis += (vis[(i + e) % n] > 0);
      }
      e++;
    }
    /*for(int e2 = e; e2 <= s + n + 2; e2++){
            oks.push_back(s);
            oke.push_back(e2-1);
    }*/
    oks.push_back(s);
    oke.push_back(e - 1);
    // cout << s << " " << e << " " << p << endl;
    for (int i = 0; i < n; i++) {
      nvis -= (vis[(i + s) % n] > 0);
      vis[(i + s) % n] -= (b[i] == '1');
      nvis += (vis[(i + s) % n] > 0);
    }
  }
  int diff[n];
  for (int s = 0; s < n; s++) {
    diff[s] = 0;
    for (int i = 0; i < n; i++) {
      if (a[(i + s) % n] != b[i])
        diff[s]++;
    }
  }

  for (int c = 0; c < oks.size(); c++) {
    int zs = oks[c];
    int ze = oke[c];
    // cout << s << " " << ze << endl;
    for (int z2 = 0; z2 < n; z2++) {
      int p = dist(ze % n, n) + (ze - zs) + dist(zs - z2, n) + diff[z2 % n];
      int q = dist(zs % n, n) + (ze - zs) + dist(ze - z2, n) + diff[z2 % n];
      ans = min(ans, min(p, q));
    }
  }
  cout << ans << endl;
}