| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1365918 | vjudge1 | Type Printer (IOI08_printer) | C++20 | 0 ms | 344 KiB |
#include<bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
#define endl '\n'
#define ll long long
int n;
string res;
int numPrints = 0;
struct node{
bool type = false;
int numbWords = 0;
node *child[26];
};
node *createNode() {
node *p = new node();
for (int i = 0; i < 26; i++) {
p->child[i] = NULL;
}
return p;
}
void addWord(node *root, string s, bool ok) {
node *p = root;
for (char c : s) {
if (p->child[c - 'a'] == NULL) {
p->child[c - 'a'] = createNode();
}
p = p->child[c - 'a'];
p->type = ok;
}
p->numbWords++;
}
void dfs(node* root) {
for (int i = 1; i <= root->numbWords; i++) {
res += 'P';
numPrints++;
if (numPrints == n) {
cout << res.size() << endl;
for (int i = 0; i < res.size(); i++) cout << res[i] << endl;
cout << endl;
exit(0);
}
}
char chLast = '.';
for (int i = 0; i < 26; i++) {
if (root->child[i] != NULL && root->child[i]->type == true) {
chLast = (char) (i + 'a');
}
}
for (int i = 0; i < 26; i++) {
if (root->child[i] == NULL || (char) (i + 'a') == chLast) {
continue;
}
res += (char) (i + 'a');
dfs(root->child[i]);
res += '-';
}
if (chLast != '.') {
res += chLast;
dfs(root->child[(chLast - 'a')]);
}
}
void solve() {
cin >> n;
string s[n + 1];
string strMax;
int lenMax = 0;
for (int i = 0; i < n; i++) {
cin >> s[i];
if (lenMax < s[i].size()) {
strMax = s[i];
lenMax = s[i].size();
}
}
node *root = createNode();
for (int i = 0; i < n; i++) {
if (s[i] != strMax) {
addWord(root, s[i], false);
}
}
addWord(root, strMax, true);
dfs(root);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("inp.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
solve();
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... | ||||
