#include "bits/stdc++.h"
using namespace std;
const int maxn = 25005;
string s[maxn];
int t[maxn * 20 * 26][26], cnt[maxn * 20 * 26];
int node = 0;
int par[maxn * 20 * 26];
void insert(string s){
int cur = 0;
for(auto c : s){
if(!t[cur][c - 'a']){
t[cur][c - 'a'] = ++node;
}
par[t[cur][c - 'a']] = cur;
cur = t[cur][c - 'a'];
}
}
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){
bool leaf = 1;
for(int i = 0; i < 26; ++i){
if(t[cur][i]){
leaf = 0;
break;
}
}
if(leaf){
v.push_back('P');
return;
}
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());
for(auto i : vv){
v.push_back((char)(i.second + 'a'));
dfs(t[cur][i.second]);
v.push_back('-');
}
}
int main(int argc, char const *argv[])
{
// freopen("in.txt", "r", stdin);
int n;
cin >> n;
memset(par, -1, sizeof par);
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 |
30 ms |
52088 KB |
Output is correct |
2 |
Correct |
30 ms |
51968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
52096 KB |
Output is correct |
2 |
Correct |
30 ms |
51968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
51960 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
52096 KB |
Output is correct |
2 |
Incorrect |
30 ms |
51968 KB |
didn't print every word |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
52088 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
56 ms |
52736 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
139 ms |
54904 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
302 ms |
59380 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
731 ms |
70516 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
595 ms |
66456 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |