#include<bits/stdc++.h>
using namespace std;
#ifdef ONPC
#include"debug.h"
#else
#define debug(...) 42
#endif
#define endl '\n'
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
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; }
const int mod = 1e9 + 7;
const int MAXN = 1e6 + 15;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
bool good_set = 1;
int trie[MAXN][26];
int Data[MAXN];
int dep[MAXN];
int node_cnt = 1;
void insert(string s){
int node = 1;
for (char c : s){
if (!trie[node][c - 'a']){
trie[node][c - 'a'] = ++node_cnt;
}
node = trie[node][c - 'a'];
}
Data[node]++;
return;
}
int cnt = 0;
int n;
void calc_dep(int node){
int mx = 0;
for (int i = 0; i < 26; i++){
if (trie[node][i]){
calc_dep(trie[node][i]);
ckmax(mx, dep[trie[node][i]]);
}
}
dep[node] = mx + 1;
}
void solve(int node){
if (Data[node]){
cout << 'P' << endl;
cnt++;
if (cnt == n) exit(0);
}
vector<int> children;
for (int i = 0; i < 26; i++){
if (trie[node][i]){
children.pb(i);
}
}
sort(all(children), [&](int l, int r){ return dep[trie[node][l]] < dep[trie[node][r]]; });
for (int child : children){
cout << char(child + 'a') << endl;
solve(trie[node][child]);
cout << '-' << endl;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++){
string s;
cin >> s;
insert(s);
}
calc_dep(1);
cout << n + 2 * (node_cnt - 1) - (dep[1] - 1) << endl;
solve(1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3164 KB |
Output is correct |
2 |
Correct |
3 ms |
3672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5212 KB |
Output is correct |
2 |
Correct |
15 ms |
10392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
11352 KB |
Output is correct |
2 |
Correct |
6 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
23832 KB |
Output is correct |
2 |
Correct |
100 ms |
46160 KB |
Output is correct |
3 |
Correct |
57 ms |
27220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
19788 KB |
Output is correct |
2 |
Correct |
118 ms |
53652 KB |
Output is correct |
3 |
Correct |
59 ms |
29704 KB |
Output is correct |
4 |
Correct |
101 ms |
51024 KB |
Output is correct |