#include <bits/stdc++.h>
#include <cassert>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 2023
#define MAXS 500
#define INF 1000000000000000001
#define bb ' '
#define ln '\n'
#define Ln '\n'
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
string s, t;
cin >> s >> t;
if (s.size() < t.size()) {
cout << 0 << ln;
return 0;
}
if (t[0] == '1') {
for (auto& c : s) c ^= 1;
for (auto& c : t) c ^= 1;
}
if (t == "001") {
for (auto& c : s) c ^= 1;
for (auto& c : t) c ^= 1;
reverse(s.begin(), s.end());
reverse(t.begin(), t.end());
}
if (t.size() == 1) {
for (auto c : s) {
if (c == t[0]) {
cout << -1 << ln;
return 0;
}
}
cout << 0 << ln;
return 0;
}
if (t.size() == 2) {
if (t[0] == t[1]) {
int N = s.size();
int i;
int cnt = 0;
for (i = 0; i < N; i++) if (s[i] == '1') cnt++;
if (cnt < N / 2) {
cout << -1 << ln;
return 0;
}
int ans = 0;
for (i = 0; i + 1 < N; i++) if (s[i] == s[i + 1] && s[i] == '0') ans++;
cout << ans << ln;
return 0;
}
int N, i;
N = s.size();
int pv = 1;
int ans = 0;
for (i = 0; i < N; i++) {
if (s[i] == '1') {
if (!pv) {
pv = 1;
ans++;
}
}
else pv = 0;
}
cout << ans << ln;
return 0;
}
if (t == "011") {
reverse(s.begin(), s.end());
while (s.size() && s.back() == '1') s.pop_back();
if (s.empty()) {
cout << 0 << Ln;
return 0;
}
reverse(s.begin(), s.end());
int N;
int str = 0;
int ans = 0;
for (auto c : s) {
if (c == '0') {
if (str > 1) ans++;
str = 0;
}
else str++;
}
if (str > 1) ans++;
cout << ans << ln;
return 0;
}
if (t == "010") {
while (s.size() && s.back() == '1') s.pop_back();
if (s.empty()) {
cout << 0 << Ln;
return 0;
}
reverse(s.begin(), s.end());
while (s.size() && s.back() == '1') s.pop_back();
if (s.empty()) {
cout << 0 << Ln;
return 0;
}
int N = s.size(), i;
int cnt = 0;
for (i = 1; i + 1 < N; i++) if (s[i - 1] == '0' && s[i + 1] == '0' && s[i] == '1') cnt++;
cout << ++cnt / 2 << Ln;
return 0;
}
int N, M;
N = s.size();
vector<int> v;
int cnt = 0;
int i;
for (i = 0; i < N; i++) {
if (s[i] == '1') {
v.push_back(cnt);
cnt = 0;
}
else cnt++;
}
v.push_back(cnt);
M = v.size();
int z, o;
z = o = 0;
int ans = 0;
for (auto x : v) {
if (!x) z++;
if (x == 1) o++;
}
for (auto& x : v) {
if (x > 3) {
int k = x / 2 - 1;
k = min(z, k);
x -= k * 2;
ans += k;
if (k) z -= k;
}
}
o += z * 2;
for (auto& x : v) {
if (x > 2) {
int k = x - 2;
ans += k;
o -= k;
if (o < 0) {
cout << -1 << ln;
return 0;
}
}
}
cout << ans << ln;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:84:7: warning: unused variable 'N' [-Wunused-variable]
84 | int N;
| ^
Main.cpp:116:9: warning: variable 'M' set but not used [-Wunused-but-set-variable]
116 | int N, M;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
3 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
604 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
272 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
604 KB |
Output is correct |
8 |
Correct |
4 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
3 ms |
604 KB |
Output is correct |
19 |
Correct |
3 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
272 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
604 KB |
Output is correct |
8 |
Correct |
4 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
3 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
3 ms |
604 KB |
Output is correct |
32 |
Correct |
3 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
312 KB |
Output is correct |
38 |
Correct |
6 ms |
1644 KB |
Output is correct |
39 |
Correct |
3 ms |
1112 KB |
Output is correct |
40 |
Correct |
4 ms |
1088 KB |
Output is correct |
41 |
Correct |
3 ms |
1176 KB |
Output is correct |
42 |
Correct |
3 ms |
1112 KB |
Output is correct |
43 |
Correct |
3 ms |
1144 KB |
Output is correct |
44 |
Correct |
4 ms |
1080 KB |
Output is correct |
45 |
Correct |
3 ms |
1148 KB |
Output is correct |
46 |
Correct |
2 ms |
1140 KB |
Output is correct |
47 |
Correct |
2 ms |
1112 KB |
Output is correct |
48 |
Correct |
4 ms |
1112 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
320 KB |
Output is correct |
51 |
Correct |
0 ms |
320 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
4 ms |
640 KB |
Output is correct |
55 |
Correct |
4 ms |
724 KB |
Output is correct |
56 |
Correct |
3 ms |
644 KB |
Output is correct |
57 |
Correct |
3 ms |
604 KB |
Output is correct |
58 |
Correct |
3 ms |
604 KB |
Output is correct |
59 |
Correct |
2 ms |
660 KB |
Output is correct |
60 |
Correct |
2 ms |
724 KB |
Output is correct |
61 |
Correct |
0 ms |
212 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
0 ms |
212 KB |
Output is correct |
64 |
Correct |
1 ms |
212 KB |
Output is correct |
65 |
Correct |
3 ms |
604 KB |
Output is correct |
66 |
Correct |
3 ms |
704 KB |
Output is correct |
67 |
Correct |
3 ms |
684 KB |
Output is correct |
68 |
Correct |
3 ms |
680 KB |
Output is correct |
69 |
Correct |
2 ms |
728 KB |
Output is correct |
70 |
Correct |
2 ms |
668 KB |
Output is correct |
71 |
Correct |
3 ms |
696 KB |
Output is correct |
72 |
Correct |
1 ms |
216 KB |
Output is correct |
73 |
Correct |
0 ms |
216 KB |
Output is correct |