# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
784855 | jakobrs | Type Printer (IOI08_printer) | C++17 | 124 ms | 105412 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
using i64 = int64_t;
struct Node {
Node *children[26];
bool is_terminal = false;
i64 longest = 0;
i64 go_here_last = -1;
Node() { std::fill(std::begin(children), std::end(children), nullptr); }
};
void dfs(Node *node, i64 &w) {
if (node->is_terminal) {
std::cout << "P\n";
w--;
}
for (i64 i = 0; i < 26; i++) {
if (i != node->go_here_last && node->children[i]) {
std::cout << (char)(i + 'a') << '\n';
dfs(node->children[i], w);
if (w)
std::cout << "-\n";
}
}
if (node->go_here_last != -1) {
std::cout << (char)(node->go_here_last + 'a') << '\n';
dfs(node->children[node->go_here_last], w);
if (w)
std::cout << "-\n";
}
}
int main() {
i64 n;
std::cin >> n;
Node root;
i64 op_count = 0;
std::string word;
for (i64 i = 0; i < n; i++) {
word.clear();
std::cin >> word;
Node *cur = &root;
for (char ch : word) {
if (word.size() > cur->longest) {
cur->go_here_last = ch - 'a';
cur->longest = word.size();
}
if (!cur->children[ch - 'a']) {
cur->children[ch - 'a'] = new Node();
op_count += 2;
}
cur = cur->children[ch - 'a'];
}
cur->is_terminal = true;
op_count += 1;
}
op_count -= root.longest;
std::cout << op_count << '\n';
i64 w = n;
dfs(&root, w);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |