// IOI 2008 Day 1 Problem 1 - Type Printer
// Problem link: https://oj.uz/problem/view/IOI08_printer
#include <bits/stdc++.h>
using namespace std;
struct Trie {
struct Node {
Node* child[26];
bool exist = false;
int maxDepth = 0;
int deepest = -1;
Node() {
for (int c = 0; c < 26; ++c) {
child[c] = nullptr;
}
}
};
Node* root;
Trie() {
root = new Node();
}
void addString(string &s) {
Node* p = root;
int n = s.size();
for (int i = 0; i < n; ++i) {
int c = s[i] - 'a';
if (!(p -> child[c])) {
p -> child[c] = new Node();
}
if (n - i > p -> maxDepth) {
p -> maxDepth = n - i;
p -> deepest = c;
}
p = p -> child[c];
}
p -> exist = true;
}
/*
void selfDelete(Node* p) {
for (int c = 0; c < 26; ++c) {
if (p -> child[c]) {
selfDelete(p -> child[c]);
}
}
delete p;
}
*/
};
int n;
Trie T;
vector<char> output;
void dfs(Trie::Node* p) {
if (p -> exist) {
output.push_back('P');
}
for (int c = 0; c < 26; ++c) {
if (c != p -> deepest && p -> child[c]) {
output.push_back(c + 'a');
dfs(p -> child[c]);
output.push_back('-');
}
}
if (p -> deepest != -1) {
output.push_back(p -> deepest + 'a');
dfs(p -> child[p -> deepest]);
output.push_back('-');
}
delete p;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
T.addString(s);
}
dfs(T.root);
while (output.back() == '-') {
output.pop_back();
}
cout << output.size() << '\n';
for (char c : output) {
cout << c << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
3 ms |
2388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
6352 KB |
Output is correct |
2 |
Correct |
16 ms |
13372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
15568 KB |
Output is correct |
2 |
Correct |
6 ms |
3540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
39148 KB |
Output is correct |
2 |
Correct |
100 ms |
89432 KB |
Output is correct |
3 |
Correct |
58 ms |
46104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
30552 KB |
Output is correct |
2 |
Correct |
122 ms |
106424 KB |
Output is correct |
3 |
Correct |
64 ms |
52396 KB |
Output is correct |
4 |
Correct |
108 ms |
99468 KB |
Output is correct |