# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
636143 | tvladm2009 | Type Printer (IOI08_printer) | C++14 | 214 ms | 105488 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
using namespace std;
const int SIGMA = 26;
struct Trie {
Trie *sons[SIGMA];
int freq;
int words;
bool is_ancestor;
Trie() {
freq = 0;
words = 0;
is_ancestor = false;
for (int i = 0; i < SIGMA; i++) {
sons[i] = NULL;
}
}
};
void ins(Trie *&root, string s, int pos = 0) {
if (pos == s.size()) {
root->words++;
} else {
if (root->sons[s[pos] - 'a'] == NULL) {
root->sons[s[pos] - 'a'] = new Trie();
}
ins(root->sons[s[pos] - 'a'], s, pos + 1);
}
}
int n, answer = 0;
void stramosi(Trie *&root, string s, int pos = 0) {
if (pos != s.size()) {
root->sons[s[pos] - 'a']->is_ancestor = true;
stramosi(root->sons[s[pos] - 'a'], s, pos + 1);
}
}
void solve(Trie *&root) {
answer += root->words;
for (int i = 0; i < SIGMA; i++) {
if (root->sons[i] != NULL && root->sons[i]->is_ancestor == false) {
answer++;
solve(root->sons[i]);
answer++;
}
}
for (int i = 0; i < SIGMA; i++) {
if (root->sons[i] != NULL && root->sons[i]->is_ancestor == true) {
answer++;
solve(root->sons[i]);
}
}
}
void dfs(Trie *&root) {
for (int i = 1; i <= root->words; i++) {
cout << "P\n";
}
for (int i = 0; i < SIGMA; i++) {
if (root->sons[i] != NULL && root->sons[i]->is_ancestor == false) {
cout << char(i + 'a') << "\n";
dfs(root->sons[i]);
cout << "-\n";
}
}
for (int i = 0; i < SIGMA; i++) {
if (root->sons[i] != NULL && root->sons[i]->is_ancestor == true) {
cout << char(i + 'a') << "\n";
dfs(root->sons[i]);
}
}
}
int main() {
cin >> n;
Trie *root = new Trie();
string tmp = "";
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
if (s.size() > tmp.size()) {
tmp = s;
}
ins(root, s);
}
stramosi(root, tmp);
solve(root);
cout << answer << "\n";
dfs(root);
return 0;
}
Compilation message (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... |