답안 #985478

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
985478 2024-05-17T23:47:19 Z PagodePaiva Type Printer (IOI08_printer) C++17
100 / 100
179 ms 106440 KB
#include<bits/stdc++.h>

using namespace std;

int n;
int cnt;
vector <char> v;

struct Trie{
    Trie *child[26];
    int prof, tam, acab;
    Trie(){
        prof = -1;
        tam = 0;
        acab = 0;
        for(int i = 0;i < 26;i++) child[i] = NULL;
    }
    void add(string s, int pos){
        if(pos == s.size()){
            acab++;
            return;
        }
        if(!child[s[pos]-'a']) child[s[pos]-'a'] = new Trie();
        child[s[pos]-'a']->add(s, pos+1);
        if(prof == -1 or child[s[pos]-'a']->tam+1>child[prof]->tam){
            prof = s[pos]-'a';
            tam = child[s[pos]-'a']->tam+1;
        } 
        return;
    }
    void query(int typ){
        while(acab > 0){
            acab--;
            v.push_back('P');
            cnt++;
            if(cnt == n){
                cout << v.size() << '\n';
                for(auto x : v){
                    cout << x << '\n';
                }
                exit(0);
            }
        }
        bool aux = true;
        for(int i = 0;i < 26;i++){
            if(child[i]) aux = false;
        }
        if(aux) return;
        if(typ == 0){
            for(int i = 0;i < 26;i++){
                if(child[i] and i != prof){
                    v.push_back(i+'a');
                    child[i]->query(1);
                    v.push_back('-');
                }
            }
            if(prof == -1) return;
            v.push_back(prof+'a');
            child[prof]->query(0);
            return;
        }
        else{
            for(int i = 0;i < 26;i++){
                if(child[i]){
                    v.push_back(i+'a');
                    child[i]->query(1);
                    v.push_back('-');
                }
            }
        }
    }
} trie;

int main(){
    cin >> n;
    for(int i = 0;i < n;i++){
        string s;
        cin >> s;
        trie.add(s, 0);
    }
    trie.query(0);
    return 0;
}

Compilation message

printer.cpp: In member function 'void Trie::add(std::string, int)':
printer.cpp:19:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |         if(pos == s.size()){
      |            ~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 2 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1884 KB Output is correct
2 Correct 3 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6236 KB Output is correct
2 Correct 18 ms 13352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 15572 KB Output is correct
2 Correct 11 ms 3672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 38996 KB Output is correct
2 Correct 124 ms 89540 KB Output is correct
3 Correct 75 ms 46140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 30416 KB Output is correct
2 Correct 179 ms 106440 KB Output is correct
3 Correct 84 ms 52372 KB Output is correct
4 Correct 130 ms 100480 KB Output is correct