Submission #965351

# Submission time Handle Problem Language Result Execution time Memory
965351 2024-04-18T11:13:03 Z noyancanturk Type Printer (IOI08_printer) C++17
100 / 100
129 ms 99784 KB
#include "bits/stdc++.h"
using namespace std;
    
#define int int64_t
#define pb push_back
    
const int lim=1e6+100;
const int mod=1e9+7;
 
using pii=pair<int,int>;

vector<char>ans;

struct trie{
    struct node{
        int c[26],depth;
        bool have;
    };
    
    node nds[lim];
    int nextunused=2;

    void insert(const string&s){
        int cur=1;
        int i=0;
        while(i<s.size()){
            if(nds[cur].c[s[i]-'a']==0){
                nds[cur].c[s[i]-'a']=nextunused++;
            }
            cur=nds[cur].c[s[i++]-'a'];
        }
        nds[cur].have=1;
    }

    struct iterator{
        trie*par;
        int cur;
        iterator(trie*par,int b):par(par),cur(b){}
        node* operator()(){
            return par->nds+cur;
        }
        void operator+=(char cc){
            cur=par->nds[cur].c[cc-'a'];
        }
        bool operator==(const iterator&it){
            return par==it.par&&cur==it.cur;
        }
    };

    const iterator begin() {
        return iterator(this,1);
    };

    const iterator end(){
        return iterator(this,0);
    }

    int depdfs(node*p){
        if(!p)return 0;
        p->depth=1;
        for(int i=0;i<26;i++){
            if(p->c[i])p->depth=max(p->depth,1+depdfs(nds+p->c[i]));
        }
        return p->depth;
    }

    void dfs(node*p,bool nope){
        if(!p)return;
        if(p->have)ans.pb('P');
        int maxchild=0,maxind=-1;
        if(!nope)for(int i=0;i<26;i++){
            if(nds[maxchild].depth<nds[p->c[i]].depth){
                maxchild=p->c[i];
                maxind=i;
            }
        }
        for(int i=0;i<26;i++){
            if(p->c[i]!=maxchild&&p->c[i]){
                ans.pb(i+'a');
                dfs(nds+p->c[i],1);
                ans.pb('-');
            }
        }
        if(maxchild){
            ans.pb(maxind+'a');
            dfs(nds+maxchild,0);
        }
    }


}tr;

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Local  
    freopen(".in","r",stdin); freopen(".out","w",stdout);
#endif
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        string s;
        cin>>s;
        tr.insert(s);
    }
    tr.depdfs(tr.nds+1);
    tr.dfs(tr.nds+1,0);
    cout<<ans.size()<<"\n";
    for(char c:ans){
        cout<<c<<"\n";
    }
}

Compilation message

printer.cpp: In member function 'void trie::insert(const string&)':
printer.cpp:26:16: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |         while(i<s.size()){
      |               ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 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 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 1112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1884 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5980 KB Output is correct
2 Correct 16 ms 12640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 14812 KB Output is correct
2 Correct 6 ms 3416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 36660 KB Output is correct
2 Correct 109 ms 83660 KB Output is correct
3 Correct 57 ms 43216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 28712 KB Output is correct
2 Correct 129 ms 99784 KB Output is correct
3 Correct 69 ms 49164 KB Output is correct
4 Correct 102 ms 94144 KB Output is correct