# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
652564 | alvinpiter | Type Printer (IOI08_printer) | C++17 | 1083 ms | 99056 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.
/*
* Insert each word into a trie
* "Add" operation is the same as going an edge downward, while "Remove" operation is the same as
going an edge upward. If there is a requirement that the printer must be empty after all operations
are performed, then the answer is 2 * numberOfEdgesInTheTrie + numberOfWords.
* However, we are allowed to leave some letters in the printer, so it is best if we leave the longest word in
the printer.
*/
#include<bits/stdc++.h>
using namespace std;
struct TrieNode {
int maxDepth;
bool isEndOfAWord;
TrieNode* children[26];
TrieNode(): maxDepth(0), isEndOfAWord(false) {}
};
vector<char> result;
void insert(TrieNode* root, string s) {
TrieNode* currentNode = root;
for (int i = 0; i < s.length(); i++) {
int charIndex = (s[i] - 'a');
currentNode->maxDepth = max(currentNode->maxDepth, (int) s.length() - i);
if (currentNode->children[charIndex] == NULL) {
currentNode->children[charIndex] = new TrieNode();
}
currentNode = currentNode->children[charIndex];
}
currentNode->isEndOfAWord = true;
}
void traverse(TrieNode* root) {
if (root->isEndOfAWord) {
result.push_back('P');
}
int deepestChildIdx = -1;
for (int i = 0; i < 26; i++) {
if (root->children[i] != NULL && (deepestChildIdx == -1 || root->children[deepestChildIdx]->maxDepth < root->children[i]->maxDepth)) {
deepestChildIdx = i;
}
}
for (int i = 0; i < 26; i++) {
if (root->children[i] != NULL && i != deepestChildIdx) {
result.push_back((char) (i + 'a'));
traverse(root->children[i]);
}
}
if (deepestChildIdx != -1) {
result.push_back((char) (deepestChildIdx + 'a'));
traverse(root->children[deepestChildIdx]);
}
result.push_back('-');
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
TrieNode* trieRoot = new TrieNode();
for (int i = 0; i < n; i++) {
string s;
cin >> s;
insert(trieRoot, s);
}
traverse(trieRoot);
while (result.back() == '-') {
result.pop_back();
}
cout << result.size() << endl;
for (auto r: result) {
cout << r << endl;
}
}
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... |