#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define int int64_t
using namespace std;
using namespace __gnu_pbds;
template <typename T>using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2e5 + 10, mod = 1e9+7;
// This isn't part of my plan
string ans;
struct Trie {
struct Node {
int child[26] = {};
int dpth = 0;
bool end = 0;
};
vector<Node> trie;
Trie() {
trie.emplace_back();
}
void add(string& s) {
int node = 0;
for(auto c : s) {
int nxt = c-'a';
if(!trie[node].child[nxt]) {
trie[node].child[nxt] = trie.size();
trie.emplace_back();
}
node = trie[node].child[nxt];
}
trie[node].end = 1;
}
int solve(int node = 0) {
trie[node].dpth = 0;
for(int i = 0; i < 26; i++) {
if(!trie[node].child[i]) continue;
trie[node].dpth = max(trie[node].dpth, solve(trie[node].child[i]));
}
trie[node].dpth++;
return trie[node].dpth;
}
void dfs(int node = 0) {
if(trie[node].end)
ans.push_back('P');
int mx = 0, best = -1;
for(int i = 0; i < 26; i++) {
if(!trie[node].child[i]) continue;
if(trie[trie[node].child[i]].dpth > mx)
mx = trie[trie[node].child[i]].dpth,best = i;
}
for(int i = 0; i < 26; i++) {
if(i == best or !trie[node].child[i]) continue;
ans.push_back(i + 'a'),dfs(trie[node].child[i]);
}
if(~best)
ans.push_back(best + 'a'),dfs(trie[node].child[best]);
ans.push_back('-');
}
};
void burn() {
int n;
cin >>n;
Trie tr;
while(n--) {
string s;
cin >>s;
tr.add(s);
}
tr.solve();
tr.dfs();
while(ans.back() != 'P') ans.pop_back();
cout << ans.size() <<'\n';
for(auto c : ans)
cout << c <<'\n';
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t{1};
// cin >> t;
while (t--)
burn();
return 0;
}
# | 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... |