# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
688607 | danikoynov | Type Printer (IOI08_printer) | C++14 | 153 ms | 106376 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.
/**
____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|
**/
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int alphabet = 26;
struct trie
{
trie *nxt[alphabet];
int max_depth, depth, word_cnt, front_move;
trie(int _depth = 0)
{
for (int i = 0; i < alphabet; i ++)
nxt[i] = NULL;
word_cnt = 0, depth = _depth;
max_depth = depth;
front_move = -1;
}
};
void add(trie *root, string &word, int pos)
{
if (pos == word.size())
{
root -> word_cnt ++;
return;
}
int ch = word[pos] - 'a';
if (root -> nxt[ch] == NULL)
root -> nxt[ch] = new trie(root -> depth + 1);
add(root -> nxt[ch], word, pos + 1);
if (root -> nxt[ch] -> max_depth > root -> max_depth)
{
root -> front_move = ch;
root -> max_depth = root -> nxt[ch] -> max_depth;
}
}
vector < char > path;
void print(trie *root, bool save)
{
///cout << root -> depth
while(root -> word_cnt > 0)
{
path.push_back('P');
root -> word_cnt --;
}
for (int i = 0; i < alphabet; i ++)
{
if (root -> nxt[i] == NULL)
continue;
if (i != root -> front_move)
{
path.push_back((char)(i + 'a'));
print(root -> nxt[i], false);
path.push_back('-');
}
}
if (root -> front_move != -1)
{
path.push_back((char)(root -> front_move + 'a'));
print(root -> nxt[root -> front_move], save);
if (!save)
path.push_back('-');
}
}
int n;
trie *tree = new trie(0);
void solve()
{
string word;
cin >> n;
for (int i = 1; i <= n; i ++)
{
cin >> word;
add(tree, word, 0);
}
print(tree, true);
cout << path.size() << endl;
for (char c : path)
cout << c << endl;
}
int main()
{
solve();
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... |