Problem p03620 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

const int N = 2005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;

string s1, s2;
int sum, answ = 10000;
int r[N], l[N];
pair<int, int> p[N];

int main() {
  cin >> s1 >> s2;
  int n = (int)s1.size();
  int z = 0, k = 0;

  for (int i = 0; i < n; i++) {
    if (s2[i] == '0')
      k++;
    if (s1[i] == '1')
      z++;
  }

  if (k == n && z > 0) {
    cout << -1 << endl;
    return 0;
  }

  for (int i = 0; i < n; i++) {
    int x = i;
    while (s2[x] != '1') {
      r[i]++;
      x++;
      x %= n;
    }
    x = i;
    while (s2[x] != '1') {
      l[i]++;
      x--;
      x += n;
      x %= n;
    }
  }

  for (int i = -n + 1; i < n; i++) {
    vector<int> g[N];
    int sz = 0;
    int ss = 0;
    for (int j = 0; j < n; j++) {
      int ind = j + i;
      ind += n;
      ind %= n;
      if (s1[j] != s2[ind]) {
        ss++;
        if (i < 0) {
          if (l[j] > -i) {
            g[l[j]].push_back(r[j]);
            sz++;
          }
        } else {
          if (r[j] > i) {
            g[l[j]].push_back(r[j]);
            sz++;
          }
        }
      }
    }
    if (sz > 0) {
      int X, Y = 0, sum = 10000;
      for (int j = n; j >= 0; j--) {
        if ((int)g[j].size() == 0)
          continue;
        for (int h = 0; h < (int)g[j].size(); h++) {
          /////////// update answer
          X = j;
          if (i < 0)
            X = max(X, -i);
          else
            Y = max(Y, i);
          if (2 * (X + Y) < sum)
            sum = 2 * (X + Y);
          /////////////////////////
          Y = max(Y, g[j][h]);
        }
      }
      X = 0;
      if (i < 0)
        X = max(X, -i);
      else
        Y = max(Y, i);
      if (2 * (X + Y) < sum)
        sum = 2 * (X + Y);
      answ = min(answ, sum - abs(i - 0) + ss);
    } else
      answ = min(answ, abs(i - 0) + ss);
  }
  cout << answ << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

const int N = 2005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;

string s1, s2;
int sum, answ = 10000;
int r[N], l[N];
pair<int, int> p[N];

int main() {
  cin >> s1 >> s2;
  int n = (int)s1.size();
  int z = 0, k = 0;

  for (int i = 0; i < n; i++) {
    if (s2[i] == '0')
      k++;
    if (s1[i] == '1')
      z++;
  }

  if (k == n) {
    if (z > 0)
      cout << -1 << endl;
    else
      cout << 0 << endl;
    return 0;
  }

  for (int i = 0; i < n; i++) {
    int x = i;
    while (s2[x] != '1') {
      r[i]++;
      x++;
      x %= n;
    }
    x = i;
    while (s2[x] != '1') {
      l[i]++;
      x--;
      x += n;
      x %= n;
    }
  }

  for (int i = -n + 1; i < n; i++) {
    vector<int> g[N];
    int sz = 0;
    int ss = 0;
    for (int j = 0; j < n; j++) {
      int ind = j + i;
      ind += n;
      ind %= n;
      if (s1[j] != s2[ind]) {
        ss++;
        if (i < 0) {
          if (l[j] > -i) {
            g[l[j]].push_back(r[j]);
            sz++;
          }
        } else {
          if (r[j] > i) {
            g[l[j]].push_back(r[j]);
            sz++;
          }
        }
      }
    }
    if (sz > 0) {
      int X, Y = 0, sum = 10000;
      for (int j = n; j >= 0; j--) {
        if ((int)g[j].size() == 0)
          continue;
        for (int h = 0; h < (int)g[j].size(); h++) {
          /////////// update answer
          X = j;
          if (i < 0)
            X = max(X, -i);
          else
            Y = max(Y, i);
          if (2 * (X + Y) < sum)
            sum = 2 * (X + Y);
          /////////////////////////
          Y = max(Y, g[j][h]);
        }
      }
      X = 0;
      if (i < 0)
        X = max(X, -i);
      else
        Y = max(Y, i);
      if (2 * (X + Y) < sum)
        sum = 2 * (X + Y);
      answ = min(answ, sum - abs(i - 0) + ss);
    } else
      answ = min(answ, abs(i - 0) + ss);
  }
  cout << answ << endl;
  return 0;
}