Problem p03620 (C++)

Buggy submission (status: Time Limit Exceeded):
// khodaya khodet komak kon
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimise("ofast")
#pragma GCC optimise("unroll-loops")

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 200000 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000010;
const ll LOG = 25;

int n, L[N], R[N], mx[N];
string A, B;
int mark[N];
vector<int> buc[N];

bool cmp(int x, int y) { return L[x] < L[y]; }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> A >> B;
  n = (int)A.size();
  int cnt = 0, cnt1 = 0;
  for (auto u : A)
    cnt += (u == '1');
  for (auto u : B)
    cnt1 += (u == '1');
  if (cnt1 == 0 && cnt > 0)
    return cout << -1, 0;
  for (int i = 0; i < n; i++) {
    bool f = 0;
    int pnt = i;
    int cnt = 0;
    while (!f) {
      if (B[pnt] == '1')
        R[i] = cnt, f = 1;
      else
        pnt++, pnt %= n, cnt++;
    }
    pnt = i;
    f = 0;
    cnt = 0;
    while (!f) {
      if (B[pnt] == '1')
        L[i] = cnt, f = 1, buc[cnt].pb(i);
      else
        pnt--, pnt %= n, pnt += n, pnt %= n, cnt++;
    }
    // cout << L[i] << ' ' << R[i] << '\n';
  }
  int ans = INF;
  // cout << n << '\n';
  for (int i = -n; i <= n; i++) {
    vector<int> khar;
    memset(mark, 0, sizeof mark);
    //	cout << i << ":\n";
    for (int j = 0; j < n; j++) {
      int ki = j + i;
      ki %= n, ki += n, ki %= n;
      // cout << j << ' ' << ki << '\n';
      if (B[ki] != A[j])
        mark[j] = 1;
    }
    for (int j = 0; j <= n; j++) {
      for (auto u : buc[j]) {
        if (mark[u])
          khar.pb(u);
      }
    }
    // for (auto u:khar) cout << u << '\n';
    //	sort(all(khar), cmp);
    mx[khar.size()] = 0;
    for (int j = khar.size() - 1; j >= 0; j--) {
      mx[j] = max(mx[j + 1], R[khar[j]]);
      // cout << khar[j] << ' ' << mx[j] << '\n';
    }
    int pnt = 0;
    int res = INF;
    for (int j = 0; j <= n; j++) {
      while (pnt != khar.size() && L[khar[pnt]] <= j)
        pnt++;
      int Mx = mx[pnt];
      //	cout << Mx << '\n';
      res = min(res, min(j + Mx + j + abs(Mx - i), Mx + Mx + j + abs(i + j)));
    }
    res += khar.size();
    ans = min(ans, res);
  }
  cout << ans;

  return 0;
}


Fixed submission:
// khodaya khodet komak kon
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimise("ofast")
#pragma GCC optimise("unroll-loops")

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 200000 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000010;
const ll LOG = 25;

int n, L[N], R[N], mx[N];
string A, B;
int mark[N];
vector<int> buc[N];

bool cmp(int x, int y) { return L[x] < L[y]; }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> A >> B;
  n = (int)A.size();
  int cnt = 0, cnt1 = 0;
  for (auto u : A)
    cnt += (u == '1');
  for (auto u : B)
    cnt1 += (u == '1');
  if (cnt1 == 0 && cnt > 0)
    return cout << -1, 0;
  if (cnt1 == 0 && cnt == 0)
    return cout << 0, 0;
  for (int i = 0; i < n; i++) {
    bool f = 0;
    int pnt = i;
    int cnt = 0;
    while (!f) {
      if (B[pnt] == '1')
        R[i] = cnt, f = 1;
      else
        pnt++, pnt %= n, cnt++;
    }
    pnt = i;
    f = 0;
    cnt = 0;
    while (!f) {
      if (B[pnt] == '1')
        L[i] = cnt, f = 1, buc[cnt].pb(i);
      else
        pnt--, pnt %= n, pnt += n, pnt %= n, cnt++;
    }
    // cout << L[i] << ' ' << R[i] << '\n';
  }
  int ans = INF;
  // cout << n << '\n';
  for (int i = -n; i <= n; i++) {
    vector<int> khar;
    memset(mark, 0, sizeof mark);
    //	cout << i << ":\n";
    for (int j = 0; j < n; j++) {
      int ki = j + i;
      ki %= n, ki += n, ki %= n;
      // cout << j << ' ' << ki << '\n';
      if (B[ki] != A[j])
        mark[j] = 1;
    }
    for (int j = 0; j <= n; j++) {
      for (auto u : buc[j]) {
        if (mark[u])
          khar.pb(u);
      }
    }
    // for (auto u:khar) cout << u << '\n';
    //	sort(all(khar), cmp);
    mx[khar.size()] = 0;
    for (int j = khar.size() - 1; j >= 0; j--) {
      mx[j] = max(mx[j + 1], R[khar[j]]);
      // cout << khar[j] << ' ' << mx[j] << '\n';
    }
    int pnt = 0;
    int res = INF;
    for (int j = 0; j <= n; j++) {
      while (pnt != khar.size() && L[khar[pnt]] <= j)
        pnt++;
      int Mx = mx[pnt];
      //	cout << Mx << '\n';
      res = min(res, min(j + Mx + j + abs(Mx - i), Mx + Mx + j + abs(i + j)));
    }
    res += khar.size();
    ans = min(ans, res);
  }
  cout << ans;

  return 0;
}