Submission #242876

# Submission time Handle Problem Language Result Execution time Memory
242876 2020-06-29T14:57:47 Z aryan12 Type Printer (IOI08_printer) C++17
100 / 100
234 ms 64248 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;
        alphabet.clear();
    }
} *root;

void trie_insert(string s) {
    Node *curr = root;
    for(int i = 0; i < s.size(); i++) {
        if(!curr->alphabet.count(s[i] - 'a')) {
            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) {
    unordered_map<int, Node*> :: iterator itr;
    for(itr = curr->alphabet.begin(); itr != curr->alphabet.end(); itr++) {
        s += (char)(itr->first + 'a');
        trie_Search(curr->alphabet[itr->first], s);
        s.pop_back();
    }
    if(max_length.size() < s.size())
        max_length = s;
}

char ans[1000010] = {0};
int currPos = 0;

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

void Solve() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    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 << currPos << "\n";
    for(int i = 0; i < currPos; i++) {
        cout << (char)(ans[i]) << "\n";
    }
}

int main() {
    Solve();
    return 0;
}

Compilation message

printer.cpp: In function 'void trie_insert(std::__cxx11::string)':
printer.cpp:19: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:47:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(idx == max_length.size()) {
        ~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 256 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 6 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1280 KB Output is correct
2 Correct 8 ms 1536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 3968 KB Output is correct
2 Correct 29 ms 8056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 9464 KB Output is correct
2 Correct 16 ms 2304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 23596 KB Output is correct
2 Correct 199 ms 54008 KB Output is correct
3 Correct 104 ms 28024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 18168 KB Output is correct
2 Correct 234 ms 64248 KB Output is correct
3 Correct 126 ms 31740 KB Output is correct
4 Correct 208 ms 61060 KB Output is correct