//https://oj.uz/problem/view/IOI08_printer
#include <bits/stdc++.h>
using namespace std;
struct Node {
bool leaf;
int ending_strings;
unordered_map<int, Node*> alphabet;
Node() {
leaf = false;
ending_strings = 0;
alphabet.clear();
}
} *root;
void trie_insert(string s) {
Node *curr = root;
for(int i = 0; i < s.size(); i++) {
if(!curr->alphabet.count(s[i] - 'a')) {
curr->alphabet[s[i] - 'a'] = new Node();
}
curr = curr->alphabet[s[i] - 'a'];
}
curr->ending_strings++;
curr->leaf = true;
}
string max_length = "";
void trie_Search(Node *curr, string s) {
unordered_map<int, Node*> :: iterator itr;
for(itr = curr->alphabet.begin(); itr != curr->alphabet.end(); itr++) {
s += (char)(itr->first + 'a');
trie_Search(curr->alphabet[itr->first], s);
s.pop_back();
}
if(max_length.size() < s.size())
max_length = s;
}
string ans = "";
void FindAnswer(Node *curr, bool edge, int idx) {
unordered_map<int, Node*> :: iterator itr;
if(idx == max_length.size()) {
for(int i = 0; i < curr->ending_strings; i++) {
ans += "P";
}
return;
}
//cout << "Inside the loop" << endl;
if(curr->leaf == true) {
for(int i = 0; i < curr->ending_strings; i++) {
ans += "P";
}
}
for(itr = curr->alphabet.begin(); itr != curr->alphabet.end(); itr++) {
//cout << i << " ";
if(edge && itr->first == max_length[idx] - 'a')
continue;
ans += (itr->first + 'a');
FindAnswer(curr->alphabet[itr->first], 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() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
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++) {
s = ans[i];
cout << s << endl;
}
}
int main() {
Solve();
return 0;
}
Compilation message
printer.cpp: In function 'void trie_insert(std::__cxx11::string)':
printer.cpp:19:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < s.size(); i++) {
~~^~~~~~~~~~
printer.cpp: In function 'void FindAnswer(Node*, bool, int)':
printer.cpp:46:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(idx == max_length.size()) {
~~~~^~~~~~~~~~~~~~~~~~~~
printer.cpp: In function 'void Solve()':
printer.cpp:90: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 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
6 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 |
Correct |
8 ms |
384 KB |
Output is correct |
2 |
Correct |
19 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
1380 KB |
Output is correct |
2 |
Correct |
42 ms |
1656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
114 ms |
3968 KB |
Output is correct |
2 |
Correct |
228 ms |
8312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
285 ms |
9736 KB |
Output is correct |
2 |
Correct |
79 ms |
2432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
669 ms |
23828 KB |
Output is correct |
2 |
Execution timed out |
1090 ms |
54024 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
564 ms |
18580 KB |
Output is correct |
2 |
Execution timed out |
1082 ms |
64036 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |