#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 1e6 + 5;
const int oo = 1e9, mod = 1e9 + 7;
int cc[2];
void process() {
string s, t; cin >> s >> t;
int cnt = 0, num = 0, len = (int)t.length(), n = (int)s.length();
foru(i,0,n-len) cnt += (t == s.substr(i, len));
foru(i,0,n-1) num += (s[i] == t[0]);
if (len == 1) {
if (cnt) cout << -1 << '\n';
else cout << 0 << '\n';
}
else if (len == 2) {
if (t[0] == t[1] && num > (n + 1) / 2) cout << -1 << '\n';
else cout << cnt << '\n';
}
if (len == 3) {
if (t[0] != t[2]) cout << cnt << '\n';
else if (t[0] != t[1]) cout << (cnt + 1) / 2 << '\n';
else {
if (num > (2 * (n / 3) + n % 3)) cout << -1 << '\n';
else {
int res = 0; s += (t[0] ^ 1);
vector<int> d;
int cur = 0;
foru(i,0,s.length()-1) {
if (s[i] != t[0]) {
if (cur <= 1) cc[cur]++;
else d.push_back(cur);
cur = 0;
}
else cur++;
}
for (int x : d) {
while (x >= 4 && cc[0]) cc[0]--, x -= 2, res++;
while (x >= 3 && cc[1]) cc[1]--, x--, res++;
if (x >= 3) {
cc[0]--;
res++;
}
}
cout << res << '\n';
}
}
}
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
Compilation message
Main.cpp: In function 'void process()':
Main.cpp:8:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | #define foru(i, l, r) for(int i = l; i <= r; i++)
......
44 | foru(i,0,s.length()-1) {
| ~~~~~~~~~~~~~~~~
Main.cpp:44:5: note: in expansion of macro 'foru'
44 | foru(i,0,s.length()-1) {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
2 ms |
860 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
416 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 |
352 KB |
Output is correct |
6 |
Correct |
3 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
3 ms |
860 KB |
Output is correct |
9 |
Correct |
3 ms |
860 KB |
Output is correct |
10 |
Correct |
3 ms |
844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
416 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 |
352 KB |
Output is correct |
6 |
Correct |
3 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
3 ms |
860 KB |
Output is correct |
9 |
Correct |
3 ms |
860 KB |
Output is correct |
10 |
Correct |
3 ms |
844 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
604 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 |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
352 KB |
Output is correct |
20 |
Correct |
3 ms |
984 KB |
Output is correct |
21 |
Correct |
3 ms |
860 KB |
Output is correct |
22 |
Correct |
3 ms |
860 KB |
Output is correct |
23 |
Correct |
3 ms |
864 KB |
Output is correct |
24 |
Correct |
3 ms |
864 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
984 KB |
Output is correct |
27 |
Correct |
3 ms |
988 KB |
Output is correct |
28 |
Correct |
3 ms |
860 KB |
Output is correct |
29 |
Correct |
3 ms |
860 KB |
Output is correct |
30 |
Correct |
3 ms |
868 KB |
Output is correct |
31 |
Correct |
3 ms |
860 KB |
Output is correct |
32 |
Correct |
3 ms |
860 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
556 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
3 ms |
980 KB |
Output is correct |
9 |
Correct |
2 ms |
1104 KB |
Output is correct |
10 |
Correct |
3 ms |
1112 KB |
Output is correct |
11 |
Correct |
3 ms |
860 KB |
Output is correct |
12 |
Correct |
3 ms |
976 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
420 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
892 KB |
Output is correct |
11 |
Correct |
3 ms |
976 KB |
Output is correct |
12 |
Correct |
3 ms |
892 KB |
Output is correct |
13 |
Correct |
3 ms |
860 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
2 ms |
888 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
3 ms |
860 KB |
Output is correct |
19 |
Correct |
3 ms |
860 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
1240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
556 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
3 ms |
980 KB |
Output is correct |
9 |
Correct |
2 ms |
1104 KB |
Output is correct |
10 |
Correct |
3 ms |
1112 KB |
Output is correct |
11 |
Correct |
3 ms |
860 KB |
Output is correct |
12 |
Correct |
3 ms |
976 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
420 KB |
Output is correct |
19 |
Correct |
0 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
892 KB |
Output is correct |
24 |
Correct |
3 ms |
976 KB |
Output is correct |
25 |
Correct |
3 ms |
892 KB |
Output is correct |
26 |
Correct |
3 ms |
860 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
3 ms |
860 KB |
Output is correct |
29 |
Correct |
2 ms |
888 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
3 ms |
860 KB |
Output is correct |
32 |
Correct |
3 ms |
860 KB |
Output is correct |
33 |
Correct |
2 ms |
860 KB |
Output is correct |
34 |
Correct |
2 ms |
860 KB |
Output is correct |
35 |
Correct |
2 ms |
1240 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
3 ms |
860 KB |
Output is correct |
39 |
Correct |
4 ms |
996 KB |
Output is correct |
40 |
Correct |
3 ms |
860 KB |
Output is correct |
41 |
Correct |
3 ms |
860 KB |
Output is correct |
42 |
Correct |
3 ms |
860 KB |
Output is correct |
43 |
Correct |
3 ms |
860 KB |
Output is correct |
44 |
Correct |
3 ms |
868 KB |
Output is correct |
45 |
Correct |
2 ms |
868 KB |
Output is correct |
46 |
Correct |
3 ms |
1256 KB |
Output is correct |
47 |
Correct |
3 ms |
1000 KB |
Output is correct |
48 |
Correct |
4 ms |
1132 KB |
Output is correct |
49 |
Correct |
0 ms |
356 KB |
Output is correct |
50 |
Correct |
0 ms |
352 KB |
Output is correct |
51 |
Correct |
0 ms |
352 KB |
Output is correct |
52 |
Correct |
0 ms |
360 KB |
Output is correct |
53 |
Correct |
1 ms |
352 KB |
Output is correct |
54 |
Correct |
2 ms |
984 KB |
Output is correct |
55 |
Correct |
3 ms |
988 KB |
Output is correct |
56 |
Correct |
2 ms |
868 KB |
Output is correct |
57 |
Correct |
2 ms |
868 KB |
Output is correct |
58 |
Correct |
2 ms |
868 KB |
Output is correct |
59 |
Correct |
2 ms |
984 KB |
Output is correct |
60 |
Correct |
2 ms |
860 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
2 ms |
860 KB |
Output is correct |
66 |
Correct |
2 ms |
860 KB |
Output is correct |
67 |
Correct |
2 ms |
860 KB |
Output is correct |
68 |
Correct |
2 ms |
860 KB |
Output is correct |
69 |
Correct |
3 ms |
860 KB |
Output is correct |
70 |
Correct |
3 ms |
860 KB |
Output is correct |
71 |
Correct |
3 ms |
860 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |