Submission #1175260

#TimeUsernameProblemLanguageResultExecution timeMemory
1175260InvMODType Printer (IOI08_printer)C++20
100 / 100
161 ms147192 KiB
#include<bits/stdc++.h>

using namespace std;

#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()

const int C = 26;

struct Trie{
    struct Node{
        Node* child[C]; bool has_end; int MxLen[C];

        Node(): has_end(false) {
            for(int i = 0; i < C; i++){
                child[i] = nullptr;
                MxLen[i] = 0;
            }
        }
    };
    typedef Node* pNode;

    pNode root; vector<char> ans;
    Trie() {
        root = new Node();
    }

    void add(const string &s){
        pNode cur = root;

        for(const char &x : s){
            int c = int(x - 'a');

            if(cur->child[c] == nullptr){
                cur->child[c] = new Node();
            }
            cur->MxLen[c] = max(cur->MxLen[c], sz(s));
            cur = cur->child[c];
        }
        cur->has_end = true;
    }

    void find_answer(pNode x){
        if(x->has_end) ans.push_back('P');

        vector<int> id(26); iota(all(id), 0);
        sort(all(id), [&](int u, int v){
             return x->MxLen[u] < x->MxLen[v];
        });

        for(int i = 0; i < C; i++){
            if(x->MxLen[id[i]]){
                pNode v = x->child[id[i]];

                ans.push_back(char('a' + id[i]));
                find_answer(v);
            }
        }
        ans.push_back('-');
    }

    void answer(){
        find_answer(root);
        while(ans.back() == '-') ans.pop_back();

        cout << sz(ans) << "\n";
        for(int i = 0; i < sz(ans); i++){
            cout << ans[i] << "\n";
        }
    }
};

void solve()
{
    int n; cin >> n;

    Trie trie;
    for(int i = 0; i < n; i++){
        string s; cin >> s;
        trie.add(s);
    }
    trie.answer();
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP", "r", stdin);
        freopen(name".OUT", "w", stdout);
    }

    solve();
    return 0;
}

Compilation message (stderr)

printer.cpp: In function 'int32_t main()':
printer.cpp:91:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |         freopen(name".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |         freopen(name".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...