답안 #636143

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
636143 2022-08-28T09:41:27 Z tvladm2009 Type Printer (IOI08_printer) C++14
100 / 100
214 ms 105488 KB
#include <iostream>

using namespace std;

const int SIGMA = 26;

struct Trie {
    Trie *sons[SIGMA];
    int freq;
    int words;
    bool is_ancestor;

    Trie() {
        freq = 0;
        words = 0;
        is_ancestor = false;
        for (int i = 0; i < SIGMA; i++) {
            sons[i] = NULL;
        }
    }
};

void ins(Trie *&root, string s, int pos = 0) {
    if (pos == s.size()) {
        root->words++;
    } else {
        if (root->sons[s[pos] - 'a'] == NULL) {
            root->sons[s[pos] - 'a'] = new Trie();
        }
        ins(root->sons[s[pos] - 'a'], s, pos + 1);
    }
}

int n, answer = 0;

void stramosi(Trie *&root, string s, int pos = 0) {
    if (pos != s.size()) {
        root->sons[s[pos] - 'a']->is_ancestor = true;
        stramosi(root->sons[s[pos] - 'a'], s, pos + 1);
    }
}

void solve(Trie *&root) {
    answer += root->words;
    for (int i = 0; i < SIGMA; i++) {
        if (root->sons[i] != NULL && root->sons[i]->is_ancestor == false) {
            answer++;
            solve(root->sons[i]);
            answer++;
        }
    }
    for (int i = 0; i < SIGMA; i++) {
        if (root->sons[i] != NULL && root->sons[i]->is_ancestor == true) {
            answer++;
            solve(root->sons[i]);
        }
    }
}

void dfs(Trie *&root) {
    for (int i = 1; i <= root->words; i++) {
        cout << "P\n";
    }
    for (int i = 0; i < SIGMA; i++) {
        if (root->sons[i] != NULL && root->sons[i]->is_ancestor == false) {
            cout << char(i + 'a') << "\n";
            dfs(root->sons[i]);
            cout << "-\n";
        }
    }
    for (int i = 0; i < SIGMA; i++) {
        if (root->sons[i] != NULL && root->sons[i]->is_ancestor == true) {
            cout << char(i + 'a') << "\n";
            dfs(root->sons[i]);
        }
    }
}

int main() {
    cin >> n;
    Trie *root = new Trie();
    string tmp = "";
    for (int i = 1; i <= n; i++) {
        string s;
        cin >> s;
        if (s.size() > tmp.size()) {
            tmp = s;
        }
        ins(root, s);
    }
    stramosi(root, tmp);
    solve(root);
    cout << answer << "\n";
    dfs(root);
    return 0;
}

Compilation message

printer.cpp: In function 'void ins(Trie*&, std::string, int)':
printer.cpp:24:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     if (pos == s.size()) {
      |         ~~~~^~~~~~~~~~~
printer.cpp: In function 'void stramosi(Trie*&, std::string, int)':
printer.cpp:37:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |     if (pos != s.size()) {
      |         ~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1840 KB Output is correct
2 Correct 5 ms 2260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 6200 KB Output is correct
2 Correct 26 ms 13072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 15444 KB Output is correct
2 Correct 13 ms 3384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 38668 KB Output is correct
2 Correct 162 ms 88516 KB Output is correct
3 Correct 102 ms 45572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 30204 KB Output is correct
2 Correct 214 ms 105488 KB Output is correct
3 Correct 121 ms 51712 KB Output is correct
4 Correct 173 ms 99436 KB Output is correct