Submission #1069085

# Submission time Handle Problem Language Result Execution time Memory
1069085 2024-08-21T15:44:20 Z codexistent Type Printer (IOI08_printer) C++14
100 / 100
105 ms 112328 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for(ll i = a; i <= b; i++)

struct Trie{
    ll nx[26], ct = 0;
    Trie () {
        FOR(i, 0, 25) nx[i] = -1;
    }
};

ll n, trie_idx = 1, rmv[500005];
Trie trie[500005];
vector<char> r;

void recurse(ll idx){
    FOR(i, 1, trie[idx].ct) r.push_back('P');
    
    FOR(i, 0, 25){
        if(i != rmv[idx] && trie[idx].nx[i] != -1) {
            r.push_back('a' + i);
            recurse(trie[idx].nx[i]);
            r.push_back('-');
        }
    }

    if(rmv[idx] != -1){
        r.push_back('a' + rmv[idx]);
        recurse(trie[idx].nx[rmv[idx]]);
        r.push_back('-');
    }
}

ll ct_rmv(ll idx){
    rmv[idx] = -1;
    ll mx_rmv = -1;
    FOR(i, 0, 25){
        if(trie[idx].nx[i] != -1) {
            ll cti = ct_rmv(trie[idx].nx[i]);
            if(cti >= mx_rmv){
                rmv[idx] = i;
                mx_rmv = cti;
            }
        }
    }

    // cout << idx << " => " << rmv[idx] << endl; 
    if(mx_rmv == -1) return 1;
    return mx_rmv + 1;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    FOR(i, 1, n){
        string s;
        cin >> s;

        ll ptr = 0;
        for(char c : s){
            if(trie[ptr].nx[c - 'a'] == -1){
                trie[ptr].nx[c - 'a'] = trie_idx;
                trie_idx++;
            }
            ptr = trie[ptr].nx[c - 'a'];
        }
        trie[ptr].ct++;
    }

    ct_rmv(0);
    recurse(0);

    while(r.size() && r.back() == '-') r.pop_back();

    cout << r.size() << '\n';
    vector<char> v;
    for(char c : r) {
        cout << c << '\n';
        /*if(c == 'P'){
            for(char c2 : v) cout << c2;
            cout << endl;
        }else if(c == '-'){
            v.pop_back();
        }else{
            v.push_back(c);
        }*/
    }
}

/*
8
swan
swaddle
result
rewind
rewindle 
switch
reswindle
reswindle
*/
# Verdict Execution time Memory Grader output
1 Correct 38 ms 106076 KB Output is correct
2 Correct 30 ms 105892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 106076 KB Output is correct
2 Correct 30 ms 105892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 106068 KB Output is correct
2 Correct 33 ms 106072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 106064 KB Output is correct
2 Correct 31 ms 106072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 106068 KB Output is correct
2 Correct 31 ms 106112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 106064 KB Output is correct
2 Correct 40 ms 106144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 106328 KB Output is correct
2 Correct 41 ms 106832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 107076 KB Output is correct
2 Correct 35 ms 106320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 108272 KB Output is correct
2 Correct 100 ms 111304 KB Output is correct
3 Correct 67 ms 108756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 107988 KB Output is correct
2 Correct 105 ms 112224 KB Output is correct
3 Correct 95 ms 109748 KB Output is correct
4 Correct 90 ms 112328 KB Output is correct