# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
889355 | dimashhh | Type Printer (IOI08_printer) | C++17 | 2 ms | 600 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;
const int N = 1e6 + 1,MOD = 1e9 + 9;
typedef long long ll;
int n;
struct trie{
trie *nx[30];
int sum = 0;
int ok = 0;
ll dp = 0,dp1 = 0;
trie():sum(0),ok(0){
dp1 = dp = 0;
for(int i = 0;i < 30;i++){
nx[i] = nullptr;
}
}
};
int q;
trie *root = new trie();
void add(string &s,trie *cur){
cur->sum++;
for(int i = 0;i < (int)s.size();i++){
int nv = (s[i] - 'a');
if(!cur->nx[nv]){
cur->nx[nv] = new trie();
}
cur = cur->nx[nv];
cur->sum++;
}
cur->ok++;
}
vector<char> res;
int sz(trie *cur){
return (cur ? cur->dp1 : 0);
}
void calc(trie *cur){
cur->dp = 2;
int p[26];
iota(p,p + 26,0);
for(auto i:p)
{
if(cur->nx[i]){
calc(cur->nx[i]);
cur->dp += cur->nx[i]->dp;
cur->dp1 = max(cur->dp1,cur->nx[i]->dp1 + 1);
}
}
}
void go(trie *cur){
if(cur->ok){
res.push_back('P');
}
int p[26];
iota(p,p + 26,0);
sort(p,p + 26,[&](int x,int y){
return sz(cur->nx[x]) < sz(cur->nx[y]);
});
for(auto i:p)
{
if(cur->nx[i]){
res.push_back((char)(i + 'a'));
go(cur->nx[i]);
}
}
res.push_back('-');
}
void test(){
cin >> n;
while(n--){
string s;
cin >> s;
add(s,root);
}
calc(root);
go(root);
while(!res.empty() && res.back() != 'P') res.pop_back();
cout << res.size() << '\n';
for(auto i:res){
cout << i << '\n';
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
freopen("printer.in","r",stdin);
freopen("printer.out","w",stdout);
int T = 1;
// cin >> T;
for(int test_case = 1;test_case <= T;test_case++){
test();
}
}
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... |