Submission #716345

# Submission time Handle Problem Language Result Execution time Memory
716345 2023-03-29T17:56:00 Z kartapolov Type Printer (IOI08_printer) C++14
90 / 100
1000 ms 119988 KB
#include<bits/stdc++.h>
using namespace std;

struct vertex {
    bool term = 0;
    vector<vertex*> children = vector<vertex*>(26, nullptr);
    vertex() = default;
    void add(const string& s, int i = 0) {
        if (this->children[s[i] - 'a'] == nullptr) {
            this->children[s[i] - 'a'] = new vertex();
        }
        if (i + 1 == s.size()) {
            (this->children[s[i] - 'a']->term) = true;
        } else {
            this->children[s[i] - 'a']->add(s, i + 1);
        }
    }
};

vector<char> ans;
string maxw = "";
int n, k;
void dfs(vertex* v, int h = 0) {
    if (v->term) {
        ans.push_back('P');
        ++k;
    }
    for (int i = 0; i < 26; ++i) {
        if (!(h < maxw.size() and i == maxw[h] - 'a') and (v->children[i] != nullptr)) {
            ans.push_back(i + 'a');
            dfs(v->children[i], h + 1);
            if (k != n) ans.push_back('-');
        }
    }
    
    if (h < maxw.size() and v->children[maxw[h] - 'a'] != nullptr) {
        ans.push_back(maxw[h]);
        dfs(v->children[maxw[h] - 'a'], h + 1);
        if (k != n) ans.push_back('-');
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> n;
    vertex root;
    for (int i = 0; i < n; ++i) {
        string word;
        cin >> word;
        if (word.size() > maxw.size()) maxw = word;
        root.add(word);
    }
    k = 0;
    dfs(&root);
    cout << ans.size() << endl;
    for (char c : ans) cout << c << endl;
}

Compilation message

printer.cpp: In member function 'void vertex::add(const string&, int)':
printer.cpp:12:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 |         if (i + 1 == s.size()) {
      |             ~~~~~~^~~~~~~~~~~
printer.cpp: In function 'void dfs(vertex*, int)':
printer.cpp:29:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |         if (!(h < maxw.size() and i == maxw[h] - 'a') and (v->children[i] != nullptr)) {
      |               ~~^~~~~~~~~~~~~
printer.cpp:36:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |     if (h < maxw.size() and v->children[maxw[h] - 'a'] != nullptr) {
      |         ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 11 ms 1220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 2132 KB Output is correct
2 Correct 30 ms 2676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 7144 KB Output is correct
2 Correct 156 ms 15036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 169 ms 17848 KB Output is correct
2 Correct 52 ms 3924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 404 ms 44152 KB Output is correct
2 Correct 977 ms 101184 KB Output is correct
3 Correct 513 ms 52000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 336 ms 34524 KB Output is correct
2 Execution timed out 1078 ms 119988 KB Time limit exceeded
3 Halted 0 ms 0 KB -