답안 #242735

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
242735 2020-06-29T07:01:33 Z aryan12 Type Printer (IOI08_printer) C++17
80 / 100
1000 ms 64148 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;
}

string ans = "";

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 += "P";
        }
        return;
    }
    //cout << "Inside the loop" << endl;
    if(curr->leaf == true) {
        for(int i = 0; i < curr->ending_strings; i++) {
            ans += "P";
        }
    }
    for(itr = curr->alphabet.begin(); itr != curr->alphabet.end(); itr++) {
        //cout << i << " ";
        if(edge && itr->first == max_length[idx] - 'a')
            continue;
        ans += (itr->first + 'a');
        FindAnswer(curr->alphabet[itr->first], 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: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:46:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(idx == max_length.size()) {
        ~~~~^~~~~~~~~~~~~~~~~~~~
printer.cpp: In function 'void Solve()':
printer.cpp:87:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < ans.size(); i++) {
                    ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 384 KB Output is correct
2 Correct 21 ms 1152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 1280 KB Output is correct
2 Correct 45 ms 1664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 114 ms 4076 KB Output is correct
2 Correct 230 ms 8440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 289 ms 9880 KB Output is correct
2 Correct 84 ms 2432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 729 ms 23828 KB Output is correct
2 Execution timed out 1097 ms 54192 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 556 ms 18580 KB Output is correct
2 Execution timed out 1061 ms 64148 KB Time limit exceeded
3 Halted 0 ms 0 KB -