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;
#define endl '\n'
//typedef long long ll;
#define int long long
typedef pair<int, int> pii;
typedef tuple<int, int, int> t3i;
#define rep(a, b, c) for(int a = (int)b ; a < (int)c ; a++)
#define repi(a, b, c) for(int a = (int)b ; a >= (int)c ; a--)
#define ff first
#define ss second
#define pb push_back
//const int oo = 2e9;
const int oo = 4e18;
const vector<int> new_node = vector<int>(26, -1); // lista padrao pra novos nos
class Trie {
public:
vector<vector<int>> adj, depth;
vector<int> max_depth;
vector<bool> str_terms;
Trie() {
adj.pb(new_node);
depth.pb(new_node);
str_terms.pb(false);
}
void add_str(string &s) {
int idx = 0, no = 0;
while (idx < s.size()) {
if (adj[no][s[idx] - 'a'] != -1) no = adj[no][s[idx] - 'a'];
else {
adj[no][s[idx] - 'a'] = adj.size();
no = adj.size();
adj.pb(new_node);
depth.pb(new_node);
str_terms.pb(0);
}
idx++;
}
str_terms[no] = true;
}
void process_depth() {
max_depth = vector<int>(adj.size(), 0);
repi(no, adj.size()-1, 0) {
int mai = 0;
rep(i, 0, 26) {
if (adj[no][i] == -1) depth[no][i] = 0;
else depth[no][i] = 1 + max_depth[adj[no][i]];
mai = max(mai, depth[no][i]);
}
max_depth[no] = mai;
}
}
};
vector<string> ans;
int n, printados = 0;
Trie tri;
void dfs(int no) {
priority_queue<pii, vector<pii>, greater<pii>> pq;
int d, c;
// como todas as strs sao distintas, nao vao ter varias que terminam no msm lugar
if (tri.str_terms[no]) {
ans.pb("P");
printados++;
}
rep(i, 0, 26) {
if (tri.adj[no][i] == -1) continue;
pq.push({tri.max_depth[tri.adj[no][i]], i});
}
while (!pq.empty()) {
tie(d, c) = pq.top();
pq.pop();
// digita o caracter e continua
ans.pb(string(1, c + 'a'));
dfs(tri.adj[no][c]);
}
// apaga o caracter se ainda tem que printar alguma coisa
if (printados != n) ans.pb("-");
}
void solve() {
string s;
cin >> n;
rep(i, 0, n) {
cin >> s;
tri.add_str(s);
}
tri.process_depth();
dfs(0);
cout << ans.size() << endl;
for (string a : ans)
cout << a << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//int t;
//cin >> t;
//while (t--)
solve();
return 0;
}
Compilation message (stderr)
printer.cpp: In member function 'void Trie::add_str(std::string&)':
printer.cpp:35:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | while (idx < s.size()) {
| ~~~~^~~~~~~~~~
# | 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... |