#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;
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]);
}
// printa todas as strings que terminam ali
rep(i, 0, tri.str_terms[no]) {
ans.pb("P");
printados++;
}
// 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 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
848 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
4468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
19 ms |
16316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
48 ms |
43412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
125 ms |
103564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
102 ms |
83352 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |