/*
File created on 06/01/2021 at 20:53:10.
Link to problem: https://oj.uz/problem/view/IOI08_printer
Description: use a trie structures
Time complexity: O(N)
Space complexity: O(N)
Status: DEV
Copyright: Ⓒ 2021 Francois Vogel
*/
#include <iostream>
#include <cmath>
#include <vector>
#include <bitset>
#include <queue>
#include <cstring>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
using namespace std;
#define pii pair<int, int>
#define f first
// #define s second
#define pb push_back
#define ins insert
#define cls clear
#define ll long long
vector<char> steps;
int proc;
struct Node {
Node(char lt, int ld) {
t = lt;
d = ld;
md = d;
words = 0;
for (int i = 0; i < 26; i++) exist[i] = nullptr;
}
void add(string& s) {
if (s.empty()) {
words++;
return;
}
int i = s[s.size()-1]-'a';
if (exist[i] == nullptr) {
exist[i] = new Node(char(i)+'a', d+1);
c.pb(exist[i]);
}
s.pop_back();
exist[i]->add(s);
md = max(md, exist[i]->md);
}
int words, d, md;
char t;
Node* exist [26];
vector<Node*> c;
};
void dfs(Node* i) {
for (int j = 0; j < i->words; j++) {
steps.pb('P');
proc--;
}
for (Node* j : i->c) {
if (j->md == i->md) continue;
steps.pb(j->t);
dfs(j);
if (proc > 0) steps.pb('-');
}
for (Node* j : i->c) {
if (j->md != i->md) continue;
steps.pb(j->t);
dfs(j);
if (proc > 0) steps.pb('-');
}
}
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
Node* root = new Node('$', 0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string ls;
cin >> ls;
reverse(ls.begin(), ls.end());
root->add(ls);
}
proc = n;
dfs(root);
cout << steps.size() << endl;
for (char i : steps) cout << i << endl;
int d = 0;
d++;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
460 KB |
Output is correct |
2 |
Correct |
14 ms |
1360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
2124 KB |
Output is correct |
2 |
Correct |
31 ms |
2776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
7404 KB |
Output is correct |
2 |
Correct |
216 ms |
15812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
225 ms |
18504 KB |
Output is correct |
2 |
Correct |
68 ms |
4272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
617 ms |
46292 KB |
Output is correct |
2 |
Execution timed out |
1087 ms |
105548 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
463 ms |
35688 KB |
Output is correct |
2 |
Execution timed out |
1099 ms |
125376 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |