답안 #696474

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
696474 2023-02-06T15:20:38 Z _martynas Type Printer (IOI08_printer) C++11
100 / 100
130 ms 101532 KB
#include <bits/stdc++.h>

using namespace std;

int n;
vector<char> coms;

struct Trie
{
    Trie* A[26] = {};
    bool end = false;

    void add(string& s, int i)
    {
        if(i == s.size()) {
            end = true;
            return;
        }
        if(!A[s[i]-'a']) {
            A[s[i]-'a'] = new Trie();
        }
        A[s[i]-'a']->add(s, i+1);
    }

    void solve(string s, int i, bool main_branch)
    {
        if(main_branch && i < s.size()) {
            A[s[i]-'a']->solve(s, i+1, true);
            coms.push_back(s[i]);
        }
        for(int j = 0; j < 26; j++) {
            if(A[j] && (!main_branch || j != s[i]-'a')) {
                coms.push_back('-');
                A[j]->solve(s, i+1, false);
                coms.push_back(char(j+'a'));
            }
        }
        if(end) {
            coms.push_back('P');
        }
    }
} trie;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    vector<string> A(n);
    for(int i = 0; i < n; i++) cin >> A[i];
    sort(A.begin(), A.end(),
    [&](string& a, string& b)
    {
        return a.size() > b.size();
    });

    for(int i = 0; i < n; i++) {
        trie.add(A[i], 0);
    }
    cerr << "Finished adding\n";

    trie.solve(A[0], 0, true);
    reverse(coms.begin(), coms.end());

    cout << coms.size() << "\n";
    for(char c : coms) {
        cout << c << "\n";
    }

    return 0;
}

Compilation message

printer.cpp: In member function 'void Trie::add(std::string&, int)':
printer.cpp:15:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |         if(i == s.size()) {
      |            ~~^~~~~~~~~~~
printer.cpp: In member function 'void Trie::solve(std::string, int, bool)':
printer.cpp:27:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |         if(main_branch && i < s.size()) {
      |                           ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 320 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 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 2 ms 1876 KB Output is correct
2 Correct 4 ms 2252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6088 KB Output is correct
2 Correct 18 ms 12872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 15104 KB Output is correct
2 Correct 8 ms 3924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 37444 KB Output is correct
2 Correct 105 ms 85428 KB Output is correct
3 Correct 57 ms 44872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 29424 KB Output is correct
2 Correct 130 ms 101532 KB Output is correct
3 Correct 67 ms 50940 KB Output is correct
4 Correct 116 ms 95960 KB Output is correct