#include <bits/stdc++.h>
using namespace std;
template<typename T> void maxer(T &MAX, T CMP) { if(MAX < CMP) MAX = CMP; }
template<typename T> void miner(T &MIN, T CMP) { if(MIN > CMP) MIN = CMP; }
const int mxc = 26;
struct trie {
trie *chi[mxc];
int cnt, len;
trie() {
for(int i = 0; i < mxc; i++)
chi[i] = NULL;
cnt = len = 0;
}
};
trie *root = new trie();
void insert(string s) {
trie *cur = root;
for(char c : s) {
if(!cur->chi[c - 'a'])
cur->chi[c - 'a'] = new trie();
maxer(cur->len, (int)s.size());
cur = cur->chi[c - 'a'];
}
cur->len = s.size();
cur->cnt++;
}
vector<char> ans;
void solve(trie *cur) {
while(cur->cnt) {
ans.push_back('P');
cur->cnt--;
}
for(int i = 0; i < mxc; i++) {
trie *nxt = cur->chi[i];
if(nxt == NULL) continue;
if(nxt->len == cur->len) continue;
ans.push_back('a' + i);
solve(nxt);
ans.push_back('-');
}
for(int i = 0; i < mxc; i++) {
trie *nxt = cur->chi[i];
if(nxt == NULL) continue;
if(nxt->len != cur->len) continue;
ans.push_back('a' + i);
solve(nxt);
ans.push_back('-');
}
}
int main() {
int n;
cin >> n;
string s;
for(int i = 0; i < n; i++) {
cin >> s;
insert(s);
}
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 |
Incorrect |
2 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
1872 KB |
Output is correct |
2 |
Incorrect |
28 ms |
2376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
5968 KB |
Output is correct |
2 |
Correct |
163 ms |
12616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
198 ms |
14792 KB |
Output is correct |
2 |
Correct |
62 ms |
3152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
497 ms |
36448 KB |
Output is correct |
2 |
Execution timed out |
1061 ms |
83220 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
399 ms |
28576 KB |
Output is correct |
2 |
Execution timed out |
1049 ms |
98828 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |