답안 #1091974

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1091974 2024-09-22T18:02:31 Z Salty Type Printer (IOI08_printer) C++14
100 / 100
128 ms 53968 KB
#include<bits/stdc++.h>
#define ll long long 
#define st first
#define pii pair<pair<int,int>,int>
#define nd second
#define pb push_back
using namespace std;
const int N = 5e5+10;
bool CASE = false;
int child[N];
int mxword[N];
int trie[N][26]; // trie[i][c] mean node number that contain character c and is a child of node i
int nxt;
bool stop[N]; 
void add(string s){
    int cur = 0;
    for(auto u:s){
        if(trie[cur][u-'a'] == 0) trie[cur][u-'a'] = ++nxt; // build new node
        cur = trie[cur][u-'a']; // go to next node 
    }
    stop[cur] = true; // show that current node is end node
}
vector<char> ans;
void dfs(int node){
    if(stop[node]) ans.push_back('P');
    vector<pii> start;
    for(int i = 0; i < 26; i++) if(trie[node][i]) start.push_back({{child[trie[node][i]],mxword[trie[node][i]]},i});
    sort(start.begin(),start.end(),[&](pii a, pii b){
         if(a.st.nd == b.st.nd) return a.st.st < b.st.st;
         return a.st.nd < b.st.nd;
    });
    for(auto k:start){
        if(trie[node][k.second] == 0) continue;
        ans.push_back(k.second+'a');
        dfs(trie[node][k.second]);
    }
    ans.push_back('-');
    return;
}
void dfs2(int node){
    child[node] = 1;
    for(int k = 0; k < 26; k++){
        if(trie[node][k] == 0) continue;
        dfs2(trie[node][k]);
        child[node] += child[trie[node][k]];
    }
    return;
}
void dfs3(int node,int lv){
    mxword[node] = lv;
    for(int k = 0; k < 26; k++){
        if(trie[node][k] == 0) continue;
        dfs3(trie[node][k],lv+1);
        mxword[node] = max(mxword[node],mxword[trie[node][k]]);
    }
    return;
}
void solve (){
    ll n;
    cin >> n;
    vector<string> v(n);
    for(int i = 0; i < n; i++){
        cin >> v[i];
        add(v[i]);
    }
    vector<pii> start;
    for(int i = 0; i < 26; i++) if(trie[0][i]) {dfs2(trie[0][i]); dfs3(trie[0][i], 1); start.push_back({{child[trie[0][i]],mxword[trie[0][i]]},i});}
    sort(start.begin(),start.end(),[&](pii a, pii b){
         if(a.st.nd == b.st.nd) return a.st.st < b.st.st;
         return a.st.nd < b.st.nd;
    });
    //cout << mxword[trie[0][0]];
    for(auto i:start){
        if(trie[0][i.second]) {
            ans.push_back(i.second+'a');
            dfs(trie[0][i.second]);
        }
    }
    while(ans.back() == '-') ans.pop_back();
    cout << ans.size() << "\n";
    for(auto u:ans){
        cout << u << "\n";
    }

}
int main(){   
    ios_base::sync_with_stdio(false);cin.tie(0);
    int t = 1;
    if(CASE) cin >> t;
    while(t--)
    {   
        solve();
    }
 
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 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 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 1 ms 348 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1264 KB Output is correct
2 Correct 3 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 3420 KB Output is correct
2 Correct 15 ms 7008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 8412 KB Output is correct
2 Correct 6 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 20168 KB Output is correct
2 Correct 112 ms 45508 KB Output is correct
3 Correct 54 ms 24528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 16088 KB Output is correct
2 Correct 128 ms 53968 KB Output is correct
3 Correct 68 ms 27860 KB Output is correct
4 Correct 115 ms 51128 KB Output is correct