#include <bits/stdc++.h>
using namespace std;
int cost[100001], trie[100001][26],timer = 0;
int n;
vector<int> adj[100001];
bool mark[100001];
char label[100001];
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];
}
}
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;
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 (cnt == 0) {
res.emplace_back('P');
n--;
}
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 |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2636 KB |
too many deletions |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2636 KB |
too many deletions |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
too many deletions |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
3532 KB |
too many deletions |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
6092 KB |
too many deletions |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
11464 KB |
too many deletions |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
26 ms |
31812 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
27 ms |
31316 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |