답안 #57724

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
57724 2018-07-15T22:35:36 Z vex Type Printer (IOI08_printer) C++14
20 / 100
772 ms 76624 KB
#include <bits/stdc++.h>
#define MAXN 25005
#define ALPH 26
using namespace std;

struct Node{
    int dp[2];
    bool kraj;
    struct Node* deca[ALPH];
}* head;

void init()
{
    head=new Node();
    head->kraj=false;
}
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=curr->deca[zn];
    }
    curr->kraj=true;
}


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

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

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

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

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

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

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

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

        cout<<char('a'+ind)<<endl;
        ispisi(v->deca[ind],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(head);
    cout<<head->dp[1]<<endl;

    ispisi(head,1);
    return 0;
}

Compilation message

printer.cpp: In function 'void ubaci(std::__cxx11::string)':
printer.cpp:20:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<s.size();i++)
                 ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 432 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 672 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 716 KB Output is correct
2 Runtime error 3 ms 788 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 1084 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 40 ms 4052 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 116 ms 12612 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 298 ms 30880 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 772 ms 76624 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 604 ms 76624 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -