Submission #1083549

# Submission time Handle Problem Language Result Execution time Memory
1083549 2024-09-03T13:18:49 Z SulA Type Printer (IOI08_printer) C++17
100 / 100
121 ms 99512 KB
#include <bits/stdc++.h>
using namespace std;

struct trie {
    trie* child[26] = {};
    bool end = false;

    void insert(const string& s, int ind = 0) {
        if (ind == s.size()) {
            end = true;
        } else {
            if (child[ s[ind] - 'a' ] == nullptr) child[ s[ind] - 'a' ] = new trie();
            child[ s[ind] - 'a' ]->insert(s, ind + 1);
        }
    }

    string dfs(const string& longest, int ind = 0, bool branch = true) {
        string oper = "";
        if (end) {
            oper += "P";
        }
        if (branch) {
            for (int i = 0; i < 26; i++) if (i + 'a' != longest[ind] && child[i] != nullptr) {
                oper += char(i + 'a');
                oper += child[i]->dfs(longest, ind + 1, false);
                oper += "-";
            }
            if (ind < longest.size()) {
                oper += longest[ind] + child[ longest[ind] - 'a' ]->dfs(longest, ind + 1, true);
            }
        } else {
            for (int i = 0; i < 26; i++) if (child[i] != nullptr) {
                oper += char(i + 'a');
                oper += child[i]->dfs(longest, ind + 1, false);
                oper += "-";
            }
        }
        return oper;
    }
};

trie* root = new trie();

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    int n; cin >> n;
    string longest;
    while (n--) {
        string s; cin >> s;
        root->insert(s);
        if (s.size() > longest.size()) {
            swap(s, longest);
        }
    }
    string ans = root->dfs(longest);
    cout << ans.size() << "\n";
    for (char c : ans) cout << c << "\n";
}

Compilation message

printer.cpp: In member function 'void trie::insert(const string&, int)':
printer.cpp:9:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    9 |         if (ind == s.size()) {
      |             ~~~~^~~~~~~~~~~
printer.cpp: In member function 'std::string trie::dfs(const string&, int, bool)':
printer.cpp:28:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |             if (ind < longest.size()) {
      |                 ~~~~^~~~~~~~~~~~~~~~
# 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 1 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 1 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 2 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1884 KB Output is correct
2 Correct 3 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5976 KB Output is correct
2 Correct 14 ms 12632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 15180 KB Output is correct
2 Correct 6 ms 3552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 36676 KB Output is correct
2 Correct 101 ms 83744 KB Output is correct
3 Correct 67 ms 43344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 28800 KB Output is correct
2 Correct 121 ms 99512 KB Output is correct
3 Correct 56 ms 49216 KB Output is correct
4 Correct 102 ms 93980 KB Output is correct