#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--;
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 |
Incorrect |
1 ms |
212 KB |
Expected integer, but "t" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
320 KB |
Expected integer, but "e" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Expected integer, but "h" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Expected integer, but "b" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Expected integer, but "a" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
1108 KB |
Expected integer, but "a" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
3668 KB |
Expected integer, but "a" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
8792 KB |
Expected integer, but "a" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
49 ms |
21636 KB |
Expected integer, but "a" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
41 ms |
16920 KB |
Expected integer, but "a" found |
2 |
Halted |
0 ms |
0 KB |
- |