답안 #1095248

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1095248 2024-10-01T16:14:22 Z BF001 Type Printer (IOI08_printer) C++17
100 / 100
143 ms 66196 KB
#include<bits/stdc++.h>
using namespace std;
 
vector<char> res;

int n;
string s;
 
struct node{
    int cnt, dep;
    vector<int> nx;
    node(){
        cnt = dep = 0;
        nx.resize(26, -1);
    }
};

vector<node> vec;

void add(string s){
    int root = 0;
    for (auto x : s){
        int val = x - 'a';
        if (vec[root].nx[val] == -1){
            vec[root].nx[val] = (int) vec.size();
            vec.push_back(node());
        }
        root = vec[root].nx[val];
    }
    vec[root].cnt++;
}

void dfs(int root){
    for (int i = 1; i <= vec[root].cnt; i++){
        res.push_back('P');
    }
    int ma = 0, pos = -1;
    for (int i = 0; i < 26; i++){
        if (vec[root].nx[i] != -1){
            int nw = vec[root].nx[i];
            if (vec[nw].dep > ma){
                ma = vec[nw].dep;
                pos = i;
            }
        }
    }
    for (int i = 0; i < 26; i++){
        if (vec[root].nx[i] != -1 && i != pos){
            res.push_back(char(i + 'a'));
            dfs(vec[root].nx[i]);
        }
    }
    if (pos != -1) {res.push_back(char(pos + 'a'));
    dfs(vec[root].nx[pos]);}
    res.push_back('-');
}

void dfs2(int root){
    vec[root].dep = 1;
    for (int i = 0; i < 26; i++){
        if (vec[root].nx[i] != -1){
            dfs2(vec[root].nx[i]);
            vec[root].dep = max(vec[root].dep, vec[vec[root].nx[i]].dep + 1);
        }
    } 
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
 
    vec.push_back(node());

    cin >> n;
    for (int i = 1; i <= n; i++){
        cin >> s;
        add(s);
    }
 
    dfs2(0);
    dfs(0);

    while (res.back() == '-') res.pop_back();

    cout << (int) res.size() << "\n";
    for (auto x : res){
        cout << x << "\n";
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 2 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1240 KB Output is correct
2 Correct 3 ms 1752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4044 KB Output is correct
2 Correct 18 ms 8352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 9932 KB Output is correct
2 Correct 7 ms 2516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 24232 KB Output is correct
2 Correct 120 ms 55864 KB Output is correct
3 Correct 70 ms 28840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 19128 KB Output is correct
2 Correct 143 ms 66196 KB Output is correct
3 Correct 70 ms 32940 KB Output is correct
4 Correct 124 ms 62616 KB Output is correct