Problem p03620 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x)                                                                  \
  {                                                                            \
    freopen(#x ".in", "r", stdin);                                             \
    freopen(#x ".out", "w", stdout);                                           \
  }
#define Edg                                                                    \
  int M = 0, fst[SZ], vb[SZ], nxt[SZ];                                         \
  void ad_de(int a, int b) {                                                   \
    ++M;                                                                       \
    nxt[M] = fst[a];                                                           \
    fst[a] = M;                                                                \
    vb[M] = b;                                                                 \
  }                                                                            \
  void adde(int a, int b) {                                                    \
    ad_de(a, b);                                                               \
    ad_de(b, a);                                                               \
  }
#define Edgc                                                                   \
  int M = 0, fst[SZ], vb[SZ], nxt[SZ], vc[SZ];                                 \
  void ad_de(int a, int b, int c) {                                            \
    ++M;                                                                       \
    nxt[M] = fst[a];                                                           \
    fst[a] = M;                                                                \
    vb[M] = b;                                                                 \
    vc[M] = c;                                                                 \
  }                                                                            \
  void adde(int a, int b, int c) {                                             \
    ad_de(a, b, c);                                                            \
    ad_de(b, a, c);                                                            \
  }
#define es(x, e) (int e = fst[x]; e; e = nxt[e])
#define esb(x, e, b) (int e = fst[x], b = vb[e]; e; e = nxt[e], b = vb[e])
#define VIZ                                                                    \
  {                                                                            \
    printf("digraph G{\n");                                                    \
    for (int i = 1; i <= n; i++) for                 \
        es(i, e) printf("%d->%d;\n", i, vb[e]);                                \
    puts("}");                                                                 \
  }
#define VIZ2                                                                   \
  {                                                                            \
    printf("graph G{\n");                                                      \
    for (int i = 1; i <= n; i++) for                  \
        es(i, e) if (vb[e] >= i) printf("%d--%d;\n", i, vb[e]);                \
    puts("}");                                                                 \
  }
#define SZ 666666
char A[SZ], B[SZ];
int n, ans;
int mi[SZ], ma[SZ], mx[SZ];
void work() {
  for (int i = 0; i < n; ++i) {
    mi[i] = ma[i] = 0;
    while (B[(i + ma[i]) % n] != '1')
      ++ma[i];
    while (B[(i + mi[i] + n) % n] != '1')
      --mi[i];
  }
  for (int s = 0; s < n; ++s) {
    for (int r = 0; r < n; ++r)
      mx[r] = 0;
    int dif = 0;
    for (int r = 0; r < n; ++r)
      if (A[r] != B[(r + s) % n])
        mx[-mi[r]] = max(mx[-mi[r]], ma[r] - s), ++dif;
    int ly = 0, ca = 1e9;
    for (int r = n - 1; r >= 0; --r)
      ca = min(ca, ly + r), ly = max(ly, mx[r]);
    ans = min(ans, 2 * ca + s + dif);
  }
}
int main() {
  scanf("%s%s", A, B);
  n = strlen(A);
  ans = 1e9;
  bool ha = 0, hb = 0;
  for (int i = 0; i < n; ++i)
    if (A[i] == '1')
      ha = 1;
  for (int i = 0; i < n; ++i)
    if (B[i] == '1')
      hb = 1;
  if (!hb && ha) {
    puts("-1");
    return 0;
  }
  work();
  reverse(A, A + n);
  reverse(B, B + n);
  work();
  printf("%d\n", ans);
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x)                                                                  \
  {                                                                            \
    freopen(#x ".in", "r", stdin);                                             \
    freopen(#x ".out", "w", stdout);                                           \
  }
#define Edg                                                                    \
  int M = 0, fst[SZ], vb[SZ], nxt[SZ];                                         \
  void ad_de(int a, int b) {                                                   \
    ++M;                                                                       \
    nxt[M] = fst[a];                                                           \
    fst[a] = M;                                                                \
    vb[M] = b;                                                                 \
  }                                                                            \
  void adde(int a, int b) {                                                    \
    ad_de(a, b);                                                               \
    ad_de(b, a);                                                               \
  }
#define Edgc                                                                   \
  int M = 0, fst[SZ], vb[SZ], nxt[SZ], vc[SZ];                                 \
  void ad_de(int a, int b, int c) {                                            \
    ++M;                                                                       \
    nxt[M] = fst[a];                                                           \
    fst[a] = M;                                                                \
    vb[M] = b;                                                                 \
    vc[M] = c;                                                                 \
  }                                                                            \
  void adde(int a, int b, int c) {                                             \
    ad_de(a, b, c);                                                            \
    ad_de(b, a, c);                                                            \
  }
#define es(x, e) (int e = fst[x]; e; e = nxt[e])
#define esb(x, e, b) (int e = fst[x], b = vb[e]; e; e = nxt[e], b = vb[e])
#define VIZ                                                                    \
  {                                                                            \
    printf("digraph G{\n");                                                    \
    for (int i = 1; i <= n; i++) for                 \
        es(i, e) printf("%d->%d;\n", i, vb[e]);                                \
    puts("}");                                                                 \
  }
#define VIZ2                                                                   \
  {                                                                            \
    printf("graph G{\n");                                                      \
    for (int i = 1; i <= n; i++) for                  \
        es(i, e) if (vb[e] >= i) printf("%d--%d;\n", i, vb[e]);                \
    puts("}");                                                                 \
  }
#define SZ 666666
char A[SZ], B[SZ];
int n, ans;
int mi[SZ], ma[SZ], mx[SZ];
void work() {
  for (int i = 0; i < n; ++i) {
    mi[i] = ma[i] = 0;
    while (B[(i + ma[i]) % n] != '1')
      ++ma[i];
    while (B[(i + mi[i] + n) % n] != '1')
      --mi[i];
  }
  for (int s = 0; s < n; ++s) {
    for (int r = 0; r < n; ++r)
      mx[r] = 0;
    int dif = 0;
    for (int r = 0; r < n; ++r)
      if (A[r] != B[(r + s) % n])
        mx[-mi[r]] = max(mx[-mi[r]], ma[r] - s), ++dif;
    int ly = 0, ca = 1e9;
    for (int r = n - 1; r >= 0; --r)
      ca = min(ca, ly + r), ly = max(ly, mx[r]);
    ans = min(ans, 2 * ca + s + dif);
  }
}
int main() {
  scanf("%s%s", A, B);
  n = strlen(A);
  ans = 1e9;
  bool ha = 0, hb = 0;
  for (int i = 0; i < n; ++i)
    if (A[i] == '1')
      ha = 1;
  for (int i = 0; i < n; ++i)
    if (B[i] == '1')
      hb = 1;
  if (!hb) {
    if (ha)
      puts("-1");
    else
      puts("0");
    return 0;
  }
  work();
  reverse(A, A + n);
  reverse(B, B + n);
  work();
  printf("%d\n", ans);
}