# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
699129 | Doncho_Bonboncho | Type Printer (IOI08_printer) | C++14 | 125 ms | 70836 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>
#include <iterator>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MAX_N = 1e6;
const int INF = 1e9;
const int mod = 1e9 + 7;
struct trie{
int go[26];
bool word;
bool big[26];
trie( ){
for( int i=0 ; i<26 ; i++ ){
go[i] = -1;
big[i] = false;
}
word = false;
}
};
std::vector<trie> tree;
int size = 1;
void add( const std::string& word, int currNode, int indWord ){
if( indWord == word.size() ){
tree[currNode].word = true;
return;
}
int currW = word[indWord] - 'a';
if( tree[currNode].go[currW] == -1 ){
tree[currNode].go[currW] = size;
tree.push_back(trie());
size++;
}
add( word, tree[currNode].go[currW], indWord +1 );
}
void addBig( const std::string& word, int currNode, int indWord ){
if( word.size() == indWord ){
tree[currNode].word = true;
return;
}
int currW = word[indWord] - 'a';
if( tree[currNode].go[currW] == -1 ){
tree[currNode].go[currW] = size;
tree.push_back(trie());
size++;
}
tree[currNode].big[currW] = true;
addBig( word, tree[currNode].go[currW], indWord +1 );
}
std::vector<char> nas;
void traverse( int currNode ){
if( tree[currNode].word ) nas.push_back('P');
for( int i=0 ; i<26 ; i++ ){
if( tree[currNode].go[i] != -1 and !tree[currNode].big[i] ){
nas.push_back(('a' + i));
traverse( tree[currNode].go[i] );
nas.push_back('-');
}
}
for( int i=0 ; i<26 ; i++ ){
if( tree[currNode].go[i] != -1 and tree[currNode].big[i] ){
nas.push_back(('a' + i));
traverse( tree[currNode].go[i] );
nas.push_back('-');
}
}
}
int main () {
std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
int n;
std::cin>>n;
tree.push_back( trie() );
std::vector<std::pair<int, std::string>> inp;
inp.resize(n);
for( int i=0 ; i<n ; i++ ){
std::cin>>inp[i].second;
inp[i].first = inp[i].second.size();
}
std::sort( inp.begin(), inp.end() );
for( int i=0 ; i<n ; i++ ){
// add( inp[i].second, 0, 0 );
if( i != n-1 ) add( inp[i].second, 0, 0 );
else addBig( inp[i].second, 0, 0 );
}
traverse( 0 );
while( nas.back() == '-' ) nas.pop_back();
std::cout<<nas.size()<<"\n";
for( auto j : nas ) std::cout<<j<<"\n";
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... |