#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T> using indexed_set = tree<T, null_type, less<T>,
rb_tree_tag, tree_order_statistics_node_update>;
#define watch(x) cerr << "{" << (#x) << " = " << x << "}" << '\n';
#define watch2(x, y) cerr << "{" << (#x) << " = " << x << ", " << (#y) << " = " << y << "}" << '\n';
#define watch3(x, y, z) cerr << "{" << (#x) << " = " << x << ", " << (#y) << " = " << y << ", " << (#z) << " = " << z << "}" << '\n';
#define fast_io ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
const ll maxn = 5e5 + 10;
int n, t[maxn][30], nodes = 1;
set<int> en;
vector<char> ans;
void add (string &s)
{
int v = 0;
for (int i = 0; i < s.size(); i++) {
if (!t[v][s[i] - 'a']) t[v][s[i] - 'a'] = nodes++;
v = t[v][s[i] - 'a'];
}
en.insert(v);
}
vector<int> lasts;
void dfs (int v)
{
if (en.count(v)) {
ans.push_back('P');
en.erase(v);
}
int i = -1;
for (int x = 0; x < 26; x++) {
if (t[v][x]) {
if (t[v][x] == lasts.back()) {i = x; continue;}
ans.push_back((char)('a' + x));
dfs(t[v][x]);
}
}
if (i != -1) {
ans.push_back((char)('a' + i));
lasts.pop_back();
dfs(t[v][i]);
}
if (en.size())
ans.push_back('-');
}
int main ()
{
cin >> n;
string mx;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
if (s.size() > mx.size()) mx = s;
add(s);
}
int v = 0;
for (int i = 0; i < mx.size(); i++) {
v = t[v][mx[i] - 'a'];
lasts.push_back(v);
}
reverse(lasts.begin(), lasts.end());
dfs(0);
cout << ans.size() << '\n';
for (char c : ans)
cout << c << '\n';
return 0;
}
Compilation message
printer.cpp: In function 'void add(std::string&)':
printer.cpp:26:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:73:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for (int i = 0; i < mx.size(); i++) {
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1096 KB |
Output is correct |
2 |
Correct |
3 ms |
1320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
3460 KB |
Output is correct |
2 |
Correct |
19 ms |
7364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
8592 KB |
Output is correct |
2 |
Correct |
14 ms |
2508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
21024 KB |
Output is correct |
2 |
Correct |
126 ms |
46780 KB |
Output is correct |
3 |
Correct |
80 ms |
24580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
16972 KB |
Output is correct |
2 |
Correct |
152 ms |
56140 KB |
Output is correct |
3 |
Correct |
87 ms |
28492 KB |
Output is correct |
4 |
Correct |
142 ms |
53156 KB |
Output is correct |