# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1083549 | SulA | Type Printer (IOI08_printer) | C++17 | 121 ms | 99512 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 <bits/stdc++.h>
using namespace std;
struct trie {
trie* child[26] = {};
bool end = false;
void insert(const string& s, int ind = 0) {
if (ind == s.size()) {
end = true;
} else {
if (child[ s[ind] - 'a' ] == nullptr) child[ s[ind] - 'a' ] = new trie();
child[ s[ind] - 'a' ]->insert(s, ind + 1);
}
}
string dfs(const string& longest, int ind = 0, bool branch = true) {
string oper = "";
if (end) {
oper += "P";
}
if (branch) {
for (int i = 0; i < 26; i++) if (i + 'a' != longest[ind] && child[i] != nullptr) {
oper += char(i + 'a');
oper += child[i]->dfs(longest, ind + 1, false);
oper += "-";
}
if (ind < longest.size()) {
oper += longest[ind] + child[ longest[ind] - 'a' ]->dfs(longest, ind + 1, true);
}
} else {
for (int i = 0; i < 26; i++) if (child[i] != nullptr) {
oper += char(i + 'a');
oper += child[i]->dfs(longest, ind + 1, false);
oper += "-";
}
}
return oper;
}
};
trie* root = new trie();
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int n; cin >> n;
string longest;
while (n--) {
string s; cin >> s;
root->insert(s);
if (s.size() > longest.size()) {
swap(s, longest);
}
}
string ans = root->dfs(longest);
cout << ans.size() << "\n";
for (char c : ans) cout << c << "\n";
}
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... |