/*
* 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;
#define MAX_NODE 500000
int numberOfNodesInTrie = 1; // Root is numbered 1
int trie[MAX_NODE + 3][26], maxDepth[MAX_NODE + 3];
bool isEndOfAWord[MAX_NODE + 3];
vector<char> result;
void insert(string s) {
int currentNode = 1;
for (int i = 0; i < s.length(); i++) {
int charIndex = (s[i] - 'a');
maxDepth[currentNode] = max(maxDepth[currentNode], (int) s.length() - i);
if (trie[currentNode][charIndex] == -1) {
trie[currentNode][charIndex] = ++numberOfNodesInTrie;
}
currentNode = trie[currentNode][charIndex];
}
isEndOfAWord[currentNode] = true;
}
void traverse(int currentNode) {
if (isEndOfAWord[currentNode]) {
result.push_back('P');
}
int deepestChildIdx = -1;
for (int i = 0; i < 26; i++) {
if (trie[currentNode][i] != -1 && (deepestChildIdx == -1 || maxDepth[trie[currentNode][i]] > maxDepth[trie[currentNode][deepestChildIdx]])) {
deepestChildIdx = i;
}
}
for (int i = 0; i < 26; i++) {
if (trie[currentNode][i] != -1 && i != deepestChildIdx) {
result.push_back((char) (i + 'a'));
traverse(trie[currentNode][i]);
}
}
if (deepestChildIdx != -1) {
result.push_back((char) (deepestChildIdx + 'a'));
traverse(trie[currentNode][deepestChildIdx]);
}
result.push_back('-');
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
memset(trie, -1, sizeof(trie));
memset(maxDepth, 0, sizeof(maxDepth));
memset(isEndOfAWord, false, sizeof(isEndOfAWord));
for (int i = 0; i < n; i++) {
string s;
cin >> s;
insert(s);
}
traverse(1);
while (result.back() == '-') {
result.pop_back();
}
cout << result.size() << endl;
for (auto r: result) {
cout << r << endl;
}
}
Compilation message
printer.cpp: In function 'void insert(std::string)':
printer.cpp:22:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i = 0; i < s.length(); i++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
53616 KB |
Output is correct |
2 |
Correct |
20 ms |
53572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
53592 KB |
Output is correct |
2 |
Correct |
20 ms |
53588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
53528 KB |
Output is correct |
2 |
Correct |
21 ms |
53572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
53608 KB |
Output is correct |
2 |
Correct |
22 ms |
53572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
53624 KB |
Output is correct |
2 |
Correct |
29 ms |
53588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
53704 KB |
Output is correct |
2 |
Correct |
41 ms |
53700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
53836 KB |
Output is correct |
2 |
Correct |
150 ms |
54092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
54224 KB |
Output is correct |
2 |
Correct |
77 ms |
53708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
441 ms |
54820 KB |
Output is correct |
2 |
Correct |
946 ms |
56012 KB |
Output is correct |
3 |
Correct |
483 ms |
54904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
355 ms |
54572 KB |
Output is correct |
2 |
Execution timed out |
1073 ms |
56440 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |