Submission #226382

# Submission time Handle Problem Language Result Execution time Memory
226382 2020-04-23T16:30:01 Z shahriarkhan Type Printer (IOI08_printer) C++14
20 / 100
693 ms 19436 KB
#include<bits/stdc++.h>
using namespace std ;

const int maxN = 5e5 + 5 ;

int nex[maxN][26] , ed[maxN] , mark[maxN] , sz ;

vector<char> ans ;

void insert_string(string &s)
{
    int v = 0 , siz = s.size() ;
    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] ;
}

void dfs(int ind)
{
    int marked = 0 , in ;
    for(int i = 0 ; i < 26 ; ++i)
    {
        if(mark[nex[ind][i]])
        {
            marked = nex[ind][i] , in = i ;
            continue ;
        }
        if(nex[ind][i])
        {
            char c = 'a' + i ;
            ans.push_back(c) ;
            dfs(nex[ind][i]) ;
        }
    }
    if(marked)
    {
        char c = 'a' + in ;
        ans.push_back(c) ;
        dfs(marked) ;
    }
    while(ed[ind]) ans.push_back('P') , --ed[ind] ;
    if(ind) ans.push_back('-') ;
}

bool cmp(string &s , string &t)
{
    return s.size()<t.size() ;
}

int main()
{
    int n ;
    scanf("%d",&n) ;
    vector<string> vs ;
    for(int i = 0 ; i < n ; ++i)
    {
        string s ;
        cin>>s ;
        vs.push_back(s) ;
    }
    sort(vs.begin(),vs.end(),cmp) ;
    for(int i = 0 ; i < n - 1 ; ++i) insert_string(vs[i]) ;
    int v = 0 , siz = vs[n-1].size() ;
    for(int i = 0 ; i < siz ; ++i)
    {
        int c = vs[n-1][i] - 'a' ;
        if(!nex[v][c]) nex[v][c] = ++sz ;
        v = nex[v][c] ;
        mark[v] = 1 ;
    }
    ++ed[v] ;
    dfs(0) ;
    int siza = ans.size() ;
    while(ans[siza-1]!='P') --siza ;
    printf("%d\n",siza) ;
    for(int i = 0 ; i < siza ; ++i) cout<<ans[i]<<endl ;
    return 0 ;
}

Compilation message

printer.cpp: In function 'int main()':
printer.cpp:57:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&n) ;
     ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Incorrect 5 ms 384 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 34 ms 1272 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 108 ms 3348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 280 ms 8044 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 693 ms 19436 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 543 ms 15596 KB Output isn't correct
2 Halted 0 ms 0 KB -