#include "bits/stdc++.h"
using namespace std;
const int maxn = 25005;
int n;
string del;
struct trie {
int total_nodes;
struct node {
node *child[26];
bool exist;
node() {
exist = 0;
for(int i = 0; i < 26; ++i) {
child[i] = nullptr;
}
}
} *root;
trie() {
total_nodes = 0;
root = new node();
}
void add(string &s) {
node *p = root;
for(int i = 0; i < (int)s.size(); ++i) {
int c = s[i] - 'a';
if(p->child[c] == nullptr) {
p->child[c] = new node();
++total_nodes;
}
p = p->child[c];
}
p->exist = 1;
}
void dfs(int depth, node *p, bool flag) {
if(p->exist) {
cout << 'P' << '\n';
}
for(int i = 0; i < 26; ++i) {
if(p->child[i] == nullptr || (flag and i == del[depth] - 'a')) continue;
cout << char(i + 'a') << '\n';
dfs(depth + 1, p->child[i], 0);
}
if(flag and depth < (int)del.size() and p->child[del[depth] - 'a'] != nullptr) {
cout << del[depth] << '\n';
dfs(depth + 1, p->child[del[depth] - 'a'], 1);
return;
}
if(!flag) cout << "-\n";
}
void dfs() {
dfs(0, root, 1);
}
} t;
void solve() {
cin >> n;
string s;
for(int i = 1; i <= n; ++i) {
cin >> s;
t.add(s);
if((int)s.size() > (int)del.size()) {
del = s;
}
}
cout << t.total_nodes * 2 + n - (int)del.size() << '\n';
t.dfs();
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
1280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1884 KB |
Output is correct |
2 |
Correct |
2 ms |
2140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5976 KB |
Output is correct |
2 |
Correct |
12 ms |
12376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14680 KB |
Output is correct |
2 |
Correct |
6 ms |
3416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
36176 KB |
Output is correct |
2 |
Correct |
90 ms |
83028 KB |
Output is correct |
3 |
Correct |
40 ms |
42596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
28252 KB |
Output is correct |
2 |
Correct |
97 ms |
98568 KB |
Output is correct |
3 |
Correct |
45 ms |
48464 KB |
Output is correct |
4 |
Correct |
75 ms |
93008 KB |
Output is correct |