This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |