#pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
using namespace std;
struct node {
map<int, node*> adj;
bool fin;
};
int n;
string sMx;
vector<char> ans;
void add(node* root, string s, int idx) {
if (idx == s.size()) {
root->fin = 1;
return;
}
int x = s[idx] - 'a';
auto it = root->adj.find(x);
if (it == root->adj.end()) {
root->adj[x] = new node;
it = root->adj.find(x);
}
add(it->second, s, idx+1);
}
void dfs(node* root, int idx, bool isMx) {
if (root->fin) {
ans.push_back('P');
}
int mx = -1;
for (auto& [i, v] : root->adj) {
char x = 'a' + i;
if (isMx && sMx[idx] == x) {
mx = i;
continue;
}
ans.push_back(x);
dfs(v, idx+1, 0);
ans.push_back('-');
}
if (mx != -1) {
auto it = root->adj.find(mx);
char x = 'a' + mx;
ans.push_back(x);
dfs(it->second, idx+1, 1);
ans.push_back('-');
}
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
node* root = new node;
int mx = 0;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
if (s.size() > mx) {
mx = s.size();
sMx = s;
}
add(root, s, 0);
}
dfs(root, 0, 1);
int r = ans.size()-1;
while (ans[r] == '-') r--;
cout << r+1 << "\n";
for (int i = 0; i <= r; i++) {
cout << ans[i] << "\n";
}
}
Compilation message
printer.cpp: In function 'void add(node*, std::string, int)':
printer.cpp:15:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | if (idx == s.size()) {
| ~~~~^~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:63:18: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
63 | if (s.size() > mx) {
| ~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1108 KB |
Output is correct |
2 |
Correct |
3 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
3540 KB |
Output is correct |
2 |
Incorrect |
16 ms |
7500 KB |
printed invalid word |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
8788 KB |
Output is correct |
2 |
Correct |
9 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
21356 KB |
Output is correct |
2 |
Correct |
104 ms |
49212 KB |
Output is correct |
3 |
Correct |
66 ms |
25496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
16708 KB |
Output is correct |
2 |
Correct |
133 ms |
58484 KB |
Output is correct |
3 |
Correct |
80 ms |
28928 KB |
Output is correct |
4 |
Correct |
103 ms |
55232 KB |
Output is correct |