#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;
for(int i = 0; i < 26; ++i){
if(t[cur][i]){
vv.push_back(make_pair(cnt[t[cur][i]], i));
}
}
sort(vv.begin(), vv.end());
int sz = vv.size();
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);
while(v.back() == '-')v.pop_back();
cout << v.size() << endl;
for(auto i : v){
cout << i << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
31616 KB |
Output is correct |
2 |
Correct |
21 ms |
31616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
31616 KB |
Output is correct |
2 |
Correct |
20 ms |
31744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
31744 KB |
Output is correct |
2 |
Correct |
20 ms |
31744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
31744 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 |
36 ms |
32128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
32632 KB |
Output is correct |
2 |
Correct |
65 ms |
32768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
34568 KB |
Output is correct |
2 |
Correct |
256 ms |
38136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
296 ms |
39292 KB |
Output is correct |
2 |
Correct |
93 ms |
33400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
681 ms |
50644 KB |
Output is correct |
2 |
Execution timed out |
1100 ms |
74988 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
569 ms |
46580 KB |
Output is correct |
2 |
Execution timed out |
1099 ms |
83056 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |