Submission #416000

# Submission time Handle Problem Language Result Execution time Memory
416000 2021-06-01T19:34:32 Z fvogel499 Type Printer (IOI08_printer) C++14
100 / 100
158 ms 134096 KB
/*
File created on 06/01/2021 at 20:53:10.
Link to problem: https://oj.uz/problem/view/IOI08_printer
Description: use a trie structures
Time complexity: O(N)
Space complexity: O(N)
Status: DEV
Copyright: Ⓒ 2021 Francois Vogel
*/

#include <iostream>
#include <cmath>
#include <vector>
#include <bitset>
#include <queue>
#include <cstring>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>

using namespace std;

#define pii pair<int, int>
#define f first
// #define s second

#define pb push_back
#define ins insert
#define cls clear

#define ll long long

string steps;
int proc;

struct Node {
    Node(char lt, int ld) {
        t = lt;
        d = ld;
        md = d;
        mn = nullptr;
        words = 0;
        for (int i = 0; i < 26; i++) exist[i] = nullptr;
    }
    void add(string& s, int k) {
        if (k == s.size()) {
            words++;
            return;
        }
        int i = s[k]-'a';
        if (exist[i] == nullptr) {
            exist[i] = new Node(char(i)+'a', d+1);
            c.pb(exist[i]);
        }
        exist[i]->add(s, k+1);
        if (exist[i]->md > md) {
            md = exist[i]->md;
            mn = exist[i];
        }
    }
    int words, d, md;
    char t;
    Node* exist [26];
    vector<Node*> c;
    Node* mn;
};

void dfs(Node* i) {
    for (int j = 0; j < i->words; j++) {
        steps += "P\n";
        proc--;
    }
    for (Node* j : i->c) {
        if (j != i->mn) {
            steps += j->t;
            steps += "\n";
            dfs(j);
            steps += "-\n";
        }
    }
    if (i->mn == nullptr) return;
    steps += i->mn->t;
    steps += '\n';
    dfs(i->mn);
    if (proc != 0) {
        steps += "-\n";
    }
}

signed main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);

    Node* root = new Node('$', 0);

    int n;
    cin >> n;
    string ls;
    for (int i = 0; i < n; i++) {
        cin >> ls;
        root->add(ls, 0);
    }

    proc = n;
    dfs(root);

    cout << steps.size()/2 << endl;
    cout << steps;

    int d = 0;
    d++;
}

Compilation message

printer.cpp: In member function 'void Node::add(std::string&, int)':
printer.cpp:48:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |         if (k == s.size()) {
      |             ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 284 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 452 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 3 ms 1356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2380 KB Output is correct
2 Correct 4 ms 2892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 7876 KB Output is correct
2 Correct 22 ms 16696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 19684 KB Output is correct
2 Correct 9 ms 4420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 49068 KB Output is correct
2 Correct 138 ms 112696 KB Output is correct
3 Correct 74 ms 57852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 38144 KB Output is correct
2 Correct 158 ms 134096 KB Output is correct
3 Correct 76 ms 65664 KB Output is correct
4 Correct 151 ms 126632 KB Output is correct