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>
#define N 1000000
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int main() {
static char cc[N + 1];
static int ll[N], rr[N], ii[N], kk[N];
int n, m, cnt, h, h_, i, j, s, d;
scanf("%d%s", &n, cc);
cnt = 0;
while (n > 0) {
m = 0;
for (i = 0; i + 1 < n; i = j) {
j = i + 1;
while (j < n && cc[j] == cc[i])
j++;
if (j - i > 1)
ll[m] = i, rr[m] = j - 1, m++;
}
if (m == 0) {
printf("-1\n");
return 0;
}
s = (ll[0] + 1) + (n - rr[m - 1]);
for (h = 0; h + 1 < m; h++)
s += ll[h + 1] - rr[h];
if (s % 2 == 0) {
h_ = 0;
for (h = -1; h < m; h++) {
d = (h + 1 == m ? n : ll[h + 1]) - (h < 0 ? -1 : rr[h]) - (h < 0 || h + 1 == m ? 1 : 0);
if (d == s / 2 - 1) {
h_ = min(h + 1, m - 1);
break;
}
}
} else {
if ((ll[0] + 1) + (n - rr[m - 1]) == s / 2 + 1)
h_ = 0;
else {
h_ = 0;
for (h = 0; h + 1 < m; h++) {
d = ll[h + 1] - rr[h];
if (d == s / 2 - 1) {
h_ = h;
break;
}
}
}
}
if ((rr[h_] - ll[h_] + 1) % 2 == 0) {
ii[cnt] = ll[h_], kk[cnt] = 2, cnt++;
n -= 2;
for (i = ll[h_]; i < n; i++)
cc[i] = cc[i + 2];
cc[n] = 0;
} else {
ii[cnt] = ll[h_], kk[cnt] = 3, cnt++;
n -= 3;
for (i = ll[h_]; i < n; i++)
cc[i] = cc[i + 3];
cc[n] = 0;
}
}
printf("%d\n", cnt);
for (h = 0; h < cnt; h++)
printf("%d %d\n", ii[h] + 1, kk[h]);
return 0;
}
Compilation message (stderr)
Main.c: In function 'main':
Main.c:13:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | scanf("%d%s", &n, cc);
| ^~~~~~~~~~~~~~~~~~~~~
# | 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... |