| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1363685 | vjudge1 | Type Printer (IOI08_printer) | C++20 | 44 ms | 51156 KiB |
/*
Author: Nhan Nguyen
School: University of Science, VNU-HCM
*/
#include <bits/stdc++.h>
#define sz(u) (int((u).size()))
#define all(x) (x).begin(), (x).end()
using namespace std;
const int maxNodes = int(5e5) + 10;
struct Node {
int child[26];
int exist, cnt;
};
struct Trie {
int cur;
bool longest[maxNodes];
Node node[maxNodes];
Trie() : cur(0) {
memset(node[0].child, -1, sizeof(node[0].child));
node[0].exist = node[0].cnt = 0;
}
int new_node() {
cur++;
memset(node[cur].child, -1, sizeof(node[cur].child));
node[cur].exist = node[cur].cnt = 0;
return cur;
}
void add_string(string s) {
int pos = 0;
for (auto f : s) {
int c = f - 'a';
if (node[pos].child[c] == -1) node[pos].child[c] = new_node();
pos = node[pos].child[c];
node[pos].cnt++;
}
node[pos].exist++;
}
void mark(string s) {
int pos = 0;
for (auto f : s) {
int c = f - 'a';
if (node[pos].child[c] == -1) return;
pos = node[pos].child[c];
longest[pos] = true;
}
}
};
int N;
vector<string> print;
vector<char> ans;
Trie tree;
void dfs(int pos) {
for (int i = 0; i < tree.node[pos].exist; ++i) ans.push_back('P');
int special = -1;
for (int i = 0; i < 26; ++i) {
int v = tree.node[pos].child[i];
if (v != -1) {
if (tree.longest[v]) {
special = i;
} else {
ans.push_back(i + 'a');
dfs(v);
ans.push_back('-');
}
}
}
if (special != -1) {
ans.push_back(special + 'a');
dfs(tree.node[pos].child[special]);
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
if (fopen("inp.txt", "r")) {
freopen("inp.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
cin >> N;
string longest_string = "";
for (int i = 1; i <= N; ++i) {
string s;
cin >> s;
if (sz(longest_string) < sz(s)) longest_string = s;
tree.add_string(s);
}
tree.mark(longest_string);
dfs(0);
cout << sz(ans) << '\n';
for (auto c : ans) cout << c << '\n';
return 0;
}Compilation message (stderr)
| # | Result | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Result | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Result | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Result | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Result | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Result | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Result | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Result | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Result | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Result | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
