#include<bits/stdc++.h>
using namespace std ;
const int maxn = 28*25005 ;
int n, fim[maxn], trie[maxn][28], qtd_pal, mx[maxn], ct ;
string s ;
vector<char> ans ;
void add(string s){
int root = 0 ;
for(int i = 0 ; i < s.size() ; i++){
if(!trie[root][s[i]-'a']) trie[root][s[i]-'a'] = ++ct ;
root = trie[root][s[i]-'a'] ;
}
fim[root] = 1 ;
}
void dfs_alt(int v, int p){
mx[v] = 1 ;
if(fim[v]) return ;
for(char i = 'a' ; i <= 'z' ; i++){
if(!trie[v][i-'a'] || trie[v][i-'a'] == p) continue ;
dfs_alt(trie[v][i-'a'], v) ;
mx[v] = max(mx[v], mx[trie[v][i-'a']] + 1) ;
}
}
void dfs(int v, int p){
if(fim[v]){
ans.push_back('P') ;
qtd_pal++ ;
}
int mxx = -1 ; char ind = 0 ;
for(char i = 'a' ; i <= 'z' ; i++){
if(!trie[v][i-'a'] || trie[v][i-'a'] == p) continue ;
if(mxx < mx[trie[v][i-'a']]){
mxx = mx[trie[v][i-'a']] ; ind = i ;
}
}
for(char i = 'a' ; i <= 'z' ; i++){
if(!trie[v][i-'a'] || trie[v][i-'a'] == p || i == ind) continue ;
ans.push_back(i) ;
dfs(trie[v][i-'a'], v) ;
if(qtd_pal != n) ans.push_back('-') ;
}
if(mxx != -1 && trie[v][ind-'a']){
ans.push_back(ind) ;
dfs(trie[v][ind-'a'], v) ;
if(qtd_pal != n) ans.push_back('-') ;
}
}
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(mx, -1, sizeof mx) ;
dfs_alt(0, 0) ;
dfs(0, 0) ;
cout << ans.size() << "\n" ;
for(auto a : ans) cout << a << "\n" ;
}
Compilation message
printer.cpp: In function 'void add(std::string)':
printer.cpp:14:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for(int i = 0 ; i < s.size() ; i++){
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3020 KB |
Output is correct |
2 |
Correct |
1 ms |
3020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3020 KB |
Output is correct |
2 |
Correct |
2 ms |
3020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
3020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3020 KB |
Output is correct |
2 |
Correct |
2 ms |
3020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
3020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
3884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
6068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
10708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
53 ms |
22248 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
44 ms |
18024 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |