Submission #635207

# Submission time Handle Problem Language Result Execution time Memory
635207 2022-08-25T16:36:56 Z ojoConmigo Type Printer (IOI08_printer) C++17
20 / 100
235 ms 36928 KB
#include <bits/stdc++.h>
using namespace std;

struct Trie{
    struct Trie *child[26];
    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]){
            curr->child[index] = createNode(key[i]);
        }
        if(masLarga){
            struct Trie *sig = curr->child[index];
            sig->longest = true;
        }
        curr = curr->child[index];
    }
    if(masLarga){
        curr->longest = true;
    }
    curr->endOfWord = true;
}

bool isEmpty(struct Trie *root){
    struct Trie *curr = root;
    for(int i=0; i<26; i++){
        if(curr->child[i]){
            return false;
        }
    }
    return 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]){
            struct Trie *sig = curr->child[i];
            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]){
            struct Trie *sig = curr->child[i];
            if(sig->longest){
                seguimos = sig;
                continue;
            }
            cout << sig->letra << endl;
            printing(sig);
        }
    }

    if(seguimos){
        cout << seguimos->letra << endl;
        printing(seguimos);
        return;
    }

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

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

    int n;
    cin >> n;
    struct Trie *root = createNode('0');
    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(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 << counting(root)+n-2 << endl;
    printing(root);
}

Compilation message

printer.cpp: In function 'int main()':
printer.cpp:108:21: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  108 |         if(s.size() > longest.first){
      |            ~~~~~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 1 ms 212 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 444 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 11 ms 1736 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 38 ms 5924 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 93 ms 14860 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 235 ms 36928 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 187 ms 29016 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -