#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string s, t;
void flip(string &s)
{
for (int i = 0; i < (int)s.size(); i++) {
s[i] = s[i] == '0' ? '1' : '0';
}
}
int main()
{
cin >> s >> t;
int n = s.size();
int k = t.size();
if (k == 1) {
bool ouc[2];
for (int i = 0; i < n; i++) ouc[s[i]-'0'] = true;
if (ouc[t[0]-'0']) cout << -1;
else cout << 0;
}
else if (k == 2) {
if (t[0] != t[1]) {
if (t[0] == '1') flip(s);
int ans = 0;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '1') continue;
while (i > 0 && s[i-1] == '0') --i;
ans++;
}
if (s[n-1] == '0') --ans;
cout << ans;
}
else {
if (t[0] == '1') flip(s);
int ans = 0;
int cnt = 0;
for (int i = 0; i < n; i++) cnt += s[i] == '0';
if (cnt > (n+1)/2) cout << -1;
else {
for (int i = 0; i < n; i++) {
if (s[i] == '1') continue;
int l = i;
while (i + 1 < n && s[i] == s[i+1]) i++;
int r = i;
ans += r - l;
}
cout << ans;
}
}
}
else {
if (t[0] != t[2]) {
if (t[0] == '1') {
flip(s); flip(t);
}
if (t[1] == '1') {
t[1] = '0';
flip(s);
reverse(s.begin(), s.end());
}
int cnt = 0;
int st = n - 1;
while (st >= 0 && s[st] == '0') st--;
for (int i = st; i >= 0; i--) {
if (s[i] == '1') continue;
int l = i;
while (i > 0 && s[i-1] == '0') i--;
int r = i;
cnt += l - r >= 1;
}
cout << cnt;
}
else if (t[0] != t[1]) {
if (t[0] == '1') flip(s);
int cnt = 0;
for (int i = 1; i < n - 1; i++) {
if (s[i-1] == '0' && s[i] == '1' && s[i+1] == '0') cnt++;
}
cout << (cnt + 1) / 2;
}
else {
int mcnt = 0, cur = 0;
for (int i = 0; i < n; i++) {
if (i % 3 != 2) mcnt++;
if (s[i] == '0') cur++;
}
if (mcnt < cur) cout << -1;
else {
if (t[0] == '1') flip(s);
int cnt = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1') continue;
int l = i;
while (i < n - 1 && s[i+1] == '0') i++;
int r = i;
cnt += (r - l) / 2;
}
int wall = 0;
for (int i = 0; i < n - 1; i++) {
if (s[i] == '1' && s[i+1] == '1') wall++;
}
wall += (s[0] == '1') + (s[n-1] == '1');
cout << 2 * cnt - wall;
}
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
612 KB |
Output is correct |
4 |
Correct |
4 ms |
612 KB |
Output is correct |
5 |
Correct |
3 ms |
612 KB |
Output is correct |
6 |
Correct |
4 ms |
612 KB |
Output is correct |
7 |
Correct |
4 ms |
612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
612 KB |
Output is correct |
7 |
Correct |
3 ms |
612 KB |
Output is correct |
8 |
Correct |
3 ms |
612 KB |
Output is correct |
9 |
Correct |
4 ms |
612 KB |
Output is correct |
10 |
Correct |
4 ms |
612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
612 KB |
Output is correct |
7 |
Correct |
3 ms |
612 KB |
Output is correct |
8 |
Correct |
3 ms |
612 KB |
Output is correct |
9 |
Correct |
4 ms |
612 KB |
Output is correct |
10 |
Correct |
4 ms |
612 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 |
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 |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
4 ms |
628 KB |
Output is correct |
21 |
Correct |
3 ms |
612 KB |
Output is correct |
22 |
Correct |
4 ms |
616 KB |
Output is correct |
23 |
Correct |
4 ms |
612 KB |
Output is correct |
24 |
Correct |
4 ms |
612 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
4 ms |
612 KB |
Output is correct |
27 |
Correct |
4 ms |
612 KB |
Output is correct |
28 |
Correct |
3 ms |
612 KB |
Output is correct |
29 |
Correct |
4 ms |
612 KB |
Output is correct |
30 |
Correct |
4 ms |
656 KB |
Output is correct |
31 |
Correct |
6 ms |
660 KB |
Output is correct |
32 |
Correct |
5 ms |
612 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 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 |
1 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 |
4 ms |
612 KB |
Output is correct |
8 |
Correct |
4 ms |
612 KB |
Output is correct |
9 |
Correct |
4 ms |
612 KB |
Output is correct |
10 |
Correct |
4 ms |
612 KB |
Output is correct |
11 |
Correct |
4 ms |
612 KB |
Output is correct |
12 |
Correct |
4 ms |
612 KB |
Output is correct |
13 |
Correct |
4 ms |
612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 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 |
4 ms |
612 KB |
Output is correct |
11 |
Correct |
4 ms |
616 KB |
Output is correct |
12 |
Correct |
5 ms |
612 KB |
Output is correct |
13 |
Correct |
4 ms |
612 KB |
Output is correct |
14 |
Correct |
4 ms |
612 KB |
Output is correct |
15 |
Correct |
4 ms |
612 KB |
Output is correct |
16 |
Correct |
4 ms |
612 KB |
Output is correct |
17 |
Correct |
4 ms |
612 KB |
Output is correct |
18 |
Correct |
3 ms |
612 KB |
Output is correct |
19 |
Correct |
4 ms |
612 KB |
Output is correct |
20 |
Correct |
3 ms |
612 KB |
Output is correct |
21 |
Correct |
4 ms |
612 KB |
Output is correct |
22 |
Correct |
4 ms |
612 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 |
1 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 |
4 ms |
612 KB |
Output is correct |
8 |
Correct |
4 ms |
612 KB |
Output is correct |
9 |
Correct |
4 ms |
612 KB |
Output is correct |
10 |
Correct |
4 ms |
612 KB |
Output is correct |
11 |
Correct |
4 ms |
612 KB |
Output is correct |
12 |
Correct |
4 ms |
612 KB |
Output is correct |
13 |
Correct |
4 ms |
612 KB |
Output is correct |
14 |
Correct |
1 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 |
1 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 |
4 ms |
612 KB |
Output is correct |
24 |
Correct |
4 ms |
616 KB |
Output is correct |
25 |
Correct |
5 ms |
612 KB |
Output is correct |
26 |
Correct |
4 ms |
612 KB |
Output is correct |
27 |
Correct |
4 ms |
612 KB |
Output is correct |
28 |
Correct |
4 ms |
612 KB |
Output is correct |
29 |
Correct |
4 ms |
612 KB |
Output is correct |
30 |
Correct |
4 ms |
612 KB |
Output is correct |
31 |
Correct |
3 ms |
612 KB |
Output is correct |
32 |
Correct |
4 ms |
612 KB |
Output is correct |
33 |
Correct |
3 ms |
612 KB |
Output is correct |
34 |
Correct |
4 ms |
612 KB |
Output is correct |
35 |
Correct |
4 ms |
612 KB |
Output is correct |
36 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |