#include<bits/stdc++.h>
using namespace std ;
//monta trie
// percorre ela com dfs
// chegou ao fim printa
// qd sobe coloca -
const int maxn = 6e5 + 5 ;
int n, trie[maxn][28], ctt, qtd[maxn], fim[maxn], ct ;
string s ;
vector<char> resp ;
void add(string s){
int root = 0 ;
for(int i = 0 ; i < (int) s.size() ; i++){
if(trie[root][s[i]-'a'] == 0) trie[root][s[i]-'a'] = ++ct ;
root = trie[root][s[i]-'a'] ;
}
fim[root] = 1 ;
}
bool cmp(pair<char,int> a, pair<char,int> b){return qtd[trie[a.second][a.first-'a']] < qtd[trie[b.second][b.first-'a']] ; }
void dfs(int v, int p){
if(fim[v]){
resp.push_back('P') ; ctt++ ;
}
int mx = -1 ; char ind = 'a' ;
for(char i = 'a' ; i <= 'z' ; i++){
if(trie[v][i-'a'] == 0 || trie[v][i-'a'] == p) continue ;
if(mx < qtd[trie[v][i-'a']]){
mx = qtd[trie[v][i-'a']] ; ind = i ;
}
}
// cout << v << " " << mx << " " << ind << "\n" ;
for(char i = 'a' ; i <= 'z' ; i++){
if(trie[v][i-'a'] == 0 || trie[v][i-'a'] == p || ind == i) continue ;
resp.push_back(i) ;
dfs(trie[v][i-'a'], v) ;
}
if(mx != -1 && trie[v][ind-'a']){
resp.push_back(ind) ; dfs(trie[v][ind-'a'], v) ;
}
if(ctt != n) resp.push_back('-') ;
}
void dfs_h(int v, int p){
qtd[v] = 1 ;
if(fim[v]) return ;
for(char i = 'a' ; i <= 'z' ; i++){
if(trie[v][i-'a'] == 0 || trie[v][i-'a'] == p) continue ;
dfs_h(trie[v][i-'a'], v) ;
qtd[v] = max(qtd[v], qtd[trie[v][i-'a']] + 1) ;
}
}
int main(){
ios_base::sync_with_stdio(false) ; cin.tie(NULL) ;
cin >> n ;
for(int i = 1 ; i <= n ; i++){
cin >> s ;
add(s) ;
}
memset(qtd, -1, sizeof qtd) ;
dfs_h(0, 0) ; // achar a maior alt de cada um
dfs(0, 0) ;
cout << resp.size() << "\n" ;
for(auto a : resp) cout << a << "\n" ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2628 KB |
Output is correct |
2 |
Correct |
1 ms |
2624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
1 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
too many deletions |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
3404 KB |
too many deletions |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
5572 KB |
too many deletions |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
10308 KB |
too many deletions |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
47 ms |
21992 KB |
too many deletions |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
44 ms |
17720 KB |
too many deletions |
2 |
Halted |
0 ms |
0 KB |
- |