# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1043036 | Zicrus | Type Printer (IOI08_printer) | C++17 | 196 ms | 101304 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;
struct node {
public:
unordered_map<char, node*> adj;
bool end;
};
node *trie;
vector<char> res;
string longest;
void dfs(node *cur, int id) {
for (auto &e : cur->adj) {
if (id != -1 && e.first == longest[id]) continue;
res.push_back(e.first);
if (e.second->end) res.push_back('P');
dfs(e.second, -1);
}
if (id != -1 && cur->adj.count(longest[id])) {
res.push_back(longest[id]);
if (cur->adj[longest[id]]->end) res.push_back('P');
dfs(cur->adj[longest[id]], id+1);
}
res.push_back('-');
}
void solve() {
dfs(trie, 0);
}
int main() {
ll n;
cin >> n;
trie = new node();
longest = "";
vector<node*> gc = {trie};
for (int i = 0; i < n; i++) {
string s;
cin >> s;
if (s.size() > longest.size()) longest = s;
node *cur = trie;
for (int j = 0; j < s.size(); j++) {
if (cur->adj.count(s[j])) {
cur = cur->adj[s[j]];
}
else {
cur = cur->adj[s[j]] = new node();
gc.push_back(cur);
}
}
cur->end = true;
}
res = vector<char>();
solve();
while (res.back() != 'P') res.pop_back();
cout << res.size() << '\n';
for (auto &e : res) cout << e << '\n';
for (auto &e : gc) delete e;
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... |