답안 #529888

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
529888 2022-02-23T23:55:28 Z Policarpo Type Printer (IOI08_printer) C++17
60 / 100
1000 ms 21836 KB
#include <iostream>
#include <queue>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <iomanip>
#include <map>
#include <cstring>
#include <set>
#include <stack>
#include <bitset>
#define ll long long
#define INF (1e9)
#define MAX (int) (1e5 + 5)
#define MOD 1000000007
#define par pair<int, int>
#define all(v) v.begin(), v.end()
#define sz(x) (int) ((x).size())
#define esq(x) (x<<1)
#define dir(x) ((x<<1)|1)
#define lsb(x) (x & -x)
#define W(x) cout << #x << ": " << x << endl
#define Wii(x) cout << x.first << ' ' << x.second << endl

using namespace std;

int n, t[MAX], trie[MAX][26], qnt, tot, resp, maxn[MAX];
char s[22];

void add(char s[]) {
    int u = 0, ts = strlen(s);
    for (int i = 0; i < ts; i++) {
        char c = s[i] - 'a';
        if (!trie[u][c]) trie[u][c] = ++qnt;
        u = trie[u][c];
    }

    t[u] = 1;
}

int a(int u) {
    int nivel = 0;
    for (int i = 0; i < 26; i++) {
        if (!trie[u][i]) continue;
        if (nivel < 1 + a(trie[u][i])) {
            nivel = 1 + a(trie[u][i]);
            maxn[u] = i;
        }
    }

    return nivel;
}

void b(int u) {
    ++resp;
    if (t[u]) {
        ++tot;
        ++resp;
    }
    for (int i = 0; i < 26; i++) {
        if (!trie[u][i] || maxn[u] == i) continue;
        b(trie[u][i]);
        if (tot != n) {
            ++resp;
        }
    }

    if (trie[u][maxn[u]]) {
        b(trie[u][maxn[u]]);
        if (tot != n) resp++;
    }
}

void solve(int u, char c) {
    if (u) printf("%c\n", c);
    if (t[u]) {
        printf("P\n");
        ++tot;
    }
    for (int i = 0; i < 26; i++) {
        if (!trie[u][i] || maxn[u] == i) continue;
        solve(trie[u][i], i + 'a');
        if (tot != n) printf("-\n");
    }

    if (trie[u][maxn[u]]) {
        solve(trie[u][maxn[u]], maxn[u] + 'a');
        if (tot != n) printf("-\n");
    }
}

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%s", s);
        add(s);
    }

    a(0);
    b(0);
    cout << --resp << endl;
    tot = 0;
    solve(0, ' ');
}

Compilation message

printer.cpp: In function 'void add(char*)':
printer.cpp:35:22: warning: array subscript has type 'char' [-Wchar-subscripts]
   35 |         if (!trie[u][c]) trie[u][c] = ++qnt;
      |                      ^
printer.cpp:35:34: warning: array subscript has type 'char' [-Wchar-subscripts]
   35 |         if (!trie[u][c]) trie[u][c] = ++qnt;
      |                                  ^
printer.cpp:36:21: warning: array subscript has type 'char' [-Wchar-subscripts]
   36 |         u = trie[u][c];
      |                     ^
printer.cpp: In function 'int main()':
printer.cpp:94:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
printer.cpp:96:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |         scanf("%s", s);
      |         ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 304 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 312 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 204 KB Output is correct
2 Correct 10 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 755 ms 688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 323 ms 1024 KB Output is correct
2 Correct 404 ms 1216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 999 ms 3020 KB Output is correct
2 Execution timed out 1068 ms 6224 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 669 ms 7472 KB Output is correct
2 Execution timed out 1089 ms 1612 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 17 ms 21836 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 18 ms 21736 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -