#include <bits/stdc++.h>
using namespace std;
const int MXN = 25000 * 20 + 5;
int n, m;
int node[MXN][26], cnt[MXN];
vector<int> adj[MXN];
char val[MXN];
int mx[MXN], cur;
vector<char> moves;
void add(int a, int i, string &s)
{
if (i == s.length())
{
cnt[a]++;
mx[a] = max(mx[a], (int)s.length());
return;
}
if (!node[a][s[i] - 'a']) node[a][s[i] - 'a'] = ++m, adj[a].push_back(m), val[m] = s[i];
add(node[a][s[i] - 'a'], i + 1, s);
mx[a] = max(mx[a], mx[node[a][s[i] - 'a']]);
}
void dfs(int a)
{
if (a) moves.push_back(val[a]);
for (int i = 0; i < cnt[a]; i++) moves.push_back('P'), cur++;
for (int v : adj[a]) dfs(v);
if (cur != n) moves.push_back('-');
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
string s[n];
for (string &i : s) cin >> i, add(0, 0, i);
for (int i = 0; i <= m; i++)
{
sort(adj[i].begin(), adj[i].end(), [&](const int &a, const int &b)
{
return mx[a] < mx[b];
});
}
dfs(0);
cout << moves.size();
for (char &ch : moves) cout << '\n' << ch;
}
Compilation message
printer.cpp: In function 'void add(int, int, std::string&)':
printer.cpp:16:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | if (i == s.length())
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
15196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
15708 KB |
Output is correct |
2 |
Correct |
4 ms |
15960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
18520 KB |
Output is correct |
2 |
Correct |
14 ms |
22620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
24024 KB |
Output is correct |
2 |
Correct |
8 ms |
17244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
37844 KB |
Output is correct |
2 |
Correct |
75 ms |
68332 KB |
Output is correct |
3 |
Correct |
43 ms |
42708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
32536 KB |
Output is correct |
2 |
Correct |
87 ms |
78532 KB |
Output is correct |
3 |
Correct |
47 ms |
46544 KB |
Output is correct |
4 |
Correct |
79 ms |
74952 KB |
Output is correct |