// #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';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
212092 KB |
Output is correct |
2 |
Correct |
67 ms |
212212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
212164 KB |
Output is correct |
2 |
Correct |
68 ms |
212108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
212052 KB |
Output is correct |
2 |
Correct |
67 ms |
212048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
212048 KB |
Output is correct |
2 |
Correct |
77 ms |
212060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
212048 KB |
Output is correct |
2 |
Correct |
66 ms |
212048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
212208 KB |
Output is correct |
2 |
Correct |
79 ms |
212304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
212560 KB |
Output is correct |
2 |
Correct |
83 ms |
213144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
214852 KB |
Output is correct |
2 |
Correct |
72 ms |
212560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
214740 KB |
Output is correct |
2 |
Correct |
136 ms |
218320 KB |
Output is correct |
3 |
Correct |
95 ms |
215548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
214240 KB |
Output is correct |
2 |
Correct |
147 ms |
219288 KB |
Output is correct |
3 |
Correct |
99 ms |
216016 KB |
Output is correct |
4 |
Correct |
123 ms |
218828 KB |
Output is correct |