This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int Count(string S, string T) {
	int ret = 0;
	for (int i = 0; i + T.size() <= S.size(); ++i) {
		ret += (S.substr(i, T.size()) == T);
	}
	return ret;
}
int Solve(string S, string T) {
	if (T.size() == 1) return (S.find(T) == string::npos ? 0 : -1);
	int N = S.size();
	if (T == "011" || T == "100") {
		reverse(S.begin(), S.end());
		reverse(T.begin(), T.end());
	}
	if (T[0] == '1') {
		for (char& c : S) c ^= 1;
		for (char& c : T) c ^= 1;
	}
	int zero = 0;
	for (char c : S) zero += (c == '0');
	if (T == "01") return Count(S, T);
	if (T == "00") {
		int lim = (N + 1) / 2;
		if (lim < zero) return -1;
		return Count(S, T);
	}
	if (T == "001") return Count(S, T);
	if (T == "010") return (Count(S, T) + 1) / 2;
	if (T == "000") {
		int lim = N - N / 3;
		if (lim < zero) return -1;
		
		int ret = 0;
		for (int t = 0; t < 2; ++t) {
			for (int i = 0; i + 3 <= N; ++i) {
				if (S.back() == '0') break;
				if (S[i] == '1' || S[i + 1] == '1' || S[i + 2] == '1') continue;
				ret++;
				reverse(S.begin() + i + 2, S.end());
				break;
			}
			reverse(S.begin(), S.end());
		}
		multiset<int> X;
		int cur = 0;
		for (char c : S) {
			if (c == '0') cur++;
			else {
				X.insert(cur);
				cur = 0;
			}
		}
		X.insert(cur);
		while (3 <= *X.rbegin()) {
			ret++;
			int a = *X.begin(), b = *X.rbegin();
			int n = min(2 - a, b - 2);
			X.erase(X.find(a));
			X.erase(X.find(b));
			X.insert(a + n);
			X.insert(b - n);
		}
		return ret;
	}
}
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	string S, T;
	cin >> S >> T;
	cout << Solve(S, T);
	return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int Solve(std::string, std::string)':
Main.cpp:76:1: warning: control reaches end of non-void function [-Wreturn-type]
   76 | }
      | ^| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |