This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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] = 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 (stderr)
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())
| ~~^~~~~~~~~~~~~
printer.cpp: In function 'void dfs(int)':
printer.cpp:28:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
28 | if (a); moves.push_back(val[a]);
| ^~
printer.cpp:28:13: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
28 | if (a); moves.push_back(val[a]);
| ^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |