#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pi pair<int, int>
#define fi first
#define se second
const int mxN = 1e5+5;
const int mod = 1e9+7;
const ll inf = 1e18;
struct node {
bool word;
int depth;
node *children[26];
node() {
word = 0, depth = 0;
for(int i = 0; i < 26; i++) children[i] = nullptr;
}
}; node *root = new node();
int n;
string s[mxN];
vector<char> ans;
void add(string t) {
int len = t.length();
node *cur = root;
for(int i = 0; i < len; i++) {
cur->depth = max(cur->depth, len);
if(cur->children[t[i]-'a'] == nullptr) cur->children[t[i]-'a'] = new node();
cur = cur->children[t[i]-'a'];
}
cur->depth = max(cur->depth, len);
cur->word = 1;
}
// dfs in a trie starting from the root
void dfs(node *u, int back) {
if(u->word) ans.push_back('P'); // red vertice - end of the word
int p = 0;
node *x = nullptr;
for(int i = 0; i < 26; i++) {
node *v = u->children[i];
if(v == nullptr) continue;
if(x == nullptr || v->depth > x->depth) { x = v; p = i; }
if(v == x) continue;
ans.push_back(i+'a');
dfs(v, 0);
}
if(x != nullptr) { ans.push_back(p+'a'); dfs(x, back); }
if(!back) ans.push_back('-');
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for(int i = 0; i < n; i++) {
cin >> s[i];
add(s[i]);
}
dfs(root, 1);
cout << ans.size() << "\n";
for(char i : ans) cout << i << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3436 KB |
Output is correct |
2 |
Correct |
3 ms |
3436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
3436 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
3436 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
3436 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
3564 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
4972 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
9196 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
18028 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
62 ms |
39784 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
51 ms |
31724 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |