#include "bits/stdc++.h"
using namespace std;
#ifndef LOCAL
#define debug(...) 86
#endif
const int maxN = 1e6 + 5;
const int maxC = 26;
struct node {
int end, fl;
node *child[maxC];
node() {
end = 0;
for (int i = 0; i < maxC; ++i) child[i] = NULL;
}
};
int nxt;
node nodes[maxN];
node *newnode() {
return &nodes[nxt++];
}
vector<char> res;
struct trie {
node *root;
trie() {
root = newnode();
}
void insert(node *n, string &s, int pos, int f) {
n->fl = f;
if (pos == (int)s.size()) {
n->end = 1;
return;
}
if (!n->child[s[pos] - 'a']) {
n->child[s[pos] - 'a'] = newnode();
}
insert(n->child[s[pos] - 'a'], s, pos + 1, f);
}
void solve(node *n) {
if (n->end) {
res.emplace_back('P');
}
int pend = -1;
for (int i = 0; i < maxC; ++i) {
if (!n->child[i]) continue;
if (n->child[i]->fl) {
pend = i;
continue;
}
res.emplace_back(char(i + 'a'));
solve(n->child[i]);
res.emplace_back('-');
}
if (pend != -1) {
res.emplace_back(char(pend + 'a'));
solve(n->child[pend]);
}
}
};
trie T;
string mx;
int N;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> N;
for (int i = 0; i < N; ++i) {
string s; cin >> s;
T.insert(T.root, s, 0, 0);
if ((int)s.size() > (int)mx.size()) {
mx = s;
}
}
T.insert(T.root, mx, 0, 1);
T.solve(T.root);
cout << (int)res.size() << '\n';
for (int i = 0; i < (int)res.size(); ++i) {
cout << res[i] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
211648 KB |
Output is correct |
2 |
Correct |
86 ms |
211564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
211636 KB |
Output is correct |
2 |
Correct |
88 ms |
211556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
211568 KB |
Output is correct |
2 |
Correct |
104 ms |
211584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
211612 KB |
Output is correct |
2 |
Correct |
89 ms |
211564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
211584 KB |
Output is correct |
2 |
Correct |
92 ms |
211596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
211596 KB |
Output is correct |
2 |
Correct |
90 ms |
211712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
211972 KB |
Output is correct |
2 |
Correct |
95 ms |
212024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
212232 KB |
Output is correct |
2 |
Correct |
100 ms |
211960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
212680 KB |
Output is correct |
2 |
Correct |
156 ms |
213960 KB |
Output is correct |
3 |
Correct |
138 ms |
212920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
212512 KB |
Output is correct |
2 |
Correct |
174 ms |
214936 KB |
Output is correct |
3 |
Correct |
133 ms |
213616 KB |
Output is correct |
4 |
Correct |
156 ms |
214704 KB |
Output is correct |