#include "bits/stdc++.h"
using namespace std;
const int maxn = 1000000;
string s[maxn];
int t[maxn][26], ed[maxn], cnt[maxn];
int node = 0;
int par[maxn];
void insert(string ss){
int cur = 0;
int sz = ss.size();
for(int i = 0; i < sz; ++i){
int idx = ss[i] - 'a';
if(t[cur][idx] == 0){
t[cur][idx] = ++node;
}
cur = t[cur][idx];
}
ed[cur] = 1;
}
vector<char> v;
void get(int cur){
cnt[cur] = 1;
for(int i = 0; i < 26; ++i){
if(t[cur][i]){
get(t[cur][i]);
cnt[cur] = max(cnt[cur], cnt[t[cur][i]] + 1);
}
}
}
void dfs(int cur){
if(ed[cur]){
ed[cur] = 0;
v.push_back('P');
}
vector<pair<int, int>> vv;
int sz = 0;
for(int i = 0; i < 26; ++i){
if(t[cur][i]){
vv.push_back(make_pair(cnt[t[cur][i]], i));
sz++;
}
}
sort(vv.begin(), vv.end());
for(int i = 0; i < sz; ++i){
v.push_back((char)(vv[i].second + 'a'));
dfs(t[cur][vv[i].second]);
v.push_back('-');
}
}
int main(int argc, char const *argv[])
{
// freopen("in.txt", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for(int i = 0; i < n; ++i){
cin >> s[i];
insert(s[i]);
}
get(0);
dfs(0);
int sz = v.size();
while(v[sz - 1] == '-')--sz;
cout << sz << endl;
for(int i = 0; i < sz; ++i){
cout << v[i] << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
31616 KB |
Output is correct |
2 |
Correct |
20 ms |
31616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
31616 KB |
Output is correct |
2 |
Correct |
21 ms |
31488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
31616 KB |
Output is correct |
2 |
Correct |
20 ms |
31616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
31744 KB |
Output is correct |
2 |
Correct |
21 ms |
31744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
31744 KB |
Output is correct |
2 |
Correct |
35 ms |
32128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
32504 KB |
Output is correct |
2 |
Correct |
61 ms |
32640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
131 ms |
34680 KB |
Output is correct |
2 |
Correct |
241 ms |
38116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
298 ms |
39160 KB |
Output is correct |
2 |
Correct |
104 ms |
33528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
727 ms |
50548 KB |
Output is correct |
2 |
Execution timed out |
1091 ms |
74984 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
579 ms |
46452 KB |
Output is correct |
2 |
Execution timed out |
1099 ms |
82956 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |