Submission #365341

# Submission time Handle Problem Language Result Execution time Memory
365341 2021-02-11T13:43:13 Z Ahmad_Hasan Type Printer (IOI08_printer) C++17
100 / 100
388 ms 190308 KB
#include <bits/stdc++.h>
#define int long long
/**
     ||||||||||       |||||     |||||    ||||||||||
    |||||||||||||     |||||     |||||  |||||
   ||||     ||||||    |||||     |||||  |||||
  |||||||||||||||||   |||||||||||||||    ||||||||||
 |||||||||||||||||||  |||||||||||||||           |||||
 |||||         |||||  |||||     |||||           |||||
 |||||         |||||  |||||     |||||    ||||||||||
AHMED;HASSAN;SAEED;
*/

using namespace std;
vector<char>ans;
struct trie{
    trie* exit[26];
    int cnt[26];
    bool leaf=0;

    trie(){
        memset(exit,0,sizeof(exit));
        memset(cnt,0,sizeof(cnt));
    }

    int insert(string &s,int cr=0){
        if(cr==s.size()){
            leaf=1;
            int mx=-1;
            for(int i=0;i<26;i++){
                mx=max(mx,cnt[i]);
            }
            return mx+1;
        }
        if(exit[s[cr]-'a']==0)
            exit[s[cr]-'a']=new trie;
        cnt[s[cr]-'a']=exit[s[cr]-'a']->insert(s,cr+1);
        int mx=-1;
        for(int i=0;i<26;i++){
            mx=max(mx,cnt[i]);
        }
        return mx+1;
    }

    void print(){
        if(leaf){
            ///cout<<'P'<<'\n';
            ans.push_back('P');
        }
        vector<pair<int,int> >vps(26);
        for(int i=0;i<26;i++){
            vps[i]={cnt[i],i};
        }
        sort(vps.begin(),vps.end());
        for(int j=0;j<26;j++){
            int i=vps[j].second;
            if(exit[i]!=0){
                ///cout<<(char)('a'+i)<<'\n';
                ans.push_back((char)('a'+i));
                exit[i]->print();
            }
        }
        ///cout<<'-'<<'\n';
        ans.push_back('-');

    }


};

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);      cout.tie(0);
    trie tr;
    int n;
    cin>>n;
    vector<string>vs(n);
    for(int i=0;i<n;i++){
        cin>>vs[i];
        tr.insert(vs[i]);
    }
    tr.print();
    while(ans.back()=='-')ans.pop_back();
    cout<<ans.size()<<'\n';
    for(int i=0;i<ans.size();i++)
        cout<<ans[i]<<'\n';

    return 0;
}

Compilation message

printer.cpp: In member function 'long long int trie::insert(std::string&, long long int)':
printer.cpp:27:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |         if(cr==s.size()){
      |            ~~^~~~~~~~~~
printer.cpp: In function 'int32_t main()':
printer.cpp:86:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |     for(int i=0;i<ans.size();i++)
      |                 ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 620 KB Output is correct
2 Correct 4 ms 1900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3308 KB Output is correct
2 Correct 8 ms 4076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 11116 KB Output is correct
2 Correct 54 ms 23680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 27880 KB Output is correct
2 Correct 24 ms 6380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 157 ms 69860 KB Output is correct
2 Correct 373 ms 159848 KB Output is correct
3 Correct 176 ms 82788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 54628 KB Output is correct
2 Correct 388 ms 190308 KB Output is correct
3 Correct 234 ms 94052 KB Output is correct
4 Correct 360 ms 179812 KB Output is correct