# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
231585 | AlexLuchianov | Type Printer (IOI08_printer) | C++14 | 239 ms | 99692 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.
#include <iostream>
#include <vector>
#include <cassert>
#include <cmath>
using namespace std;
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
int const nmax = 25000;
int const sigma = 26;
vector<char> sol;
class Trie{
private:
struct Node{
int _scount;
int depth;
Node* son[sigma];
Node(){
_scount = 0;
for(int i = 0; i < sigma; i++)
son[i] = nullptr;
}
};
public:
void _insert(Node* &root, string &s, int ptr){
if(root == nullptr)
root = new Node();
if(s.size() == ptr)
root->_scount++;
else
_insert(root->son[s[ptr] - 'a'], s, ptr + 1);
}
void dfs(Node* &root){
if(root == nullptr)
return ;
root->depth = 1;
for(int h = 0; h < sigma; h++)
if(root->son[h] != nullptr) {
dfs(root->son[h]);
root->depth = max(root->depth, root->son[h]->depth + 1);
}
}
void print(Node* &root, bool discount){
if(root == nullptr)
return ;
for(int h = 0; h < root->_scount; h++)
sol.push_back('P');
int chosen = 0;
for(int h = 0; h < sigma; h++) {
if(root->son[h] == nullptr)
continue;
if(root->son[h]->depth + 1 == root->depth)
chosen = h;
}
if(discount == 0)
chosen = -1;
for(int h = 0; h < sigma; h++){
if(h == chosen)
continue;
if(root->son[h] != nullptr){
sol.push_back(char(h + 'a'));
print(root->son[h], 0);
sol.push_back('-');
}
}
if(-1 != chosen && root->son[chosen] != nullptr){
sol.push_back(char(chosen + 'a'));
print(root->son[chosen], 1);
}
}
Node* root = nullptr;
};
int main()
{
int n;
cin >> n;
Trie trie;
for(int i = 1;i <= n; i++){
string s;
cin >> s;
trie._insert(trie.root, s, 0);
}
trie.dfs(trie.root);
trie.print(trie.root, 1);
cout << sol.size() << '\n';
for(int i = 0; i < sol.size(); i++)
cout << sol[i] << '\n';
return 0;
}
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... |