# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
712540 | caccaccac | Type Printer (IOI08_printer) | C++14 | 146 ms | 99584 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
struct TrieNode {
TrieNode *child[26];
bool end = 0;
int max_h = 0;
TrieNode() {
for (int i = 0; i < 26; i++) {
child[i] = NULL;
}
end = 0;
max_h = 0;
}
};
TrieNode *root = new TrieNode();
void add(string &s) {
TrieNode *u = root;
for (char ch : s) {
ch -= 'a';
if (u->child[ch] == NULL) {
u->child[ch] = new TrieNode();
}
u = u->child[ch];
}
u->end = 1;
}
string f;
/**
* minimize the minus in the end --> write the longest word in the end
*/
void dfs(TrieNode *u) {
for (int i = 0; i < 26; i++) {
if (u->child[i] != NULL) {
dfs(u->child[i]);
u->max_h = max(u->max_h, u->child[i]->max_h + 1);
}
}
}
void dfs_ans(TrieNode *u) {
if (u->end) f += 'P';
int longest = -1;
for (int i = 0; i < 26; i++) {
if (u->child[i] != NULL) {
if (longest == -1) {
longest = i;
} else if (u->child[longest]->max_h < u->child[i]->max_h) {
longest = i;
}
}
}
for (int i = 0; i < 26; i++) {
if (longest == i) {
continue;
}
if (u->child[i] != NULL) {
f += char(i + 'a');
dfs_ans(u->child[i]);
f += '-';
}
}
if (longest != -1) {
f += char(longest + 'a');
dfs_ans(u->child[longest]);
f += '-';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define task "a"
if (fopen(task ".inp", "r")) {
freopen(task ".inp", "r", stdin);
freopen(task ".out", "w", stdout);
}
int n;
cin >> n;
while (n--) {
string s;
cin >> s;
add(s);
}
dfs(root);
dfs_ans(root);
while (f.back() == '-') f.pop_back();
cout << f.size() << "\n";
for (char ch : f) cout << ch << "\n";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | 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... |