제출 #1318132

#제출 시각아이디문제언어결과실행 시간메모리
1318132liminhaType Printer (IOI08_printer)C++20
100 / 100
673 ms134200 KiB
#include <bits/stdc++.h>
using namespace std;

#define __ ios::sync_with_stdio(0); cin.tie(NULL);
#define pb push_back
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) x.begin(),x.end()

const int mxn = 5010;
const int mxw = 500000+10;
const int INF = 1e18;
const int mod = 1e9+7;

vector<vector<int>> trie(mxw,vector<int>(26));
int cnt = 0;
vector<int> stop(mxw);
vector<int> longest(mxw);

void insert(string s){
    int node = 0;
    for(char c : s){
        if(trie[node][c-'a'] == 0) {
            trie[node][c-'a'] = ++cnt;
        }
        node = trie[node][c-'a'];
    }
    stop[node] = 1;
}

vector<char> ans;

int letter = 0;

int comp = 0;
int n;

void dfs(int s){
    if(stop[s]) {
        ans.pb('P');
        comp++;
    }

    int flag = -1;

    for(int i=0;i<26;i++){
        if(trie[s][i]) {
            if(longest[trie[s][i]]){
                flag = i;
                continue;
            }
            ans.pb(char(i+'a'));
            dfs(trie[s][i]);
            ans.pb('-');
        }
    }

    if(flag != -1){
        ans.pb((char(flag+'a')));
        dfs(trie[s][flag]);
        // ans.pb('-');
    }

}



void solve(){
    cin >> n;
    vector<string> words(n);
    rep(i,n){
        cin >> words[i];
    }

    sort(all(words),[](const auto p1, const auto p2){
        if(p1.size() == p2.size()) return p1 < p2;
        return p1.size() < p2.size();
    });


    rep(i,n) insert(words[i]);
    string t = words.back();

    int node = 0;
    for(auto u : t){
        int id = u-'a';
        node = trie[node][id];
        longest[node] = 1;
    }


    dfs(0);
    cout << ans.size() << endl;
    for(auto u : ans) cout << u << endl;
}




signed main(){ __ 
    int T = 1;
    // cin >> T;
    while(T--){
        solve();
    }
    return 0;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...