#include <bits/stdc++.h>
using namespace std;
int trie[500001][26],timer = 0;
int n;
vector<int> adj[500001];
bool mark[500001], special[500001];
char label[500001];
vector<char> res;
void addString(string s) {
int cur = 0;
for (char c: s) {
int val = c - 'a';
if (!trie[cur][val]) {
trie[cur][val] = ++timer;
adj[cur].emplace_back(timer);
label[timer] = c;
}
cur = trie[cur][val];
}
special[cur] = 1;
}
void markLongest(string s) {
int cur = 0;
for (char c: s) {
int val = c - 'a';
mark[trie[cur][val]] = 1;
cur = trie[cur][val];
}
mark[cur] = 1;
}
void DFS(int u) {
int cnt = 0;
if (special[u]) {
res.emplace_back('P');
n--;
}
for (int v: adj[u]) {
if (mark[v]) continue;
cnt++;
res.emplace_back(label[v]);
DFS(v);
}
for (int v: adj[u]) {
if (!mark[v]) continue;
cnt++;
res.emplace_back(label[v]);
DFS(v);
}
if (n) res.emplace_back('-');
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
string s, longest;
for (int i = 1; i <= n; i++) {
cin >> s;
addString(s);
if (longest.size() < s.size()) longest = s;
}
markLongest(longest);
DFS(0);
cout << res.size() << '\n';
for (char c: res) cout << c << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
11980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12096 KB |
Output is correct |
2 |
Correct |
6 ms |
12040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11980 KB |
Output is correct |
2 |
Correct |
8 ms |
11980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11980 KB |
Output is correct |
2 |
Correct |
7 ms |
12000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12072 KB |
Output is correct |
2 |
Correct |
8 ms |
12492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
13004 KB |
Output is correct |
2 |
Correct |
9 ms |
13260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
15552 KB |
Output is correct |
2 |
Correct |
25 ms |
19664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
20936 KB |
Output is correct |
2 |
Correct |
11 ms |
14028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
34268 KB |
Output is correct |
2 |
Correct |
94 ms |
63928 KB |
Output is correct |
3 |
Correct |
67 ms |
38592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
29116 KB |
Output is correct |
2 |
Correct |
146 ms |
73796 KB |
Output is correct |
3 |
Correct |
64 ms |
42140 KB |
Output is correct |
4 |
Correct |
90 ms |
70328 KB |
Output is correct |