Submission #242712

# Submission time Handle Problem Language Result Execution time Memory
242712 2020-06-29T05:52:16 Z aryan12 Type Printer (IOI08_printer) C++17
80 / 100
1000 ms 262148 KB
//https://oj.uz/problem/view/IOI08_printer
#include <bits/stdc++.h>
using namespace std;

struct Node {
    bool leaf;
    int ending_strings;
    unordered_map<int, Node*> alphabet;

    Node() {
        leaf = false;
        ending_strings = 0;
        for(int i = 0; i < 26; i++) {
            alphabet[i] = NULL;
        }
    }
} *root;

void trie_insert(string s) {
    Node *curr = root;
    for(int i = 0; i < s.size(); i++) {
        if(curr->alphabet[s[i] - 'a'] == NULL) {
            curr->alphabet[s[i] - 'a'] = new Node();
        }
        curr = curr->alphabet[s[i] - 'a'];
    }
    curr->ending_strings++;
    curr->leaf = true;
}

string max_length = "";

void trie_Search(Node *curr, string s) {
    for(int i = 0; i < 26; i++) {
        if(curr->alphabet[i] != NULL) {
            s += (char)(i + 'a');
            trie_Search(curr->alphabet[i], s);
            s.pop_back();
        }
    }
    if(max_length.size() < s.size())
        max_length = s;
}

string ans = "";

void FindAnswer(Node *curr, bool edge, int idx) {
    if(idx == max_length.size()) {
        for(int i = 0; i < curr->ending_strings; i++) {
            ans += "P";
        }
        return;
    }
    //cout << "Inside the loop" << endl;
    if(curr->leaf == true) {
        for(int i = 0; i < curr->ending_strings; i++) {
            ans += "P";
        }
    }
    for(int i = 0; i < 26; i++) {
        //cout << i << " ";
        if(edge && i == max_length[idx] - 'a')
            continue;
        if(curr->alphabet[i] != NULL) {
            ans += (i + 'a');
            FindAnswer(curr->alphabet[i], false, idx + 1);
            ans += "-";
        }
    }
    //cout << "Have come out of the " << idx << " loop" << endl;
    if(edge) {
        int x = max_length[idx] - 'a';
        ans += max_length[idx];
        FindAnswer(curr->alphabet[x], true, idx + 1);
    }
}

void Solve() {
    int n;
    cin >> n;
    root = new Node();
    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;
        trie_insert(s);
    }
    string s = "";
    trie_Search(root, s);
    FindAnswer(root, true, 0);
    cout << ans.size() << endl;
    for(int i = 0; i < ans.size(); i++) {
        cout << (char)(ans[i]) << endl;
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    Solve();
    return 0;
}

Compilation message

printer.cpp: In function 'void trie_insert(std::__cxx11::string)':
printer.cpp:21:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < s.size(); i++) {
                    ~~^~~~~~~~~~
printer.cpp: In function 'void FindAnswer(Node*, bool, int)':
printer.cpp:48:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(idx == max_length.size()) {
        ~~~~^~~~~~~~~~~~~~~~~~~~
printer.cpp: In function 'void Solve()':
printer.cpp:91:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < ans.size(); i++) {
                    ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1024 KB Output is correct
2 Correct 28 ms 4864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 8448 KB Output is correct
2 Correct 65 ms 10744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 182 ms 30456 KB Output is correct
2 Correct 388 ms 65400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 452 ms 77188 KB Output is correct
2 Correct 130 ms 15992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1105 ms 194176 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 890 ms 151188 KB Output is correct
2 Runtime error 492 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -