#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
#define f0r(i,n) for(ll i = 0; i<(ll)(n); i++)
#define fir(it,d) for(auto it = d.begin(); it!=d.end(); it++)
struct node{
char c;
ll endings;
vll crs;
ll depth;
ll pr;
bool hasLast;
};
vector<node> trie(1,{'@',0,vll(0),0,0,false});
vector<char> ans;
void addString(string s){
ll tId = 0;
f0r(sId,s.size()){
bool found = false;
f0r(i,trie[tId].crs.size()){
if(trie[trie[tId].crs[i]].c==s[sId]){
tId = trie[tId].crs[i];
found = true;
break;
}
}
if(!found){
trie[tId].crs.push_back(trie.size());
trie.push_back({s[sId],0,vll(0),trie[tId].depth+1,tId,false});
tId = trie.size()-1;
}
}
trie[tId].endings++;
}
bool thing(ll a, ll b){
return(trie[a].hasLast+0<trie[b].hasLast+0);
}
void dfs(ll id){
if(id) ans.push_back(trie[id].c);
f0r(i,trie[id].endings) ans.push_back('P');
sort(trie[id].crs.begin(),trie[id].crs.begin(),thing);
f0r(i,trie[id].crs.size()){
ll cr = trie[id].crs[i];
dfs(cr);
}
if(id) ans.push_back('-');
}
bool compSize(string a, string b){
return(a.size()<b.size());
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
ll n;
cin>>n;
vector<string> inps(n);
f0r(i,n){
cin>>inps[i];
}
sort(inps.begin(),inps.end(),compSize);
f0r(i,n){
addString(inps[i]);
}
ll deepest = 0;
f0r(i,trie.size()){
if(trie[i].depth>trie[deepest].depth) deepest = i;
}
for(ll i = deepest; i!=0; i=trie[i].pr){
trie[i].hasLast = true;
}
dfs(0);
while(ans.size()&&ans.back()=='-'){
ans.pop_back();
}
cout<<ans.size()<<"\n";
f0r(i,ans.size()){
cout<<ans[i]<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
1044 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
3080 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
7152 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
42 ms |
22980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
13680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |