#include <bits/stdc++.h>
using namespace std;
int const N=1e5+5;
struct node
{
char ch;
map<char,node*> mp;
set<pair<int,node*>> adj;
int dep=1;
int cnt=0;
};
node* root;
void add(){
string s;
cin>>s;
int i=0;
node* cur=root;
while(i<int(s.length())){
if(cur->mp[s[i]]==NULL){
cur->mp[s[i]]=new node();
cur->mp[s[i]]->ch=s[i];
}
cur=cur->mp[s[i]];
i++;
}
cur->cnt++;
}
int dep_set(node* rt){
for(auto i:rt->mp){
rt->dep=max(rt->dep,dep_set(i.second)+1);
rt->adj.insert({i.second->dep,i.second});
}
return rt->dep;
}
vector<char> ans;
void dfs1(node* rt){
for (int i = 0; i < rt->cnt; ++i)
ans.push_back('P');
for(auto i:rt->adj){
ans.push_back(i.second->ch);
dfs1(i.second);
ans.push_back('-');
}
}
int main(){
int t;
cin>>t;
root=new node();
for (int i = 0; i < t; ++i)
add();
dep_set(root);
dfs1(root);
int sz=ans.size();
while(sz>0&&ans[sz-1]=='-'){
ans.pop_back();
sz--;
}
printf("%d\n",sz);
for(char i:ans)
printf("%c\n",i);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
408 KB |
Output is correct |
2 |
Correct |
2 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2004 KB |
Output is correct |
2 |
Correct |
4 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
6720 KB |
Output is correct |
2 |
Correct |
24 ms |
14156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
16648 KB |
Output is correct |
2 |
Correct |
10 ms |
3556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
41392 KB |
Output is correct |
2 |
Correct |
144 ms |
94812 KB |
Output is correct |
3 |
Correct |
86 ms |
48596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
32356 KB |
Output is correct |
2 |
Correct |
171 ms |
112700 KB |
Output is correct |
3 |
Correct |
96 ms |
55640 KB |
Output is correct |
4 |
Correct |
166 ms |
106932 KB |
Output is correct |