답안 #1065036

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1065036 2024-08-18T21:31:10 Z YassirSalama Type Printer (IOI08_printer) C++17
100 / 100
982 ms 99036 KB
#include<bits/stdc++.h>
using namespace std;
struct Node{
    Node* c[26];
    int maxdepth=0;
    bool leaf=false;
    Node(){
        for(int i=0;i<26;i++) c[i]=nullptr;
    }
};
Node* root=new Node();
void insert(string s){
    Node* node=root;
    int n=s.length();
    for(int i=0;i<n;i++){
        if(node->c[s[i]-'a']==nullptr) node->c[s[i]-'a']=new Node();
        node=node->c[s[i]-'a'];
        node->maxdepth=max(node->maxdepth,n);
    }
    node->leaf=true;
}
int k;
vector<char> op;
void dfs(Node* node){
    int best=-1;
    int mx=-1;
    for(int i=0;i<26;i++){
        if(node->c[i]!=nullptr){
            if(node->c[i]->maxdepth>mx){
                mx=node->c[i]->maxdepth;
                best=i;
            }
        }
    }
    if(node->leaf){
        k--;
        op.push_back('P');
    }
    for(int j=0;j<26;j++){
        if(node->c[j]){
            if(j==best) continue;
            op.push_back(char('a'+j));
            dfs(node->c[j]);
            if(k!=0)
                op.push_back('-');
        }
    }
    if(best!=-1){
        op.push_back(char(best+'a'));
        dfs(node->c[best]);
        if(k!=0) op.push_back('-');
    }
}
signed main(){
    // ios_base::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    k=n;
    string s;
    for(int i=0;i<n;i++){
        cin>>s;
        insert(s);
    }
    dfs(root);
    cout<<op.size()<<endl;
    for(auto x:op){
        cout<<x<<endl;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 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 6 ms 348 KB Output is correct
2 Correct 8 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 1948 KB Output is correct
2 Correct 20 ms 2136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 92 ms 5984 KB Output is correct
2 Correct 127 ms 12380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 158 ms 14804 KB Output is correct
2 Correct 48 ms 3164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 367 ms 36552 KB Output is correct
2 Correct 828 ms 83448 KB Output is correct
3 Correct 465 ms 42904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 321 ms 28484 KB Output is correct
2 Correct 982 ms 99036 KB Output is correct
3 Correct 495 ms 48576 KB Output is correct
4 Correct 887 ms 93640 KB Output is correct