답안 #485950

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
485950 2021-11-09T19:49:26 Z Alexandruabcde Type Printer (IOI08_printer) C++14
100 / 100
162 ms 99516 KB
#include <bits/stdc++.h>

using namespace std;

constexpr int SIGMAX = 26;
constexpr int NMAX = 25005;

struct Node {
    Node *Next[SIGMAX];

    int cnt;
    int val;

    Node () {
        for (int i = 0; i < SIGMAX; ++ i )
            Next[i] = nullptr;

        val = 0;
        cnt = 0;
    }
};

Node *Trie = new Node;

void Add (Node *Tree, string cuv, int lit) {
    if (lit == cuv.size()) {
        Tree->val = max(Tree->val, (int)cuv.size());
        Tree->cnt ++;
        return;
    }

    int urm = cuv[lit]-'a';

    if (Tree->Next[urm] == nullptr) {
        Tree->Next[urm] = new Node;

        Add(Tree->Next[urm], cuv, lit+1);
    }
    else Add(Tree->Next[urm], cuv, lit+1);

    Tree->val = max(Tree->val, Tree->Next[urm]->val);
}

int N;
vector <char> Ans;

void Read () {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> N;

    for (int i = 1; i <= N; ++ i ) {
        string S;

        cin >> S;

        Add(Trie, S, 0);
    }
}

void FindAnswer (Node *Tree) {
    for (int i = 0; i < Tree->cnt; ++ i )
        Ans.push_back('P');

    for (int i = 0; i < SIGMAX; ++ i ) {
        if (Tree->Next[i] == nullptr || Tree->Next[i]->val == Tree->val) continue;

        Ans.push_back(('a' + i));
        FindAnswer(Tree->Next[i]);
        Ans.push_back('-');
    }

    for (int i = 0; i < SIGMAX; ++ i ) {
        if (Tree->Next[i] == nullptr || Tree->Next[i]->val < Tree->val) continue;

        Ans.push_back(('a' + i));
        FindAnswer(Tree->Next[i]);
        Ans.push_back('-');
    }
}

void Solve () {
    FindAnswer(Trie);

    while (Ans.size() > 0 && Ans.back() == '-') Ans.pop_back();

    cout << Ans.size() << '\n';
    for (int i = 0; i < Ans.size(); ++ i )
        cout << Ans[i] << '\n';
}

int main () {
    Read();
    Solve();

    return 0;
}

Compilation message

printer.cpp: In function 'void Add(Node*, std::string, int)':
printer.cpp:26:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     if (lit == cuv.size()) {
      |         ~~~~^~~~~~~~~~~~~
printer.cpp: In function 'void Solve()':
printer.cpp:89:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |     for (int i = 0; i < Ans.size(); ++ i )
      |                     ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 1100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1744 KB Output is correct
2 Correct 3 ms 2252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5968 KB Output is correct
2 Correct 18 ms 12504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 14724 KB Output is correct
2 Correct 10 ms 3404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 36596 KB Output is correct
2 Correct 121 ms 83856 KB Output is correct
3 Correct 93 ms 43164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 28708 KB Output is correct
2 Correct 162 ms 99516 KB Output is correct
3 Correct 82 ms 49064 KB Output is correct
4 Correct 128 ms 93984 KB Output is correct