#include <bits/stdc++.h>
#ifndef LOCAL
#define debug(...) 0
#else
#include "../template/debug.cpp"
#endif
using namespace std;
using ll = int_fast64_t;
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define pq priority_queue
#define FOR(i,a,b) for(int i = (a); i < (b); ++i)
#define FORE(i,a,b) for(int i = (a); i <= (b); ++i)
#define ROF(i,a,b) for(int i = (a); i >= (b); --i)
#define trav(a,x) for(auto& a: x)
#define fuck(x) trav(i,x) cin >> i
#define sz(x) (int)x.size()
template<class T> using minpq = pq<T, vector<T>, greater<T>>;
template<class T> bool ckmin(T& a, const T& b){return b<a?a=b,1:0;}
template<class T> bool ckmax(T& a, const T& b){return a<b?a=b,1:0;}
template<int D, typename T>struct vt : public vector<vt<D - 1, T>> { template<typename... Args>
vt(int n = 0, Args... args) : vector<vt<D - 1, T>>(n, vt<D - 1, T>(args...)) {}};
template<typename T> struct vt<1, T> : public vector<T> {
vt(int n = 0, const T& val = T()) : vector<T>(n, val) {}};
vector<char> ans;
struct trie {
trie* children[26] = {};
int last = 69, mxd = -1;
bool isleaf = false;
};
void trie_add(trie* root, string s) {
trie* cur = root;
FOR(i,0,sz(s)){
int c = s[i] - 'a';
if (cur->children[c] == NULL)
cur->children[c] = new trie;
if(ckmax(cur -> mxd, sz(s) - i - 1)){
cur -> last = c;
}
cur = cur->children[c];
}
cur->isleaf = true;
}
void dfs(trie* root){
if(root -> isleaf) ans.pb('P');
FOR(i,0,26) if(root -> children[i] != NULL && i != root -> last) {
ans.pb(char(i + 'a'));
dfs(root -> children[i]);
}
if(root -> last != 69) {
ans.pb(char(root -> last + 'a'));
dfs(root -> children[root -> last]);
}
ans.pb('-');
}
void uwu(){
int n; cin >> n;
vector<string> str(n);
trav(i, str) cin >> i;
trie t;
trav(i, str) trie_add(&t, i);
dfs(&t);
while(ans.back() == '-') ans.pop_back();
cout << sz(ans) << "\n";
trav(i, ans) cout << i << "\n";
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
int t = 1;
// cin>>t;
while(t--){
uwu();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
444 KB |
Output is correct |
2 |
Correct |
2 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1988 KB |
Output is correct |
2 |
Correct |
3 ms |
2372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
6484 KB |
Output is correct |
2 |
Correct |
16 ms |
13684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
16048 KB |
Output is correct |
2 |
Correct |
7 ms |
4052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
39904 KB |
Output is correct |
2 |
Correct |
114 ms |
91196 KB |
Output is correct |
3 |
Correct |
56 ms |
47780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
31332 KB |
Output is correct |
2 |
Correct |
133 ms |
108452 KB |
Output is correct |
3 |
Correct |
79 ms |
54348 KB |
Output is correct |
4 |
Correct |
110 ms |
102464 KB |
Output is correct |