#include<bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
vector<char> ans;
const int maxn = 5e5 + 69;
int trie[maxn][26], kraj[maxn], sz[maxn];
int n, node;
void add(string s) {
int m = s.size();
int t = 0;
for (int i = 0;i < m;i++) {
int x = s[i] - 'a';
if (trie[t][x] == 0)
trie[t][x] = ++node;
t = trie[t][x];
}
kraj[t]++;
}
void dfs(int v) {
sz[v] = 1;
for (int i = 0;i < 26;i++) {
if (trie[v][i]) {
dfs(trie[v][i]);
sz[v] += sz[trie[v][i]];
}
}
}
void dfs1(int v) {
for (int i = 0;i < kraj[v];i++)
ans.push_back('P');
vector<pair<int, int>> x;
for (int i = 0;i < 26;i++) {
if (trie[v][i])
x.push_back({sz[trie[v][i]], i});
}
sort(x.begin(), x.end());
for (int i = 0;i < (int)x.size();i++) {
ans.push_back((char)(x[i].s + 'a'));
dfs1(trie[v][x[i].s]);
ans.push_back('-');
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n;
for (int i = 0;i < n;i++) {
string s;
cin >> s;
add(s);
}
dfs(0);
dfs1(0);
while (ans.back() == '-')
ans.pop_back();
cout << ans.size() << '\n';
for (auto x : ans)
cout << x << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3932 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4184 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
20284 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
40652 KB |
Output is correct |
2 |
Correct |
107 ms |
84728 KB |
Output is correct |
3 |
Incorrect |
57 ms |
46548 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
42 ms |
33236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |