Submission #57723

# Submission time Handle Problem Language Result Execution time Memory
57723 2018-07-15T22:23:14 Z vex Type Printer (IOI08_printer) C++14
20 / 100
698 ms 77324 KB
#include <bits/stdc++.h>
#define MAXN 25005
#define ALPH 26
using namespace std;

int tme=0;
struct Node{
    int time;
    bool kraj;
    struct Node* deca[ALPH];
}* head;
Node* vreme[500005];

void init()
{
    head=new Node();
    head->kraj=false;
    head->kraj=tme;
    vreme[tme]=head;
    tme++;
}
void ubaci(string s)
{
    Node* curr=head;
    for(int i=0;i<s.size();i++)
    {
        int zn=s[i]-'a';
        if(curr->deca[zn]==NULL)
        {
            curr->deca[zn]=new Node();
            curr->deca[zn]->kraj=false;
            curr->deca[zn]->time=tme;
            vreme[tme]=curr->deca[zn];
            tme++;
        }
        curr=curr->deca[zn];
    }
    curr->kraj=true;
}

int dp[500005][2];
void resi(int v)
{
    int br=0;
    for(int i=0;i<ALPH;i++)if(vreme[v]->deca[i]!=NULL)br++;
    if(br==0)
    {
        dp[v][0]=dp[v][1]=1;
        return;
    }

    dp[v][0]=0;
    int sum=0;
    for(int i=0;i<ALPH;i++)
    {
        if(vreme[v]->deca[i]!=NULL)
        {
            int nv=vreme[v]->deca[i]->time;
            resi(nv);
            sum+=dp[nv][0];
            dp[v][0]+=2;
        }
    }
    dp[v][0]+=sum;

    dp[v][1]=dp[v][0]-1;
    for(int i=0;i<ALPH;i++)
    {
        if(vreme[v]->deca[i]!=NULL)
        {
            int nv=vreme[v]->deca[i]->time;
            if(dp[v][0]-1-dp[nv][0]+dp[nv][1]<dp[v][1])  dp[v][1]=dp[v][0]-1-dp[nv][0]+dp[nv][1];
        }
    }

    if(vreme[v]->kraj)
    {
        dp[v][0]++;
        dp[v][1]++;
    }
}

void ispisi(int v,int w)
{
    int br=0;
    for(int i=0;i<ALPH;i++)if(vreme[v]->deca[i]!=NULL)br++;
    if(br==0)
    {
        cout<<"P"<<endl;
        return;
    }

    int sum=0;
    for(int i=0;i<ALPH;i++)
    {
        if(vreme[v]->deca[i]!=NULL)
        {
            int nv=vreme[v]->deca[i]->time;
            sum+=dp[nv][0]+2;
        }
    }

    int delta=0;
    if(vreme[v]->kraj)
    {
        cout<<"P"<<endl;
        delta=1;
    }
    if(w==0)
    {
        for(int i=0;i<ALPH;i++)
        {
            if(vreme[v]->deca[i]!=NULL)
            {
                int nv=vreme[v]->deca[i]->time;
                cout<<char('a'+i)<<endl;
                ispisi(nv,0);
                cout<<"-"<<endl;
            }
        }
    }
    else{
        int ind=-1;
        for(int i=0;i<ALPH;i++)
        {
            if(vreme[v]->deca[i]!=NULL)
            {
                int nv=vreme[v]->deca[i]->time;
                if(dp[v][0]-1-dp[nv][0]+dp[nv][1]+delta==dp[v][1])  ind=i;
            }
        }

        for(int i=0;i<ALPH;i++)
        {
            if(vreme[v]->deca[i]!=NULL && i!=ind)
            {
                int nv=vreme[v]->deca[i]->time;
                cout<<char('a'+i)<<endl;
                ispisi(nv,0);
                cout<<"-"<<endl;
            }
        }

        cout<<char('a'+ind)<<endl;
        ispisi(vreme[v]->deca[ind]->time,1);
    }
}

int n;
string x;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    init();
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        ubaci(x);
    }

    resi(0);
    cout<<dp[0][1]<<endl;

    ispisi(0,1);
    return 0;
}

Compilation message

printer.cpp: In function 'void ubaci(std::__cxx11::string)':
printer.cpp:25:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<s.size();i++)
                 ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 4 ms 496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 604 KB Output is correct
2 Correct 3 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 5 ms 920 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 920 KB Output is correct
2 Runtime error 4 ms 920 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Runtime error 7 ms 1152 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 30 ms 4088 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 119 ms 12760 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 301 ms 31164 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 698 ms 77324 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 545 ms 77324 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -