# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1022628 | subrat0018 | Type Printer (IOI08_printer) | C++17 | 506 ms | 140476 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 <bits/stdc++.h>
using namespace std;
#define nline "\n"
class Node{
public:
bool word;
vector<Node*> links;
Node(){
word = false;
links.resize(26, nullptr);
}
};
class Trie{
public:
Node *root;
Trie(){
root = new Node();
}
void insert(string &s){
Node *node = root;
for(auto &val: s){
if(!node->links[val - 'a']){
node->links[val - 'a'] = new Node();
}
node = node->links[val - 'a'];
}
node->word = true;
}
};
vector<char> ans;
unordered_map<Node *, int> height;
void dfs(Node *n){
if(!n)
return;
height[n] = 1;
for(int i=0;i<26;i++){
if(n->links[i]){
dfs(n->links[i]);
height[n] = max(height[n], 1 + height[n->links[i]]);
}
}
}
Node *curr;
bool cmp(int i, int j){
return height[curr->links[i]] < height[curr->links[j]];
}
void dfs2(Node *n){
if(!n)
return;
if(n->word){
ans.push_back('P');
}
int arr[26];
curr = n;
iota(arr, arr+26, 0);
sort(arr, arr+26,cmp);
for(int i=0;i<26;i++){
if(!n->links[arr[i]])continue;
ans.push_back(arr[i] + 'a');
dfs2(n->links[arr[i]]);
ans.push_back('-');
}
}
void solve()
{
int n;cin>>n;
Trie t;
for(int i=0;i<n;i++){
string s;
cin>>s;
t.insert(s);
}
dfs(t.root);
dfs2(t.root);
while(ans.size() && ans.back() == '-')ans.pop_back();
cout<<ans.size()<<nline;
for(auto &val: ans)
cout<<val<<nline;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("debug.txt", "w", stderr);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin>>t;
// prec();
for (int i = 1; i <= t; i++)
{
// cout<<"Case "<<i<<": ";
solve();
}
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... |