// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define pf push_front
#define vi vector<ll>
#define vii vector<vi>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()
#define sz(a) (ll)a.size()
#define fi first
#define se second
using namespace std;
const ll mod = 1e9+7;
const ll md = 998244353;
const ll smod = 1e6;
const ll inf = 1e18;
const ll imx = 1e9;
const ll blocksz = 320;
const ll N = 1e6+5;
const ll lim = 1e6+5;
// Road to Expert: 136pts left
// Target: Top 1 CHD
// Ultimate Target: VOI 202k (5 <= k <= 6)
ll n;
struct node{
ll exist,child[26];
node(){
exist = 0;
for(int i = 0; i < 26; ++i) child[i] = 0;
}
};
node trie[N];
ll now = 0;
void add(string &s){
ll u = 0;
for(auto v:s){
ll k = v-'a';
if(!trie[u].child[k]){
trie[u].child[k] = ++now;
trie[now] = node();
}
u = trie[u].child[k];
}
++trie[u].exist;
}
ll h[N];
bool ok[N];
void dfs(ll u){
ll gt = 0, cur = -1;
h[u] = 1;
for(int i = 0; i < 26; ++i){
if(trie[u].child[i]){
dfs(trie[u].child[i]);
if(h[trie[u].child[i]] >= gt){
gt = h[trie[u].child[i]];
if(cur != -1) ok[cur] = 0;
cur = trie[u].child[i];
ok[cur] = 1;
}
}
}
h[u] = gt+1;
}
vector<char> res;
void get(ll u){
for(int i = 0; i < trie[u].exist; ++i) res.pb('P');
ll cur = -1;
for(int i = 0; i < 26; ++i){
if(trie[u].child[i]){
if(ok[trie[u].child[i]]){cur = i; continue;}
res.pb((char)(i+'a'));
get(trie[u].child[i]);
res.pb('-');
}
}
if(cur != -1){
res.pb((char)(cur+'a'));
get(trie[u].child[cur]);
res.pb('-');
}
}
void solve(){
cin >> n;
trie[0] = node();
for(int i = 1; i <= n; ++i){
string s;cin >> s;
add(s);
}
dfs(0);
get(0);
while(res.back() == '-') res.pop_back();
cout << res.size() << '\n';
for(auto v:res) cout << v << '\n';
}
signed main(){
IOS
int T = 1;
// cin >> T;
for(int t = 1; t <= T; ++t){
// cout << "Case " << t << ":\n";
solve();
cout << '\n';
}
cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
68 ms |
212048 KB |
Expected EOF |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
66 ms |
212180 KB |
Expected EOF |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
67 ms |
212048 KB |
Expected EOF |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
73 ms |
212048 KB |
Expected EOF |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
67 ms |
212048 KB |
Expected EOF |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
76 ms |
212164 KB |
Expected EOF |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
73 ms |
212584 KB |
Expected EOF |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
80 ms |
213152 KB |
Expected EOF |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
96 ms |
214736 KB |
Expected EOF |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
101 ms |
214236 KB |
Expected EOF |
2 |
Halted |
0 ms |
0 KB |
- |