#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;
int tot=0;
int maxx=0;
void add(){
string s;
cin>>s;
int i=0;
node* cur=root;
maxx=max(maxx,int(s.length()));
while(i<int(s.length())){
if(cur->mp[s[i]]==NULL){
cur->mp[s[i]]=new node();
tot++;
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)
printf("%c\n",'P');
for(auto i:rt->adj){
printf("%c\n",i.second->ch);
tot--;
dfs1(i.second);
if(tot!=0)
printf("%c\n",'-');
}
}
int main(){
int t;
cin>>t;
root=new node();
for (int i = 0; i < t; ++i)
add();
dep_set(root);
printf("%d\n",(tot*2)+t-maxx);
dfs1(root);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
3 ms |
2388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
6632 KB |
Output is correct |
2 |
Correct |
24 ms |
13888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
16332 KB |
Output is correct |
2 |
Correct |
10 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
40940 KB |
Output is correct |
2 |
Correct |
141 ms |
93920 KB |
Output is correct |
3 |
Correct |
88 ms |
48220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
31960 KB |
Output is correct |
2 |
Correct |
169 ms |
111708 KB |
Output is correct |
3 |
Correct |
99 ms |
54672 KB |
Output is correct |
4 |
Correct |
136 ms |
105452 KB |
Output is correct |