//https://oj.uz/problem/view/IOI08_printer
#include <bits/stdc++.h>
using namespace std;
struct Node {
bool leaf;
int cnt_of_strings;
unordered_map<int, Node*> alphabet;
Node() {
leaf = false;
cnt_of_strings = 0;
for(int i = 0; i < 26; i++) {
alphabet[i] = NULL;
}
}
} *root;
void trie_insert(string s) {
Node *curr = root;
for(int i = 0; i < s.size(); i++) {
if(curr->alphabet[s[i] - 'a'] == NULL) {
curr->alphabet[s[i] - 'a'] = new Node();
}
curr = curr->alphabet[s[i] - 'a'];
curr->cnt_of_strings++;
}
curr->leaf = true;
}
string max_length = "";
void trie_Search(Node *curr, string s) {
for(int i = 0; i < 26; i++) {
if(curr->alphabet[i] != NULL) {
s += (char)(i + 'a');
trie_Search(curr->alphabet[i], s);
s.pop_back();
}
}
if(max_length.size() < s.size())
max_length = s;
}
string ans = "";
void FindAnswer(Node *curr, bool edge, int idx) {
if(curr->leaf == true) {
ans += "P";
return;
}
//cout << idx << endl;
//cout << "Inside the loop" << endl;
for(int i = 0; i < 26; i++) {
//cout << i << " ";
if(edge && i == max_length[idx] - 'a')
continue;
if(curr->alphabet[i] != NULL) {
ans += (i + 'a');
FindAnswer(curr->alphabet[i], false, idx + 1);
ans += "-";
}
}
//cout << "Have come out of the " << idx << " loop" << endl;
if(edge) {
int x = max_length[idx] - 'a';
ans += max_length[idx];
FindAnswer(curr->alphabet[x], true, idx + 1);
}
}
void Solve() {
int n;
cin >> n;
root = new Node();
for(int i = 1; i <= n; i++) {
string s;
cin >> s;
trie_insert(s);
}
string s = "";
trie_Search(root, s);
FindAnswer(root, true, 0);
cout << ans.size() << endl;
for(int i = 0; i < ans.size(); i++) {
cout << (char)(ans[i]) << endl;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
Solve();
return 0;
}
Compilation message
printer.cpp: In function 'void trie_insert(std::__cxx11::string)':
printer.cpp:21:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < s.size(); i++) {
~~^~~~~~~~~~
printer.cpp: In function 'void Solve()':
printer.cpp:85:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < ans.size(); i++) {
~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
512 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
512 KB |
Output is correct |
2 |
Incorrect |
5 ms |
384 KB |
didn't print every word |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
1024 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
27 ms |
8448 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
72 ms |
30200 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
188 ms |
76664 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
471 ms |
193180 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
369 ms |
150392 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |