#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;
class Trie {
public:
vector<vector<int>> adj, depth;
vector<int> str_terms, max_depth;
const vector<int> new_node = vector<int>(26, -1); // lista padrao pra novos nos
Trie() {
adj.pb(new_node);
depth.pb(new_node);
str_terms.pb(0);
}
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]++;
}
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;
// printa todas as strings que terminam ali
rep(i, 0, 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
printer.cpp: In member function 'void Trie::add_str(std::string&)':
printer.cpp:34: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]
34 | while (idx < s.size()) {
| ~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
848 KB |
Output is correct |
2 |
Correct |
4 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4300 KB |
Output is correct |
2 |
Correct |
11 ms |
5960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
16316 KB |
Output is correct |
2 |
Correct |
42 ms |
34484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
43420 KB |
Output is correct |
2 |
Correct |
14 ms |
10188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
103620 KB |
Output is correct |
2 |
Correct |
258 ms |
231296 KB |
Output is correct |
3 |
Correct |
142 ms |
120936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
83096 KB |
Output is correct |
2 |
Runtime error |
235 ms |
262144 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |