# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
521592 | vlad_TT | Type Printer (IOI08_printer) | C++17 | 57 ms | 76448 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <cstring>
const int SIGMA = 26;
struct Trie {
int words;
Trie* children[SIGMA];
bool is_ancestor;
Trie() {
words = 0;
is_ancestor = false;
for (int i = 0; i < SIGMA; i++) {
children[i] = NULL;
}
}
};
void insert(Trie* root, char* S) {
if (*S == '\0') {
root->words++;
} else {
if (root->children[S[0] - 'a'] == NULL) {
root->children[S[0] - 'a'] = new Trie();
}
insert(root->children[S[0] - 'a'], S + 1);
}
}
void stramosi(Trie* root, char* S) {
if (*S != '\0') {
root->children[S[0] - 'a']->is_ancestor = true;
stramosi(root->children[S[0] - 'a'], S + 1);
}
}
int answer = 0;
int count(Trie* root) {
answer += root->words;
for (int i = 0; i < SIGMA; i++) {
if (root->children[i] != NULL && root->children[i]->is_ancestor == false) {
answer++;
count(root->children[i]);
answer++;
}
}
for (int i = 0; i < SIGMA; i++) {
if (root->children[i] != NULL && root->children[i]->is_ancestor == true) {
answer++;
count(root->children[i]);
}
}
}
void dfs(Trie* root) {
for (int i = 1; i <= root->words; i++) {
std::cout << "P\n";
}
for (int i = 0; i < SIGMA; i++) {
if (root->children[i] != NULL && root->children[i]->is_ancestor == false) {
std::cout << char(i + 'a') << "\n";
dfs(root->children[i]);
std::cout << "-\n";
}
}
for (int i = 0; i < SIGMA; i++) {
if (root->children[i] != NULL && root->children[i]->is_ancestor == true) {
std::cout << char(i + 'a') << "\n";
dfs(root->children[i]);
}
}
}
int main() {
Trie root;
char* S = new char[20 + 1];
char last[20 + 1];
int n, mx = 0;
std::cin >> n;
for (int i = 1; i <= n; i++) {
std::cin >> S;
if (strlen(S) > mx) {
mx = strlen(S);
for (int j = 0; S[j] != '\0'; j++) {
last[j] = S[j];
}
}
insert(&root, S);
}
char c;
c = std::cin.get();
if (c == ' ') {
c = std::cin.get();
}
stramosi(&root, last);
count(&root);
std::cout << answer << "\n";
dfs(&root);
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... |