#include <bits/stdc++.h>
#define st first
#define nd second
using lint = int64_t;
constexpr int mod = int(1e9) + 7;
constexpr int inf = 0x3f3f3f3f;
constexpr int ninf = 0xcfcfcfcf;
constexpr lint linf = 0x3f3f3f3f3f3f3f3f;
const long double pi = acosl(-1.0);
// Returns -1 if a < b, 0 if a = b and 1 if a > b.
int cmp_double(double a, double b = 0, double eps = 1e-9) {
return a + eps > b ? b + eps > a ? 0 : 1 : -1;
}
using namespace std;
map<int, int>trie[500100];
int depth[500100];
bool flag[500100];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin>>n;
vector<string>a(n);
int sz = 1;
for(int i=0;i<n;i++){
cin>>a[i];
int cur = 0;
for(char c : a[i]){
if(!trie[cur].count(c)) trie[cur][c] = sz++;
cur = trie[cur][c];
}
flag[cur] = 1;
}
auto get_depth = [&](auto& self, int cur, int h) -> void{
depth[cur] = h;
for(auto [c, v] : trie[cur]){
self(self, v, h+1);
depth[cur] = max(depth[cur], depth[v]);
}
};
get_depth(get_depth, 0, 0);
string ans;
auto dfs = [&](auto& self, int cur) -> void{
vector<pair<int, int>>v;
if(flag[cur]) ans.push_back('P');
for(auto [c, u] : trie[cur]){
v.push_back({depth[u], c});
}
sort(v.begin(), v.end());
for(auto [d, c] : v){
ans.push_back(c);
self(self, trie[cur][c]);
}
ans.push_back('-');
};
dfs(dfs, 0);
while(ans.back() == '-') ans.pop_back();
cout<<ans.size()<<"\n";
for(char c : ans) cout<<c<<"\n";
return 0;
}
/*
[ ]Leu o problema certo???
[ ]Ver se precisa de long long
[ ]Viu o limite dos fors (é n? é m?)
[ ]Tamanho do vetor, será que é 2e5 em vez de 1e5??
[ ]Testar sample
[ ]Testar casos de borda
[ ]1LL no 1LL << i
[ ]Testar mod (é 1e9+7, mesmo?, será que o mod não ficou negativo?)
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
23704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
11 ms |
23756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23756 KB |
Output is correct |
2 |
Correct |
13 ms |
23932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
24252 KB |
Output is correct |
2 |
Correct |
15 ms |
24396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
25352 KB |
Output is correct |
2 |
Correct |
32 ms |
27324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
27856 KB |
Output is correct |
2 |
Correct |
21 ms |
25224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
33916 KB |
Output is correct |
2 |
Correct |
122 ms |
47276 KB |
Output is correct |
3 |
Correct |
75 ms |
36964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
31976 KB |
Output is correct |
2 |
Correct |
145 ms |
51800 KB |
Output is correct |
3 |
Correct |
88 ms |
38696 KB |
Output is correct |
4 |
Correct |
105 ms |
50308 KB |
Output is correct |