#include "bits/stdc++.h"
using namespace std;
#define int long long
const int sz = 5e5 + 5;
const int inf = 1e18;
int trie[sz][26];
int depth[sz];
int leaf[sz];
int nxt = 0, cnt = 0;
char ch[sz];
vector<int> adj[sz];
vector<char> res;
int n;
struct Trie
{
void add(string s)
{
int root = 0;
for(char c : s)
{
int child = c - 'a';
if (!trie[root][child])
{
trie[root][child] = ++nxt;
adj[root].push_back(trie[root][child]);
ch[trie[root][child]] = c;
}
root = trie[root][child];
depth[root] = max(depth[root], (int)s.size() - (&c - &s.front()));
}
leaf[root] = true;
}
void dfs(int node)
{
if (node) res.push_back(ch[node]);
if (leaf[node]) res.push_back('P'), cnt++;
for(int to : adj[node]) dfs(to);
if (cnt != n) res.push_back('-');
}
};
void solve()
{
cin >> n;
Trie tree;
for(int i = 1; i <= n; i++)
{
string s;
cin >> s;
tree.add(s);
}
for(int i = 0; i <= nxt; i++) sort(adj[i].begin(), adj[i].end(), [&](int a, int b)
{
return depth[a] < depth[b];
});
tree.dfs(0);
cout << res.size() << endl;
for(char c : res) cout << c << endl;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
12124 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
12888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
12120 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Incorrect |
10 ms |
13716 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
14424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
57 ms |
18524 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
140 ms |
28364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
352 ms |
53320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
284 ms |
45504 KB |
Output is correct |
2 |
Correct |
920 ms |
125164 KB |
Output is correct |
3 |
Incorrect |
465 ms |
68180 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |