#include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <algorithm>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::string s, t;
std::cin >> s >> t;
int n = s.length();
int len_t = t.length();
if (n < len_t) {
std::cout << 0 << std::endl;
return 0;
}
int n0 = 0;
int n1 = 0;
for (char c : s) {
if (c == '0') {
n0++;
} else {
n1++;
}
}
if (len_t == 1) {
if (t == "0") {
std::cout << (n0 == 0 ? 0 : -1) << std::endl;
} else {
std::cout << (n1 == 0 ? 0 : -1) << std::endl;
}
return 0;
}
if (len_t == 2) {
if (t == "00") {
if (n1 == 0 && n0 >= 2) {
std::cout << -1 << std::endl;
} else if (n0 >= n1 + 2) {
std::cout << -1 << std::endl;
} else if (s.find("00") == std::string::npos) {
std::cout << 0 << std::endl;
} else {
std::cout << 1 << std::endl;
}
return 0;
} else if (t == "11") {
if (n0 == 0 && n1 >= 2) {
std::cout << -1 << std::endl;
} else if (n1 >= n0 + 2) {
std::cout << -1 << std::endl;
} else if (s.find("11") == std::string::npos) {
std::cout << 0 << std::endl;
} else {
std::cout << 1 << std::endl;
}
return 0;
} else if (t == "01") {
std::string target(n1, '1');
target.append(n0, '0');
if (s == target) {
std::cout << 0 << std::endl;
return 0;
}
std::string rev_target = target;
std::reverse(rev_target.begin(), rev_target.end());
if (s == rev_target) {
std::cout << 1 << std::endl;
return 0;
}
int first_diff = -1;
int last_diff = -1;
for (int i = 0; i < n; ++i) {
if (s[i] != target[i]) {
if (first_diff == -1) {
first_diff = i;
}
last_diff = i;
}
}
if (first_diff != -1) {
std::string sub_s = s.substr(first_diff, last_diff - first_diff + 1);
std::string sub_target = target.substr(first_diff, last_diff - first_diff + 1);
std::reverse(sub_s.begin(), sub_s.end());
if (sub_s == sub_target) {
std::cout << 1 << std::endl;
return 0;
}
}
std::cout << 2 << std::endl;
return 0;
} else if (t == "10") {
std::string target(n0, '0');
target.append(n1, '1');
if (s == target) {
std::cout << 0 << std::endl;
return 0;
}
std::string rev_target = target;
std::reverse(rev_target.begin(), rev_target.end());
if (s == rev_target) {
std::cout << 1 << std::endl;
return 0;
}
int first_diff = -1;
int last_diff = -1;
for (int i = 0; i < n; ++i) {
if (s[i] != target[i]) {
if (first_diff == -1) {
first_diff = i;
}
last_diff = i;
}
}
if (first_diff != -1) {
std::string sub_s = s.substr(first_diff, last_diff - first_diff + 1);
std::string sub_target = target.substr(first_diff, last_diff - first_diff + 1);
std::reverse(sub_s.begin(), sub_s.end());
if (sub_s == sub_target) {
std::cout << 1 << std::endl;
return 0;
}
}
std::cout << 2 << std::endl;
return 0;
}
}
if (len_t == 3) {
if (t == "000") {
if (n1 == 0 && n0 >= 3) {
std::cout << -1 << std::endl;
} else if (n0 > 2 * (n1 + 1)) {
std::cout << -1 << std::endl;
} else if (s.find("000") == std::string::npos) {
std::cout << 0 << std::endl;
} else {
std::cout << 1 << std::endl;
}
return 0;
} else if (t == "111") {
if (n0 == 0 && n1 >= 3) {
std::cout << -1 << std::endl;
} else if (n1 > 2 * (n0 + 1)) {
std::cout << -1 << std::endl;
} else if (s.find("111") == std::string::npos) {
std::cout << 0 << std::endl;
} else {
std::cout << 1 << std::endl;
}
return 0;
} else {
if (s.find(t) != std::string::npos) {
std::cout << 1 << std::endl;
} else {
std::cout << 0 << std::endl;
}
return 0;
}
}
return 0;
}
# | 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... |