#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define sz(v) (int) v.size()
#define all(v) v.begin(), v.end()
using namespace std;
constexpr int N = 2e5+5;
constexpr int M = 1e9+7;
constexpr int inf = 1e18;
struct Node {
bool exist;
int max_depth;
Node *nxt[26];
Node() {
exist = false;
max_depth = 0;
for (int i = 0; i < 26; ++i) nxt[i] = NULL;
}
};
struct Trie {
Node *root;
Trie() {
root = new Node();
}
void insert(string s) {
Node *cur = root;
for (char c : s) {
int idx = c - 'a';
if (cur->nxt[idx] == NULL) cur->nxt[idx] = new Node();
cur = cur->nxt[idx];
cur->max_depth = max(cur->max_depth, sz(s));
}
cur->exist = true;
}
} trie;
int trie_max_depth;
string res;
void dfs(Node *v) {
if (v->exist) {
res.push_back('P');
}
int abyss = -1;
for (int i = 0; i < 26; ++i) {
if (v->nxt[i] != NULL) {
if (v->nxt[i]->max_depth == trie_max_depth) {
abyss = i;
}
}
}
for (int i = 0; i < 26; ++i) {
if (i == abyss) continue;
if (v->nxt[i] != NULL) {
res.push_back(i + 'a');
dfs(v->nxt[i]);
res.push_back('-');
}
}
if (abyss != -1) {
res.push_back(abyss + 'a');
dfs(v->nxt[abyss]);
res.push_back('-');
}
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
while (n--) {
string s;
cin >> s;
trie_max_depth = max(trie_max_depth, sz(s));
trie.insert(s);
}
dfs(trie.root);
while (res.back() == '-') res.pop_back();
cout << res.size() << "\n";
for (char c : res) cout << c << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |