답안 #498638

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
498638 2021-12-26T02:43:34 Z _AnKahng Type Printer (IOI08_printer) C++14
100 / 100
143 ms 99572 KB
#include<bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;

int n,mx;
string s,larger;
vector<char> kq;

struct node{
    node* child[26];
    bool fl,e;
    node(){
        fo(i,0,25) child[i] = nullptr;
        e = fl = false;
    }
};

node* root;

struct Trie{
    void Insert(string s, bool o){
        node* p = root;
        for(char a : s){
            if(!p -> child[a -'a']) p -> child[a - 'a'] = new node;
            p = p -> child[a - 'a'];
            p -> fl = o;
        }
        p -> fl = o;
        p -> e = true;
    }
    void Dfs(node *p){
        if(p -> e) kq.push_back('P');
        int tmp = -1;
        fo(i,0,25)
            if(p -> child[i]) {
                node* v = p -> child[i];
                if(v -> fl){
                    tmp = i;
                    continue;
                }
                kq.push_back('a' + i);
                Dfs(v);
                kq.push_back('-');
            }
        if(tmp != -1){
            kq.push_back('a' + tmp);
            Dfs(p -> child[tmp]);
        }
    }
} trie;

int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    root = new node;
    cin >> n;
    fo(i,1,n) {
        cin >> s;
        trie.Insert(s,0);
        if(s.size() > mx){
            mx = s.size();
            larger = s;
        }
    }
    trie.Insert(larger,1);
    trie.Dfs(root);
    cout << kq.size()<<"\n";
    for(auto k : kq) cout << k<<"\n";






}

Compilation message

printer.cpp: In function 'int main()':
printer.cpp:60:21: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   60 |         if(s.size() > mx){
      |            ~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 288 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 0 ms 316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 1100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1740 KB Output is correct
2 Correct 3 ms 2252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5960 KB Output is correct
2 Correct 19 ms 12600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 14796 KB Output is correct
2 Correct 9 ms 3300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 36676 KB Output is correct
2 Correct 115 ms 83636 KB Output is correct
3 Correct 63 ms 43200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 28608 KB Output is correct
2 Correct 143 ms 99572 KB Output is correct
3 Correct 71 ms 49000 KB Output is correct
4 Correct 116 ms 93984 KB Output is correct