답안 #739901

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

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

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;
        }
        if (u->sz < (int) s.size() - id) u->sz = s.size() - id, u->f = s[id] - 'a';
        insert(u->c[s[id] - 'a'], id + 1, s);
    }
    void dfs(node* u) {
        if (u->endofword) ans.push_back('P');
        for (int i = 0; i < 26; i++)
            if (u->c[i] != NULL && i != u->f) {
                ans.push_back('a' + i);
                dfs(u->c[i]);
            }
        if (u->f != -1) ans.push_back('a' + u->f), dfs(u->c[u->f]);
        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.dfs(tr.root);
    while (ans.back() == '-') ans.pop_back();
    cout << ans.size() << '\n';
    for (char c : ans) cout << c << '\n';
}

Compilation message

printer.cpp: In constructor 'node::node(bool)':
printer.cpp:7:13: warning: 'node::f' will be initialized after [-Wreorder]
    7 |     int sz, f;
      |             ^
printer.cpp:7:9: warning:   'int node::sz' [-Wreorder]
    7 |     int sz, f;
      |         ^~
printer.cpp:8:5: warning:   when initialized here [-Wreorder]
    8 |     node(bool x) : endofword(x), f(-1), sz(-1) { };
      |     ^~~~
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 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 452 KB Output is correct
2 Correct 2 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1876 KB Output is correct
2 Correct 5 ms 2388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 6344 KB Output is correct
2 Correct 17 ms 13396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 15696 KB Output is correct
2 Correct 7 ms 3540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 39036 KB Output is correct
2 Correct 103 ms 89076 KB Output is correct
3 Correct 55 ms 45824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 30464 KB Output is correct
2 Correct 125 ms 106512 KB Output is correct
3 Correct 61 ms 52320 KB Output is correct
4 Correct 112 ms 100500 KB Output is correct