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 <stdio.h>
#include <string.h>
#define N 200000
int max(int a, int b) { return a > b ? a : b; }
int count(char *aa, int n, char *bb, int m) {
int i, k;
k = 0;
for (i = 0; i + m <= n; i++)
if (strncmp(aa + i, bb, m * sizeof *bb) == 0)
k++;
return k;
}
int main() {
static char aa[N + 1], bb[4];
int n, m, i, j, a, d, k1, k2, k, l;
scanf("%s%s", aa, bb), n = strlen(aa), m = strlen(bb);
k = count(aa, n, bb, m);
if (m == 1)
printf(k != 0 ? "-1\n" : "0\n");
else if (m == 2) {
if (bb[0] != bb[1])
printf("%d\n", k);
else {
d = 0;
for (i = 0; i < n; i++)
d += aa[i] == bb[0] ? 1 : -1;
printf("%d\n", d > 1 ? -1 : k);
}
} else {
if (bb[0] != bb[2])
printf("%d\n", k);
else if (bb[0] != bb[1])
printf("%d\n", (k + 1) / 2);
else {
k1 = k2 = 0, k = 0, l = 0;
for (i = -1, j = 0; j <= n; j++)
if (j == n || aa[j] != bb[0]) {
a = j - i - 1;
if (a == 1)
k1++;
else if (a == 0)
k2++;
else {
a -= 2;
k += a, l += (a + 1) / 2;
}
i = j;
}
printf("%d\n", k > k1 + k2 * 2 ? -1 : max(k - k2, l));
}
}
return 0;
}
Compilation message (stderr)
Main.c: In function 'main':
Main.c:22:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf("%s%s", aa, bb), n = strlen(aa), m = strlen(bb);
| ^~~~~~~~~~~~~~~~~~~~~
# | 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... |