# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
226392 | shahriarkhan | Type Printer (IOI08_printer) | C++14 | 172 ms | 51308 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 ;
const int maxN = 5e5 + 5 ;
int nex[maxN][26] , dep[maxN] , ed[maxN] , sz ;
char s[25] ;
vector<char> ans ;
void insert_string()
{
int v = 0 , siz = strlen(s) ;
for(int i = 0 ; i < siz ; ++i)
{
int c = s[i]-'a' ;
if(!nex[v][c]) nex[v][c] = ++sz ;
v = nex[v][c] ;
}
++ed[v] ;
}
int depth(int ind)
{
int p = 0 ;
for(int i = 0 ; i < 26 ; ++i)
{
if(nex[ind][i])
{
p = 1 ;
dep[ind] = max(dep[ind],depth(nex[ind][i]) + 1) ;
}
}
if(!p) dep[ind] = 1 ;
return dep[ind] ;
}
void dfs(int ind)
{
int p = 0 ;
vector<pair<int,int> > vx ;
while(ed[ind]) ed[ind]-- , ans.push_back('P') ;
for(int i = 0 ; i < 26 ; ++i)
{
if(nex[ind][i])
{
vx.push_back({dep[nex[ind][i]],i}) ;
++p ;
}
}
sort(vx.begin(),vx.end()) ;
for(int i = 0 ; i < p ; ++i)
{
char c = 'a' + vx[i].second ;
ans.push_back(c) ;
dfs(nex[ind][vx[i].second]) ;
ans.push_back('-') ;
}
}
int main()
{
int n ;
scanf("%d",&n) ;
for(int i = 0 ; i < n ; ++i)
{
scanf("%s",s) ;
insert_string() ;
}
depth(0) ;
dfs(0) ;
int siz = ans.size() ;
while(ans[siz-1]!='P') --siz ;
printf("%d\n",siz) ;
for(int i = 0 ; i < siz ; ++i) printf("%c\n",ans[i]);
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... |