Problem p03620 (C++)

Buggy submission (status: Time Limit Exceeded):
// Shift and Flip
// * frank_c1
// * 2017 / 12 / 26

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

const int maxn = 2005;
const int INF = (int)(1e9);
char s[maxn];
int a[maxn], b[maxn];
int l[maxn], r[maxn];

struct node {
  int l, r, id;
} h[maxn];

bool operator<(node a, node b) {
  return a.l != b.l ? a.l < b.l : (a.r != b.r ? a.r < b.r : a.id < b.id);
}

int main() {
  scanf("%s", s);
  int n = strlen(s);
  for (int i = 0; i < n; ++i)
    a[i] = s[i] - '0';
  scanf("%s", s);
  for (int i = 0; i < n; ++i)
    b[i] = s[i] - '0';
  int ac = 0, bc = 0;
  for (int i = 0; i < n; ++i)
    if (a[i])
      ac = 1;
  for (int i = 0; i < n; ++i)
    if (b[i])
      bc = 1;
  if (ac && !bc)
    return printf("-1\n"), 0;
  for (int i = 0; i < n; ++i) {
    int p = i, l = 0, r = 0;
    while (!b[p]) {
      if ((--p) < 0)
        p += n;
      ++l;
    }
    p = i;
    while (!b[p]) {
      if ((++p) >= n)
        p -= n;
      ++r;
    }
    h[i] = (node){l, r, i};
  }
  sort(h, h + n);
  int ret = INF;
  // left
  for (int c = 0; c < n; ++c) {
    int mx = 0, cnt = 0, tmp = INF;
    for (int i = n - 1; i >= 0; --i)
      if (a[h[i].id] ^ b[(h[i].id + n - c) % n]) {
        int x = h[i].l, y = h[i].r;
        ++cnt;
        tmp = min(tmp, 2 * max(0, x - c) + c + 2 * mx);
        mx = max(mx, y);
      }
    tmp = min(tmp, c + 2 * mx);
    ret = min(ret, tmp + cnt);
  }
  // right
  for (int c = 0; c < n; ++c) {
    int mx = 0, cnt = 0, tmp = INF;
    for (int i = n - 1; i >= 0; --i)
      if (a[h[i].id] ^ b[(h[i].id + c) % n]) {
        int x = h[i].l, y = h[i].r;
        ++cnt;
        tmp = min(tmp, 2 * max(0, mx - c) + c + 2 * x);
        mx = max(mx, y);
      }
    tmp = min(tmp, 2 * max(0, mx - c) + c);
    ret = min(ret, tmp + cnt);
  }
  return printf("%d\n", ret), 0;
}

Fixed submission:
// Shift and Flip
// * frank_c1
// * 2017 / 12 / 26

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

const int maxn = 2005;
const int INF = (int)(1e9);
char s[maxn];
int a[maxn], b[maxn];
int l[maxn], r[maxn];

struct node {
  int l, r, id;
} h[maxn];

bool operator<(node a, node b) {
  return a.l != b.l ? a.l < b.l : (a.r != b.r ? a.r < b.r : a.id < b.id);
}

int main() {
  scanf("%s", s);
  int n = strlen(s);
  for (int i = 0; i < n; ++i)
    a[i] = s[i] - '0';
  scanf("%s", s);
  for (int i = 0; i < n; ++i)
    b[i] = s[i] - '0';
  int ac = 0, bc = 0;
  for (int i = 0; i < n; ++i)
    if (a[i])
      ac = 1;
  for (int i = 0; i < n; ++i)
    if (b[i])
      bc = 1;
  if (ac && !bc)
    return printf("-1\n"), 0;
  if (!ac && !bc)
    return printf("0\n"), 0;
  for (int i = 0; i < n; ++i) {
    int p = i, l = 0, r = 0;
    while (!b[p]) {
      if ((--p) < 0)
        p += n;
      ++l;
    }
    p = i;
    while (!b[p]) {
      if ((++p) >= n)
        p -= n;
      ++r;
    }
    h[i] = (node){l, r, i};
  }
  sort(h, h + n);
  int ret = INF;
  // left
  for (int c = 0; c < n; ++c) {
    int mx = 0, cnt = 0, tmp = INF;
    for (int i = n - 1; i >= 0; --i)
      if (a[h[i].id] ^ b[(h[i].id + n - c) % n]) {
        int x = h[i].l, y = h[i].r;
        ++cnt;
        tmp = min(tmp, 2 * max(0, x - c) + c + 2 * mx);
        mx = max(mx, y);
      }
    tmp = min(tmp, c + 2 * mx);
    ret = min(ret, tmp + cnt);
  }
  // right
  for (int c = 0; c < n; ++c) {
    int mx = 0, cnt = 0, tmp = INF;
    for (int i = n - 1; i >= 0; --i)
      if (a[h[i].id] ^ b[(h[i].id + c) % n]) {
        int x = h[i].l, y = h[i].r;
        ++cnt;
        tmp = min(tmp, 2 * max(0, mx - c) + c + 2 * x);
        mx = max(mx, y);
      }
    tmp = min(tmp, 2 * max(0, mx - c) + c);
    ret = min(ret, tmp + cnt);
  }
  return printf("%d\n", ret), 0;
}