답안 #640790

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
640790 2022-09-15T09:09:08 Z ojoConmigo Type Printer (IOI08_printer) C++17
100 / 100
245 ms 97980 KB
#include <bits/stdc++.h>
using namespace std;

struct Trie{
    //struct Trie *child[26];
    unordered_map<char,Trie*> child;
    char letra;
    bool endOfWord,longest;
};

struct Trie *createNode(char letra){
    struct Trie *nodo = new Trie;
    /*
    for(int i=0; i<26; i++){
        nodo->child[i] = NULL;
    }
    */
    nodo->endOfWord = false;
    nodo->longest = false;
    nodo->letra = letra;
    return nodo;
}

void insert(struct Trie *root, string key, bool masLarga){
    struct Trie *curr = root;
    for(int i=0; i<(int)key.size(); i++){
        /*
        int index = key[i] - 'a';
        if(curr->child[index] == NULL){
            curr->child[index] = createNode(key[i]);
        }
        */
        if(curr->child.find(key[i]) == curr->child.end()){
            curr->child[key[i]] = createNode(key[i]);
        }
        //curr = curr->child[index];
        curr = curr->child[key[i]];
        if(masLarga) curr->longest = true;
    }
    curr->endOfWord = true;
}

int counting(struct Trie *root){
    struct Trie *curr = root;
    int cont = 2;
    if(curr->longest){
        cont--;
    }
    /*
    for(int i=0; i<26; i++){
        if(curr->child[i] != NULL){
            struct Trie *sig = curr->child[i];
            cont += counting(sig);
        }
    }
    */
    for(auto it = curr->child.begin(); it != curr->child.end(); it++){
        struct Trie *sig = it->second;
        cont += counting(sig);
    }
    return cont;
}

void printing(struct Trie *root){
    struct Trie *curr = root;
    //cout << curr->letra << endl;
    struct Trie *seguimos = NULL;
    /*
    for(int i=0; i<26; i++){
        if(curr->child[i] != NULL){
        //cout << sizeof(*(curr->child[i])) << " " << sizeof(Trie) << endl;
        //if(sizeof(*(curr->child[i])) == sizeof(Trie)){
            struct Trie *sig = curr->child[i];
            if(sig->longest){
                seguimos = sig;
            }else{
                cout << sig->letra << endl;
                printing(sig);
            }
        }
    }
    */
    for(auto it = curr->child.begin(); it != curr->child.end(); it++){
        struct Trie *sig = it->second;
        if(sig->longest){
            seguimos = sig;
        }else{
            cout << sig->letra << "\n";
            printing(sig);
        }
    }

    if(curr->endOfWord){
        cout << 'P' << "\n";
    }

    if(seguimos != NULL){
    //cout << sizeof(*seguimos) << " " << sizeof(Trie) << endl;
    //if(sizeof(*seguimos) == sizeof(Trie)){
        cout << seguimos->letra << "\n";
        printing(seguimos);
        return;
    }

    if(!curr->longest) cout << '-' << "\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    struct Trie *root = createNode('0');
    root->longest = true;
    vector<string> v(n);
    pair<int,int> longest;
    longest.first = 1;
    longest.second = 0;
    for(int i=0; i<n; i++){
        string s;
        cin >> s;
        v[i] = s;
        if((int)s.size() > longest.first){
            longest.first = s.size();
            longest.second = i;
        }
    }

    for(int i=0; i<n; i++){
        if(i == longest.second){
            insert(root,v[i],true);
        }else insert(root,v[i],false);
    }
    //cout << sizeof(Trie) << endl;
    //cout << sizeof(*root) << endl;
    cout << counting(root)+n-1 << "\n";
    printing(root);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 1092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1740 KB Output is correct
2 Correct 5 ms 2132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 5588 KB Output is correct
2 Correct 30 ms 11968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 13972 KB Output is correct
2 Correct 13 ms 3796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 35200 KB Output is correct
2 Correct 203 ms 82204 KB Output is correct
3 Correct 114 ms 42212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 26572 KB Output is correct
2 Correct 245 ms 97980 KB Output is correct
3 Correct 125 ms 47816 KB Output is correct
4 Correct 215 ms 92352 KB Output is correct