#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) {
int go = -1;
for (int i = 0; i + 2 < N; ++i) {
if (S.back() == '0') break;
if (S[i] == '1' || S[i + 1] == '1' || S[i + 2] == '1') continue;
if (i + 3 < N && S[i + 3] == '0') {
go = i;
break;
}
else go = i;
}
if (go != -1) {
ret++;
reverse(S.begin() + go + 2, S.end());
}
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
Main.cpp: In function 'int Solve(std::string, std::string)':
Main.cpp:83:1: warning: control reaches end of non-void function [-Wreturn-type]
83 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
996 KB |
Output is correct |
4 |
Correct |
1 ms |
996 KB |
Output is correct |
5 |
Correct |
1 ms |
996 KB |
Output is correct |
6 |
Correct |
1 ms |
992 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
1252 KB |
Output is correct |
7 |
Correct |
3 ms |
1248 KB |
Output is correct |
8 |
Correct |
3 ms |
1252 KB |
Output is correct |
9 |
Correct |
2 ms |
1252 KB |
Output is correct |
10 |
Correct |
2 ms |
1252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
1252 KB |
Output is correct |
7 |
Correct |
3 ms |
1248 KB |
Output is correct |
8 |
Correct |
3 ms |
1252 KB |
Output is correct |
9 |
Correct |
2 ms |
1252 KB |
Output is correct |
10 |
Correct |
2 ms |
1252 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
1252 KB |
Output is correct |
21 |
Correct |
2 ms |
1276 KB |
Output is correct |
22 |
Correct |
3 ms |
1252 KB |
Output is correct |
23 |
Correct |
3 ms |
1248 KB |
Output is correct |
24 |
Correct |
3 ms |
1248 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
1252 KB |
Output is correct |
27 |
Correct |
3 ms |
1252 KB |
Output is correct |
28 |
Correct |
1 ms |
988 KB |
Output is correct |
29 |
Correct |
3 ms |
1368 KB |
Output is correct |
30 |
Correct |
3 ms |
1252 KB |
Output is correct |
31 |
Correct |
2 ms |
1252 KB |
Output is correct |
32 |
Correct |
2 ms |
1384 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
452 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
1252 KB |
Output is correct |
8 |
Correct |
3 ms |
1252 KB |
Output is correct |
9 |
Correct |
3 ms |
1280 KB |
Output is correct |
10 |
Correct |
3 ms |
1252 KB |
Output is correct |
11 |
Correct |
3 ms |
1248 KB |
Output is correct |
12 |
Correct |
3 ms |
1240 KB |
Output is correct |
13 |
Correct |
3 ms |
1248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
1252 KB |
Output is correct |
11 |
Correct |
3 ms |
1252 KB |
Output is correct |
12 |
Correct |
3 ms |
1252 KB |
Output is correct |
13 |
Correct |
3 ms |
1252 KB |
Output is correct |
14 |
Correct |
3 ms |
1240 KB |
Output is correct |
15 |
Correct |
3 ms |
1248 KB |
Output is correct |
16 |
Correct |
3 ms |
1248 KB |
Output is correct |
17 |
Correct |
2 ms |
1252 KB |
Output is correct |
18 |
Correct |
3 ms |
1252 KB |
Output is correct |
19 |
Correct |
2 ms |
1252 KB |
Output is correct |
20 |
Correct |
2 ms |
1252 KB |
Output is correct |
21 |
Correct |
2 ms |
1252 KB |
Output is correct |
22 |
Correct |
3 ms |
1252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
452 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
1252 KB |
Output is correct |
8 |
Correct |
3 ms |
1252 KB |
Output is correct |
9 |
Correct |
3 ms |
1280 KB |
Output is correct |
10 |
Correct |
3 ms |
1252 KB |
Output is correct |
11 |
Correct |
3 ms |
1248 KB |
Output is correct |
12 |
Correct |
3 ms |
1240 KB |
Output is correct |
13 |
Correct |
3 ms |
1248 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
3 ms |
1252 KB |
Output is correct |
24 |
Correct |
3 ms |
1252 KB |
Output is correct |
25 |
Correct |
3 ms |
1252 KB |
Output is correct |
26 |
Correct |
3 ms |
1252 KB |
Output is correct |
27 |
Correct |
3 ms |
1240 KB |
Output is correct |
28 |
Correct |
3 ms |
1248 KB |
Output is correct |
29 |
Correct |
3 ms |
1248 KB |
Output is correct |
30 |
Correct |
2 ms |
1252 KB |
Output is correct |
31 |
Correct |
3 ms |
1252 KB |
Output is correct |
32 |
Correct |
2 ms |
1252 KB |
Output is correct |
33 |
Correct |
2 ms |
1252 KB |
Output is correct |
34 |
Correct |
2 ms |
1252 KB |
Output is correct |
35 |
Correct |
3 ms |
1252 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
33 ms |
10192 KB |
Output is correct |
39 |
Correct |
20 ms |
4068 KB |
Output is correct |
40 |
Correct |
1 ms |
996 KB |
Output is correct |
41 |
Correct |
34 ms |
4068 KB |
Output is correct |
42 |
Correct |
35 ms |
4068 KB |
Output is correct |
43 |
Correct |
26 ms |
4064 KB |
Output is correct |
44 |
Correct |
32 ms |
5860 KB |
Output is correct |
45 |
Correct |
1 ms |
992 KB |
Output is correct |
46 |
Correct |
24 ms |
4340 KB |
Output is correct |
47 |
Correct |
19 ms |
4068 KB |
Output is correct |
48 |
Correct |
16 ms |
4320 KB |
Output is correct |
49 |
Correct |
0 ms |
344 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
344 KB |
Output is correct |
52 |
Correct |
0 ms |
456 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
2 ms |
1252 KB |
Output is correct |
55 |
Correct |
2 ms |
1252 KB |
Output is correct |
56 |
Correct |
3 ms |
1252 KB |
Output is correct |
57 |
Correct |
3 ms |
1252 KB |
Output is correct |
58 |
Correct |
2 ms |
1252 KB |
Output is correct |
59 |
Correct |
3 ms |
1252 KB |
Output is correct |
60 |
Correct |
3 ms |
1252 KB |
Output is correct |
61 |
Correct |
0 ms |
344 KB |
Output is correct |
62 |
Correct |
0 ms |
344 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
3 ms |
1252 KB |
Output is correct |
66 |
Correct |
3 ms |
1252 KB |
Output is correct |
67 |
Correct |
3 ms |
1380 KB |
Output is correct |
68 |
Correct |
2 ms |
1252 KB |
Output is correct |
69 |
Correct |
5 ms |
1252 KB |
Output is correct |
70 |
Correct |
2 ms |
1252 KB |
Output is correct |
71 |
Correct |
3 ms |
1248 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
1 ms |
348 KB |
Output is correct |