답안 #1108146

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1108146 2024-11-03T03:17:05 Z vjudge1 Type Printer (IOI08_printer) C++17
100 / 100
118 ms 55996 KB
#include <iostream>
#include <vector>
#include <algorithm>


int main(){
    std::cin.tie(nullptr)->sync_with_stdio(false);
    int n;
    std::cin >> n;
    std::vector<std::vector<std::pair<char,int>>> trie(500001);
    std::vector<std::vector<int>> subtreeSize(500001, std::vector<int>(2));
    std::vector<bool> finalVertex(500001);
    int curVertices = 0;
    for(int i = 0; i < n; i++){
        std::string s; 
        std::cin >> s;
        int m = (int) s.length();
        int curVertex = 0;
        for(int j = 0; j < m; j++){
            bool found = false;
            for(std::pair<char,int> & p : trie[curVertex]){
                if(p.first == s[j]){
                    found = true;
                    curVertex = p.second;
                    break;
                }
            }
            if(found == false){
                curVertices += 1;
                trie[curVertex].push_back({s[j], curVertices});
                curVertex = curVertices;
            }
            if(j == (m - 1)){
                finalVertex[curVertex] = true;
            }
        }
    }
    auto dfs = [&trie, &subtreeSize](int u, int p, auto && self) -> void {
        subtreeSize[u][0] += 1;
        subtreeSize[u][1] = 1;
        for(std::pair<char,int> & v : trie[u]){
            if(v.second != p){
                self(v.second, u, self);
                subtreeSize[u][0] += subtreeSize[v.second][0];
                subtreeSize[u][1] = std::max(subtreeSize[u][1], 1 + subtreeSize[v.second][1]);
            }
        }
    };
    dfs(0, 0, dfs);
    for(int i = 0; i <= curVertices; i++){
        sort(trie[i].begin(), trie[i].end(), [&subtreeSize](const std::pair<char,int> & a, const std::pair<char,int> & b) -> bool {
            return 2*subtreeSize[a.second][0] + (2*subtreeSize[b.second][0] - subtreeSize[b.second][1]) 
            < 2*subtreeSize[b.second][0] + (2*subtreeSize[a.second][0] - subtreeSize[a.second][1]);
        });
    }
    int built = 0;
    std::vector<char> operations;
    auto build = [&trie, &curVertices, &finalVertex, &built, &operations](char c, int u, int p, auto && self) -> void{
        if(u != 0){
            operations.push_back(c);
            built += 1;
        }
        if(finalVertex[u]){
            operations.push_back('P');
        }
        for(std::pair<char,int> & v : trie[u]){
            if(v.second != p){
                self(v.first, v.second, u, self);
            }
        }
        if(built < curVertices){
            operations.push_back('-');
        }
    };
    build('0', 0, 0, build);
    std::cout << operations.size() << '\n';
    for(char & c : operations){
        std::cout << c << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 39504 KB Output is correct
2 Correct 38 ms 39516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 39504 KB Output is correct
2 Correct 28 ms 39496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 39504 KB Output is correct
2 Correct 29 ms 39496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 39504 KB Output is correct
2 Correct 27 ms 39428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 39504 KB Output is correct
2 Correct 27 ms 39568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 39760 KB Output is correct
2 Correct 27 ms 39760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 40528 KB Output is correct
2 Correct 36 ms 41556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 41932 KB Output is correct
2 Correct 30 ms 40272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 45168 KB Output is correct
2 Correct 90 ms 52924 KB Output is correct
3 Correct 72 ms 46276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 43888 KB Output is correct
2 Correct 118 ms 55996 KB Output is correct
3 Correct 66 ms 47552 KB Output is correct
4 Correct 88 ms 54980 KB Output is correct