This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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) {
| ^~~~
# | 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... |