Submission #1324200

#TimeUsernameProblemLanguageResultExecution timeMemory
1324200SSKMFType Printer (IOI08_printer)C++20
50 / 100
82 ms44936 KiB
#include <bits/stdc++.h>
using namespace std;

struct Nod {
    int cantitate = 0 , urmatorul[26] = { };
    pair <int , int> maxim = { };
    bool necesar = false;
} arbore[500002];

int cantitate = 1;

inline void Parcurgere (const int nod)
{
    for (int indice = 0 ; indice < 26 ; indice++) {
        if (arbore[nod].urmatorul[indice])
        {
            Parcurgere(arbore[nod].urmatorul[indice]);
            arbore[nod].cantitate += 1 + arbore[arbore[nod].urmatorul[indice]].cantitate;
            arbore[nod].maxim = max(arbore[nod].maxim , {arbore[arbore[nod].urmatorul[indice]].maxim.first + 1 , indice});
        }
    }
}

inline void Afisare (const int nod , const bool __main)
{
    if (arbore[nod].necesar)
        { cout.put('P').put('\n'); }

    for (int indice = 0 ; indice < 26 ; indice++) {
        if (arbore[nod].urmatorul[indice] && (!__main || indice != arbore[nod].maxim.second))
        {
            cout.put('a' + indice).put('\n');
            Afisare(arbore[nod].urmatorul[indice] , false);
            cout.put('-').put('\n');
        }
    }

    if (__main && arbore[nod].maxim.second)
    {
        cout.put('a' + arbore[nod].maxim.second).put('\n');
        Afisare(arbore[nod].urmatorul[arbore[nod].maxim.second] , true);
    }
}

inline void Solve ()
{
    int necesar;
    cin >> necesar;

    for (int indice = 1 ; indice <= necesar ; indice++)
    {
        string sir;
        cin >> sir;

        int nod = 1;
        for (auto& caracter : sir) {
            if (!arbore[nod].urmatorul[caracter - 'a'])
                { arbore[nod].urmatorul[caracter - 'a'] = ++cantitate; }
            nod = arbore[nod].urmatorul[caracter - 'a'];
        }

        arbore[nod].necesar = true;
    }

    Parcurgere(1);

    cout << 2 * arbore[1].cantitate - arbore[1].maxim.first + necesar << '\n';

    Afisare(1 , true);
}

int main ()
{
    ios :: sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    int numar_teste = 1;
    // cin >> numar_teste;
    while (numar_teste--)
        { Solve(); }
    
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...