답안 #365340

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
365340 2021-02-11T13:41:30 Z Ahmad_Hasan Type Printer (IOI08_printer) C++17
100 / 100
415 ms 190820 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()
{
    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:84:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |     for(int i=0;i<ans.size();i++)
      |                 ~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 768 KB Output is correct
2 Correct 4 ms 1900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3308 KB Output is correct
2 Correct 11 ms 4076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 11372 KB Output is correct
2 Correct 55 ms 23788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 28008 KB Output is correct
2 Correct 25 ms 6508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 168 ms 69988 KB Output is correct
2 Correct 352 ms 160352 KB Output is correct
3 Correct 195 ms 83264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 143 ms 54872 KB Output is correct
2 Correct 415 ms 190820 KB Output is correct
3 Correct 226 ms 94436 KB Output is correct
4 Correct 383 ms 180196 KB Output is correct