제출 #396248

#제출 시각아이디문제언어결과실행 시간메모리
396248SundavarType Printer (IOI08_printer)C++14
100 / 100
342 ms48616 KiB
#include <bits/stdc++.h>

using namespace std;

struct node{
    map<int,int> to;
    bool end = false;
    int depth = 0;
};
vector<node> graph;

void add(string& s){
    int curr = 0;
    for(char c : s){
        if(graph[curr].to.count(c-'a') == 0){
            graph[curr].to[c-'a'] = graph.size();
            graph.push_back(node());
        }
        curr = graph[curr].to[c-'a'];
    }
    graph[curr].end = true;
}

string sol;
int getDepth(int curr){
    int ans = -1;
    for(int i = 0; i < 26; i++)
        if(graph[curr].to.count(i) != 0)
            ans = max(ans, getDepth(graph[curr].to[i]));
    graph[curr].depth = ++ans;
    return ans;
}
void walk(int curr, string& s, string& ending){
    if(graph[curr].end)
        s += 'P';
    vector<pair<int,int> > to;
    for(int i = 0; i < 26; i++)
        if(graph[curr].to.count(i) != 0)
            to.push_back({graph[graph[curr].to[i]].depth, i});
    sort(to.begin(), to.end());
    for(pair<int,int>& t : to){
        s += ending; ending = "";
        s += (char)('a'+t.second);
        walk(graph[curr].to[t.second], s, ending);
    }
    ending += "-";
}

int main(){
    int n;
    cin>>n;
    graph.push_back(node());
    while(n--){
        string s;
        cin>>s;
        add(s);
    }
    string sol = "", ending = "";
    getDepth(0);
    walk(0, sol, ending);
    cout<<sol.size()<<"\n";
    for(char& a : sol)
        cout<<a<<"\n";

}
#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...