Submission #889356

# Submission time Handle Problem Language Result Execution time Memory
889356 2023-12-19T11:53:50 Z dimashhh Type Printer (IOI08_printer) C++17
100 / 100
203 ms 120140 KB
#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();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 2 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2140 KB Output is correct
2 Correct 5 ms 2808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 7256 KB Output is correct
2 Correct 25 ms 14980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 17872 KB Output is correct
2 Correct 8 ms 3932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 43864 KB Output is correct
2 Correct 175 ms 101060 KB Output is correct
3 Correct 87 ms 52280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 34316 KB Output is correct
2 Correct 203 ms 120140 KB Output is correct
3 Correct 98 ms 59084 KB Output is correct
4 Correct 178 ms 113596 KB Output is correct