#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;
};
vector<node> trie(1,{'@',0,vll(0)});
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());
tId = trie.size();
trie.push_back({s[sId],0,vll(0)});
}
}
trie[tId].endings++;
}
void dfs(ll id){
if(id) ans.push_back(trie[id].c);
f0r(i,trie[id].endings) ans.push_back('P');
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]);
}
dfs(0);
while(ans.back()=='-'){
ans.pop_back();
}
cout<<ans.size()<<"\n";
f0r(i,ans.size()){
cout<<ans[i]<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 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 |
952 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
2288 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
5716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
17128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
10980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |