Submission #595709

# Submission time Handle Problem Language Result Execution time Memory
595709 2022-07-14T02:17:05 Z tht2005 Type Printer (IOI08_printer) C++17
100 / 100
93 ms 51040 KB
#include <bits/stdc++.h>

using namespace std;

int rd() {
    bool neg = 0; char c = getchar(); for(; c < '0' || c > '9'; c = getchar()) if(c == '-') neg = !neg;
    int n = 0; while('0' <= c && c <= '9') n = (n << 3) + (n << 1) + c - '0', c = getchar();
    return neg ? -n : n;
}
void wr(int n) {
    static char o[11];
    if(n < 0) putchar('-'), n = -n;
    int i = 0; do o[i++] = n % 10 + '0'; while(n /= 10);
    while(i--) putchar(o[i]);
}

#define N 25004
#define L 20
#define V ((N) * (L))

int cnt = 1, f[V], trc[V], nxt[V][26];
bool lf[V];
char s[L + 1];

void dfs1(int v) {
    f[v] = 0;
    trc[v] = -1;
    for(int i = 26, u; i--; ) {
        u = nxt[v][i];
        if(u == 0) {
            continue;
        }
        dfs1(u);
        if(f[v] < f[u] + 1) {
            f[v] = f[u] + 1;
            trc[v] = i;
        }
    }
}
void dfs2(int v, bool ret) {
    if(lf[v]) {
        putchar('P');
        putchar('\n');
    }
    if(ret) {
        for(int i = 26, u; i--; ) {
            u = nxt[v][i];
            if(u == 0) {
                continue;
            }
            putchar('a' + i);
            putchar('\n');
            dfs2(u, 1);
        }
        putchar('-');
        putchar('\n');
    }
    else {
        for(int i = 26, u; i--; ) {
            u = nxt[v][i];
            if(u == 0 || trc[v] == i) {
                continue;
            }
            putchar('a' + i);
            putchar('\n');
            dfs2(u, 1);
        }
        if(~trc[v]) {
            putchar('a' + trc[v]);
            putchar('\n');
            dfs2(nxt[v][trc[v]], 0);
        }
    }
}

int main() {
    int n;
    scanf("%d", &n);
    for(int i = 0, l, p; i < n; ++i) {
        scanf("%s", s);
        l = strlen(s);
        p = 0;
        for(int j = 0, e; j < l; ++j) {
            e = s[j] - 'a';
            if(nxt[p][e] == 0) {
                nxt[p][e] = cnt++;
            }
            p = nxt[p][e];
        }
        lf[p] = 1;
    }
    dfs1(0);
    printf("%d\n", 2 * cnt - 2 - f[0] + n);
    dfs2(0, 0);
    return 0;
}

Compilation message

printer.cpp: In function 'int main()':
printer.cpp:78:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
printer.cpp:80:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |         scanf("%s", s);
      |         ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 304 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1108 KB Output is correct
2 Correct 2 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3156 KB Output is correct
2 Correct 11 ms 6484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 7572 KB Output is correct
2 Correct 7 ms 1876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 18840 KB Output is correct
2 Correct 81 ms 42864 KB Output is correct
3 Correct 43 ms 22316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 14796 KB Output is correct
2 Correct 93 ms 51040 KB Output is correct
3 Correct 52 ms 25288 KB Output is correct
4 Correct 84 ms 48160 KB Output is correct