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 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];
static int kk[N + 1];
int n, m, i, j, a, d, k1, k2, k3, k;
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 = k3 = 0;
for (i = -1, j = 0; j <= n; j++)
if (j == n || aa[j] != bb[0]) {
a = j - i - 1;
if (a == 0)
k3++;
else if (a == 1)
k2++;
else if (a == 2)
k1++;
else if (a > 3)
kk[a - 3]++;
}
k = 0;
while (k3 > 0) {
a = n;
while (a > 0 && kk[a] == 0)
a--;
if (a >= 3)
k++, k3--, kk[a]--, kk[a - 3]++;
else {
k2 += k3, k1 += k3;
break;
}
}
while (k2 > 0) {
a = n;
while (a > 0 && kk[a] == 0)
a--;
if (a >= 2)
k++, k2--, kk[a]--, kk[a - 2]++;
else {
k1 += k2 * 2;
break;
}
}
while (k1 > 0) {
a = n;
while (a > 0 && kk[a] == 0)
a--;
if (a > 0)
k++, k1--, kk[a]--, kk[a - 1]++;
else
break;
}
a = n;
while (a > 0 && kk[a] == 0)
a--;
printf("%d\n", a > 0 ? -1 : k);
}
}
return 0;
}
Compilation message (stderr)
Main.c: In function 'main':
Main.c:21:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | 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... |