#include <stdio.h>
#include <string.h>
#define N 25000
#define L 20
#define N_ (2 + N * L)
#define A 26
int max(int a, int b) { return a > b ? a : b; }
int tt[N_][A], dd[N_]; char word[N_];
void dfs1(int i) {
int a;
for (a = 0; a < A; a++) {
int j = tt[i][a];
if (j) {
dfs1(j);
dd[i] = max(dd[i], dd[j] + 1);
}
}
}
void dfs2(int i, int final) {
int a, a_;
if (word[i])
printf("P\n");
a_ = -1;
for (a = 0; a < A; a++) {
int j = tt[i][a];
if (j && dd[i] == dd[j] + 1) {
a_ = a;
break;
}
}
for (a = 0; a < A; a++) {
int j = tt[i][a];
if (j && a != a_)
printf("%c\n", a + 'a'), dfs2(j, 0);
}
if (a_ != -1)
printf("%c\n", a_ + 'a'), dfs2(tt[i][a_], final);
if (!final)
printf("-\n");
}
int main() {
int n, n_, i;
scanf("%d", &n);
n_ = 2;
for (i = 0; i < n; i++) {
static char cc[L + 1];
int l, h, t;
scanf("%s", cc), l = strlen(cc);
for (h = 0, t = 1; h < l; h++) {
int a = cc[h] - 'a';
if (!tt[t][a])
tt[t][a] = n_++;
t = tt[t][a];
}
word[t] = 1;
}
dfs1(1);
printf("%d\n", n + (n_ - 2) * 2 - dd[1]);
dfs2(1, 1);
return 0;
}
Compilation message
printer.c: In function 'main':
printer.c:55:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
printer.c:61:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%s", cc), l = strlen(cc);
| ^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
972 KB |
Output is correct |
2 |
Correct |
3 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
3040 KB |
Output is correct |
2 |
Correct |
18 ms |
6220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
7244 KB |
Output is correct |
2 |
Correct |
7 ms |
1868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
17916 KB |
Output is correct |
2 |
Correct |
120 ms |
41540 KB |
Output is correct |
3 |
Correct |
64 ms |
21496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
14016 KB |
Output is correct |
2 |
Correct |
141 ms |
49220 KB |
Output is correct |
3 |
Correct |
77 ms |
24340 KB |
Output is correct |
4 |
Correct |
121 ms |
46552 KB |
Output is correct |