#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;
// insert t
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; }
}
for(int i = 0; i < 26; i++) {
node *v = u->children[i];
if(v == x || v == nullptr) 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3436 KB |
Output is correct |
2 |
Correct |
2 ms |
3436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3436 KB |
Output is correct |
2 |
Correct |
2 ms |
3436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3436 KB |
Output is correct |
2 |
Correct |
2 ms |
3456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3436 KB |
Output is correct |
2 |
Correct |
2 ms |
3436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3564 KB |
Output is correct |
2 |
Correct |
5 ms |
4332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4972 KB |
Output is correct |
2 |
Correct |
6 ms |
5484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
9196 KB |
Output is correct |
2 |
Correct |
28 ms |
15852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
18028 KB |
Output is correct |
2 |
Correct |
15 ms |
6764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
39908 KB |
Output is correct |
2 |
Correct |
167 ms |
87908 KB |
Output is correct |
3 |
Correct |
96 ms |
47468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
31720 KB |
Output is correct |
2 |
Correct |
198 ms |
104160 KB |
Output is correct |
3 |
Correct |
105 ms |
53348 KB |
Output is correct |
4 |
Correct |
184 ms |
98404 KB |
Output is correct |