# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
555974 | iDoItSaNdWiTcH | Type Printer (IOI08_printer) | C++17 | 1080 ms | 99504 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;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = 1e9 + 5;
//////////////////////////////////////////////////////////////////////////////////////////
string cur = "";
struct Trie {
int totalsz = 0;
bool isend = 0;
struct Trie* children[26] = {};
};
void tinsert(struct Trie* root, string s) {
//find the bug in tinsert
Trie* cnt = root;
for (int i = 0; i < s.length(); i++) {
if (cnt->children[s[i] - 'a'] == nullptr) {
//create new tree
cnt->children[s[i] - 'a'] = new Trie();
}
//calculate how much is left]
int sz = cnt->children[s[i] - 'a']->totalsz;
if (s.length() - i - 1 > sz) {
(cnt->children[s[i] - 'a']->totalsz) = s.length() - i - 1;
}
cnt = cnt->children[s[i] - 'a'];
}
cnt->isend = 1;
}
void dfs(Trie* root) {
//theres a string that ends here
if (root->isend) {
cur += "P";
}
//first find the one with longest string
int longestsz = 0;
int c = 0;
for (int i = 0; i < 26; i++) {
if (root->children[i] != nullptr) {
if (root->children[i]->totalsz > longestsz) {
longestsz = root->children[i]->totalsz;
c = i;
}
}
}
//do the others
for (int i = 0;i < 26; i++) {
if (root->children[i] != nullptr && i != c) {
cur += char('a' + i);
dfs(root->children[i]);
cur += "-";
}
}
//last do back the longest string
if (root->children[c] != nullptr) {
cur += char('a' + c);
dfs(root->children[c]);
cur += "-";
}
}
void solve(){
int n; cin >> n;
struct Trie* root = new Trie();
while (n--) {
string s; cin >> s;
tinsert(root, s);
}
dfs(root);
int p = 0;
for (int i = cur.length() - 1; i > -1; i--) {
if (cur[i] == '-') p++;
else break;
}
int maxx = cur.length() - p;
cout << maxx << endl;
for (int i = 0; i < maxx; i++) {
cout << cur[i] << endl;
}
}
//////////////////////////////////////////////////////////////////////////////////////////
int main(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
//cin >> t;
while (t--){
solve();
}
}
/*
find a method to dfs the longest strings at last
*/
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... |