답안 #499100

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
499100 2021-12-27T08:40:20 Z rampu Type Printer (IOI08_printer) C++14
100 / 100
94 ms 77052 KB
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int n;
struct node {
    bool w = false;
    bool l = false;
    int next[26];
};
node trie[700000];
int t = 0;

int maxi;
string word, larger;

void insert(const string &word, bool p) {
    int node = 0;
    for (int i = 0; i < word.size(); i++) {
        int letter = (word[i] - 'a');
        trie[node].next[letter] = trie[node].next[letter] ? trie[node].next[letter] : ++t;
        node = trie[node].next[letter];
        trie[node].l = p;
    }
    trie[node].w = true;
    trie[node].l = p;
}

string ans;

void dfs(int node) {
    if (trie[node].w) ans += "P";
    int pending = -1, next_node;
    for (int i = 0; i < 26; i++) {
        if (trie[node].next[i]) {
            next_node = trie[node].next[i];
            if (trie[next_node].l) {
                pending = i;
                continue;
            }
            ans.push_back(i + 'a');
            dfs(next_node);
            ans += "-";
        }
    }

    if (pending != -1) {
        ans.push_back(pending + 'a');
        dfs(trie[node].next[pending]);
    }
}

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

    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> word;
        insert(word, 0);
        if (word.size() >= maxi) {
            maxi = word.size();
            larger = word;
        }
    }
    insert(larger, 1);
    dfs(0);
    cout << ans.size() << '\n';
    for(int i = 0; i < ans.size(); i++)
        cout << ans[i] << '\n';

    return 0;
}

Compilation message

printer.cpp: In function 'void insert(const string&, bool)':
printer.cpp:20:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |     for (int i = 0; i < word.size(); i++) {
      |                     ~~^~~~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:62:25: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   62 |         if (word.size() >= maxi) {
      |             ~~~~~~~~~~~~^~~~~~~
printer.cpp:70:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for(int i = 0; i < ans.size(); i++)
      |                    ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 74188 KB Output is correct
2 Correct 29 ms 74244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 74216 KB Output is correct
2 Correct 27 ms 74236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 74204 KB Output is correct
2 Correct 28 ms 74284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 74180 KB Output is correct
2 Correct 28 ms 74232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 74240 KB Output is correct
2 Correct 28 ms 74316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 74324 KB Output is correct
2 Correct 32 ms 74336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 74484 KB Output is correct
2 Correct 36 ms 74700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 74736 KB Output is correct
2 Correct 34 ms 74444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 75344 KB Output is correct
2 Correct 84 ms 76536 KB Output is correct
3 Correct 59 ms 75588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 75136 KB Output is correct
2 Correct 94 ms 77052 KB Output is correct
3 Correct 72 ms 75752 KB Output is correct
4 Correct 87 ms 76844 KB Output is correct