Submission #208275

# Submission time Handle Problem Language Result Execution time Memory
208275 2020-03-10T13:36:48 Z DodgeBallMan Type Printer (IOI08_printer) C++14
100 / 100
152 ms 66412 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 5e5 + 10;
int t[N][30], n, ptr, sub[N], hv[N];
bool leaf[N];
vector<char> c;
void dfs( int u ) {
    sub[u] = 0;
    for( int i = 0 ; i < 26 ; i++ ) {
        int v = t[u][i];
        if( v == -1 ) continue ;
        dfs( v );
        if( sub[v] + 1 > sub[u] ) hv[u] = i, sub[u] = sub[v] + 1;
    }
}

void solve( int u ) {
    if( leaf[u] ) c.emplace_back('P');
    for( int i = 0 ; i < 26 ; i++ ) {
        int v = t[u][i];
        if( v == -1 || i == hv[u] ) continue ;
        c.emplace_back( i + 'a' );
        solve( v );
    }
    if( hv[u] != -1 ) {
        c.emplace_back( hv[u] + 'a' );
        solve( t[u][hv[u]] );
    }
    c.emplace_back( '-' );
}

int main()
{
    memset( t, -1, sizeof t );
    memset( hv, -1, sizeof hv );
    scanf("%d",&n);
    for( int i = 1 ; i <= n ; i++ ) {
        char s[30];
        scanf(" %s",s+1);
        int cptr = 0;
        for( int j = 1 ; j <= strlen( s+1 ) ; j++ ) {
            int &pos = t[cptr][s[j]-'a'];
            if( pos == -1 ) pos = ++ptr;
            cptr = pos;
        }
        leaf[cptr] = true;
    }
    dfs( 0 );
    solve( 0 );
    while( !c.empty() && c.back() == '-' ) c.pop_back();
    printf("%d\n",c.size());
    for( int i = 0 ; i < ( int )c.size() ; i++ ) printf("%c\n",c[i]);
    return 0;
}

Compilation message

printer.cpp: In function 'int main()':
printer.cpp:43:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for( int j = 1 ; j <= strlen( s+1 ) ; j++ ) {
                          ~~^~~~~~~~~~~~~~~~
printer.cpp:53:27: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<char>::size_type {aka long unsigned int}' [-Wformat=]
     printf("%d\n",c.size());
                   ~~~~~~~~^
printer.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&n);
     ~~~~~^~~~~~~~~
printer.cpp:41:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf(" %s",s+1);
         ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 37 ms 61048 KB Output is correct
2 Correct 37 ms 60924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 61008 KB Output is correct
2 Correct 37 ms 61048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 61048 KB Output is correct
2 Correct 37 ms 61048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 61048 KB Output is correct
2 Correct 38 ms 61048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 61048 KB Output is correct
2 Correct 38 ms 61048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 61048 KB Output is correct
2 Correct 39 ms 61176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 61432 KB Output is correct
2 Correct 51 ms 61816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 61940 KB Output is correct
2 Correct 44 ms 61432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 63216 KB Output is correct
2 Correct 138 ms 65644 KB Output is correct
3 Correct 91 ms 63600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 62860 KB Output is correct
2 Correct 152 ms 66412 KB Output is correct
3 Correct 102 ms 63984 KB Output is correct
4 Correct 137 ms 66156 KB Output is correct