제출 #595709

#제출 시각아이디문제언어결과실행 시간메모리
595709tht2005Type Printer (IOI08_printer)C++17
100 / 100
93 ms51040 KiB
#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;
}

컴파일 시 표준 에러 (stderr) 메시지

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...