#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;
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) {
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);
}
void solve(node *n) {
if (n->end) res.emplace_back('P');
for (int i = 0; i < maxC; ++i) {
if (!n->child[i]) continue;
res.emplace_back(char(i + 'a'));
solve(n->child[i]);
res.emplace_back('-');
}
}
};
trie T;
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);
}
T.solve(T.root);
int mx = 0, cnt = 0, pos;
for (int i = 0; i < (int)res.size(); ++i) {
if (res[i] == '-') {
++cnt;
if (mx < cnt) {
mx = cnt;
pos = i;
}
}
else cnt = 0;
}
int j = pos - mx;
cnt = 0;
while (j >= 0) {
if (res[j] == 'P') {
}
else if (res[j] == '-') {
--cnt;
}
else {
++cnt;
if (cnt == mx) {
break;
}
}
--j;
}
for (int i = j; i <= pos - mx; ++i) {
res.emplace_back(res[i]);
}
cout << (int)res.size() - (pos - j + 1) << '\n';
for (int i = 0; i < (int)res.size(); ++i) {
if (i >= j && i <= pos) continue;
cout << res[i] << '\n';
}
}
Compilation message
printer.cpp: In function 'int main()':
printer.cpp:107:25: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
107 | if (i >= j && i <= pos) continue;
| ~~^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
211584 KB |
Output is correct |
2 |
Correct |
87 ms |
211556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
211628 KB |
Output is correct |
2 |
Correct |
100 ms |
211572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
211540 KB |
Output is correct |
2 |
Incorrect |
89 ms |
211600 KB |
printed invalid word |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
211532 KB |
Output is correct |
2 |
Correct |
92 ms |
211572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
91 ms |
211588 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
88 ms |
211708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
211776 KB |
Output is correct |
2 |
Incorrect |
101 ms |
212172 KB |
printed invalid word |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
105 ms |
212184 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
138 ms |
212700 KB |
Output is correct |
2 |
Correct |
182 ms |
214612 KB |
Output is correct |
3 |
Incorrect |
139 ms |
213372 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
120 ms |
212556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |