#include <stdio.h>
#include <string.h>
#define N 2000
#define M 2000
#define A 26
int max(int a, int b) { return a > b ? a : b; }
void sort(int *aa, int *ii, int *jj, int n, int k) {
static int kk[N + 1];
int h, i, j, a;
memset(kk, 0, (n + 1) * sizeof *kk);
for (h = 0; h < k; h++) {
i = ii[h];
kk[aa[i] + 1]++;
}
for (a = 1; a <= n; a++)
kk[a] += kk[a - 1];
for (h = 0; h < k; h++) {
j = ii[h];
jj[kk[aa[j]]++] = j;
}
}
int main() {
static char cc[N][M + 1];
static int aa[N][M], xx[N], yy[N], ii[N], jj[N];
int n, m, h, i, j, k, ij;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
scanf("%s", cc[i]);
for (ij = (n - 1 + m - 1) - 1; ij >= 0; ij--) {
k = 0;
for (i = max(ij - (m - 1), 0), j = ij - i; i < n && j >= 0; i++, j--) {
ii[k++] = i, xx[i] = cc[i][j] - 'a';
yy[i] = (i + 1 < n && (j + 1 == m || aa[i + 1][j] < aa[i][j + 1])) ? aa[i + 1][j] : aa[i][j + 1];
}
sort(yy, ii, jj, n, k), sort(xx, jj, ii, A, k);
for (h = 0; h < k; h++) {
i = ii[h], j = ij - i;
aa[i][j] = h;
}
}
i = 0, j = 0;
while (i + 1 < n || j + 1 < m) {
printf("%c", cc[i][j]);
if (i + 1 < n && (j + 1 == m || aa[i + 1][j] < aa[i][j + 1]))
i++;
else
j++;
}
printf("%c\n", cc[i][j]);
return 0;
}
Compilation message
pohlepko.c: In function 'main':
pohlepko.c:32:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
pohlepko.c:34:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | scanf("%s", cc[i]);
| ^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
3 ms |
3244 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
13 ms |
3524 KB |
Output is correct |
7 |
Correct |
71 ms |
14676 KB |
Output is correct |
8 |
Correct |
231 ms |
23668 KB |
Output is correct |
9 |
Correct |
1 ms |
844 KB |
Output is correct |
10 |
Correct |
4 ms |
1836 KB |
Output is correct |
11 |
Correct |
6 ms |
1516 KB |
Output is correct |
12 |
Correct |
20 ms |
7880 KB |
Output is correct |
13 |
Correct |
26 ms |
13188 KB |
Output is correct |
14 |
Correct |
233 ms |
23672 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
16 |
Correct |
101 ms |
16460 KB |
Output is correct |