# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
387379 | stefantaga | Type Printer (IOI08_printer) | C++14 | 191 ms | 89568 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;
struct trie
{
int nr,marime;
trie *v[28];
trie ()
{
int j;
nr=0;
marime=0;
for (j=0;j<=26;j++)
{
v[j]=nullptr;
}
}
} *tree = new trie ;
void adauga (trie *tree, char *s)
{
if (*s==0)
{
tree->nr++;
return;
}
int loc=(*s-'a');
if (tree->v[loc]==nullptr)
{
tree->v[loc]=new trie;
}
adauga(tree->v[loc],s+1);
}
vector <char> op;
void dfs1(trie *tree)
{
int j;
tree->marime=1+tree->nr;
for (j=0;j<=26;j++)
{
if (tree->v[j]!=nullptr)
{
dfs1(tree->v[j]);
tree->marime=tree->marime+tree->v[j]->marime;
}
}
}
void dfs2(trie *tree)
{
int j;
for (j=1;j<=tree->nr;j++)
{
op.push_back('P');
}
vector <pair <int,int> > val;
for (j=0;j<=26;j++)
{
if (tree->v[j]!=nullptr)
{
val.push_back({tree->v[j]->marime,j});
}
}
sort (val.begin(),val.end());
for (j=0;j<val.size();j++)
{
op.push_back((char)(val[j].second+97));
dfs2(tree->v[val[j].second]);
op.push_back('-');
}
}
int n,i,j;
char s[25];
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(0);
#ifdef HOME
ifstream cin("date.in");
ofstream cout("date.out");
#endif // HOME
cin>>n;
for (i=1;i<=n;i++)
{
cin>>s;
adauga(tree,s);
}
dfs1(tree);
dfs2(tree);
while (op.size()>0&&op[op.size()-1]=='-')
{
op.pop_back();
}
cout<<op.size()<<'\n';
for (j=0;j<op.size();j++)
{
cout<<op[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... |