이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
string oper;
struct trie {
trie *ch[26] = {};
int mxdepth = 0, subtsize = 0;
bool isword = false;
void insert(string &s) {
trie *node = this;
for (char &c : s) {
int idx = c - 'a';
if (!node->ch[idx]) node->ch[idx] = new trie();
node->subtsize++;
node = node->ch[idx];
}
node->isword = true;
}
void calcmxdepth() {
for (int i = 0; i < 26; i++) {
if (!ch[i]) continue;
ch[i]->calcmxdepth();
mxdepth = max(mxdepth, ch[i]->mxdepth + 1);
}
}
void construct() {
if (isword) oper += 'P';
vector <int> order;
for (int i = 0; i < 26; i++) if (ch[i]) order.push_back(i);
sort(order.begin(), order.end(), [&](int &a, int &b) {
return ch[a]->mxdepth < ch[b]->mxdepth;
});
for (int &i : order) {
oper += char(i + 'a');
ch[i]->construct();
oper += '-';
}
}
} t;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
t.insert(s);
}
t.calcmxdepth();
t.construct();
while (oper.back() == '-') oper.pop_back();
cout << oper.size();
for (char &c : oper) cout << '\n' << c;
return 0;
}
# | 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... |