답안 #739895

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
739895 2023-05-11T14:19:26 Z Desh03 Type Printer (IOI08_printer) C++17
30 / 100
186 ms 83260 KB
#include <bits/stdc++.h>
using namespace std;

struct node {
    node* c[26] = {NULL};
    bool endofword;
    int sz;
    node(bool x) : endofword(x) { };
};

vector<char> ans;

struct trie {
    node* root;
    trie() : root(NULL) { };
    void insert(node* &u, int id, string &s) {
        if (u == NULL) u = new node(false);
        if (id == s.size()) {
            u->endofword = true;
            return;
        }
        insert(u->c[s[id] - 'a'], id + 1, s);
    }
    void dfs1(node* &u) {
        u->sz = 1;
        for (int i = 0; i < 26; i++)
            if (u->c[i] != NULL)
                dfs1(u->c[i]), u->sz += u->c[i]->sz;
    }
    void dfs2(node* u) {
        if (u->endofword) ans.push_back('P');
        vector<int> f(26);
        for (int i = 0; i < 26; i++) f[i] = i;
        sort(f.begin(), f.end(), [&](int a, int b) {
            int x = u->c[a] == NULL ? 0 : u->c[a]->sz, y = u->c[b] == NULL ? 0 : u->c[b]->sz;
            return x < y;
        });
        for (int i : f)
            if (u->c[i] != NULL) {
                ans.push_back('a' + i);
                dfs2(u->c[i]);
            }
        ans.push_back('-');
    }
    void insert(string s) {
        insert(root, 0, s);
    }
};

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    trie tr;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        tr.insert(s);
    }
    tr.dfs1(tr.root);
    tr.dfs2(tr.root);
    while (ans.back() == '-') ans.pop_back();
    cout << ans.size() << '\n';
    for (char c : ans) cout << c << '\n';
}

Compilation message

printer.cpp: In member function 'void trie::insert(node*&, int, std::string&)':
printer.cpp:18:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |         if (id == s.size()) {
      |             ~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1748 KB Output is correct
2 Incorrect 5 ms 2260 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 15 ms 5940 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 14704 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 36404 KB Output is correct
2 Correct 186 ms 83260 KB Output is correct
3 Incorrect 99 ms 42700 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 74 ms 28460 KB Output isn't correct
2 Halted 0 ms 0 KB -