#include <bits/stdc++.h>
using namespace std;
const int mxc = 26;
struct trie {
trie *chi[mxc];
int len;
int cnt;
int las;
trie() {
cnt = 0;
len = 0;
las = -1;
for(int i = 0; i < mxc; i++)
chi[i] = NULL;
}
};
void insert(trie *root, string s) {
for(char c : s) {
if(root->chi[c - 'a'] == NULL)
root->chi[c - 'a'] = new trie();
root = root->chi[c - 'a'];
}
root->len = s.size();
root->cnt++;
}
trie *root = new trie();
vector<char> ans;
void maxer(int &MAX, int CMP) { if(MAX < CMP) MAX = CMP; }
void dfs(trie *cur) {
for(int i = 0; i < mxc; i++) {
trie *nxt = cur->chi[i];
if(nxt == NULL) continue;
dfs(nxt);
// maxer(cur->len, nxt->len);
if(cur->len < nxt->len) {
cur->len = nxt->len;
cur->las = i;
}
}
}
void solve(trie *cur) {
while(cur->cnt) {
ans.push_back('P');
cur->cnt--;
}
for(int i = 0; i < mxc; i++) {
if(cur->chi[i] == NULL) continue;
if(i == cur->las) continue;
ans.push_back('a' + i);
solve(cur->chi[i]);
ans.push_back('-');
}
if(cur->las != -1) {
int i = cur->las;
ans.push_back('a' + i);
solve(cur->chi[i]);
ans.push_back('-');
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
for(int i = 0; i < n; i++) {
string s;
cin >> s;
insert(root, s);
}
dfs(root);
solve(root);
while(ans.back() != 'P')
ans.pop_back();
cout << ans.size() << endl;
for(char c : ans)
cout << c << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
760 KB |
Output is correct |
2 |
Correct |
12 ms |
1332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
2096 KB |
Output is correct |
2 |
Correct |
29 ms |
2524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
6412 KB |
Output is correct |
2 |
Correct |
165 ms |
13324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
197 ms |
15704 KB |
Output is correct |
2 |
Correct |
54 ms |
3580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
503 ms |
39056 KB |
Output is correct |
2 |
Execution timed out |
1065 ms |
89072 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
415 ms |
30388 KB |
Output is correct |
2 |
Execution timed out |
1074 ms |
105636 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |