Submission #889089

# Submission time Handle Problem Language Result Execution time Memory
889089 2023-12-18T19:45:59 Z Macker Type Printer (IOI08_printer) C++17
100 / 100
139 ms 65476 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define all(v) v.begin(), v.end()

struct node{
    map<char, node*> m = {};
    int mxd = 0;
    node* mxch = NULL;
    bool end = 0;
};

void add(string s, int i, node* cur){
    if(i == s.size()){
        cur->end = true;
        return;
    }
    if(cur->m.find(s[i]) == cur->m.end()){
        cur->m[s[i]] = new node();
    }
    add(s, i + 1, cur->m[s[i]]);
}

void depthDfs(node* a, int d){
    if(a->m.size() == 0){
        a->mxd = d;
    }
    else{
        for (auto &b : a->m) {
            depthDfs(b.second, d + 1);
            int nd = b.second->mxd;
            if(nd > a->mxd){
                a->mxd = nd;
                a->mxch = b.second;
            }
        }
    }
}


vector<char> res;
void resDfs(node* a, bool first){
    char c;
    if(a->end) res.push_back('P');
    for (auto &b : a->m) {
        if(b.second == a->mxch && first){
            c = b.first;
            continue;
        }
        res.push_back(b.first);
        resDfs(b.second, false);
        res.push_back('-');
    }
    if(first && a->mxch){
        res.push_back(c);
        resDfs(a->mxch, true);
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n; cin >> n;
    node* root = new node();
    for (int i = 0; i < n; i++) {
        string s; cin >> s;
        add(s, 0, root);
    }

    depthDfs(root, 0);
    resDfs(root, true);

    cout << res.size() << endl;
    for (auto &i : res)
        cout << i <<"\n";
}

Compilation message

printer.cpp: In function 'void add(std::string, int, node*)':
printer.cpp:15:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |     if(i == s.size()){
      |        ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1372 KB Output is correct
2 Correct 2 ms 1628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 3932 KB Output is correct
2 Correct 14 ms 8536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 9936 KB Output is correct
2 Correct 9 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 24012 KB Output is correct
2 Correct 117 ms 54708 KB Output is correct
3 Correct 70 ms 28180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 18832 KB Output is correct
2 Correct 139 ms 65476 KB Output is correct
3 Correct 98 ms 32404 KB Output is correct
4 Correct 104 ms 61648 KB Output is correct