Submission #880506

# Submission time Handle Problem Language Result Execution time Memory
880506 2023-11-29T14:49:07 Z StefanL2005 Type Printer (IOI08_printer) C++14
100 / 100
88 ms 35920 KB
#include <bits/stdc++.h>
using namespace std;
#define last Nod->fii.size() - 1
int nr = 0;

struct Trie {
    int down, word;
    Trie *path;
    vector<pair<Trie*, char>> fii;

    Trie() {
        down = word = 0; path = 0;
    }
};

Trie *Root = new Trie;

void insert(Trie *Nod, char *p)
{
    if (*p == '\0')
    {
        Nod->word++;
        return;
    }

    for (int i = 0; i < Nod->fii.size(); i++)
        if (*p == Nod->fii[i].second)
        {
            insert(Nod->fii[i].first, p + 1);

            if (Nod->fii[i].first->down + 1 > Nod->down)
            {
                Nod->path = Nod->fii[i].first;
                Nod->down = Nod->path->down + 1;
            }
            return;
        }

    nr++;
    Nod->fii.push_back(make_pair(new Trie, *p));
    insert(Nod->fii[last].first, p + 1);

    if (Nod->fii[last].first->down + 1 > Nod->down)
    {
        Nod->down = Nod->fii[last].first-> down + 1;
        Nod->path = Nod->fii[last].first;
    }
}

void easy_search(Trie *Nod)
{
    while (Nod->word > 0)
    {
        cout<< "P\n";
        Nod->word--;
    }

    for (int i = 0; i < Nod->fii.size(); i++)
    {
        cout<< Nod->fii[i].second << "\n";
        easy_search(Nod->fii[i].first);
        cout<< "-\n";
    }
}

int show_longest(Trie *Nod, int k)
{
    if (Nod->path == NULL)
        return k;
    return show_longest(Nod->path, k + 1);
}

void complex_search(Trie *Nod)
{
    while (Nod->word > 0)
    {
        cout<< "P\n";
        Nod->word--;
    }

    if (Nod->path == NULL)
        return;
    
    char let;
    for (int i = 0; i < Nod->fii.size(); i++)
    {
        if (Nod->fii[i].first == Nod->path)
        {
            let = Nod->fii[i].second;
            continue;
        }

        cout<< Nod->fii[i].second << "\n";
        easy_search(Nod->fii[i].first);
        cout<< "-\n";
    }

    cout<< let << "\n";
    complex_search(Nod->path);
}
int main()
{
    int N;
    cin>> N;
    
    for (int i = 0; i < N; i++)
    {
        string w; cin>> w;
        insert(Root, &w[0]);
    }

    cout<< (nr - Root->down) * 2 + Root->down + N << "\n";
    
    complex_search(Root);
    return 0;
}

Compilation message

printer.cpp: In function 'void insert(Trie*, char*)':
printer.cpp:26:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<Trie*, char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     for (int i = 0; i < Nod->fii.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~
printer.cpp: In function 'void easy_search(Trie*)':
printer.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<Trie*, char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |     for (int i = 0; i < Nod->fii.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~
printer.cpp: In function 'void complex_search(Trie*)':
printer.cpp:85:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<Trie*, char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |     for (int i = 0; i < Nod->fii.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~
printer.cpp:84:10: warning: 'let' may be used uninitialized in this function [-Wmaybe-uninitialized]
   84 |     char let;
      |          ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 856 KB Output is correct
2 Correct 2 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2396 KB Output is correct
2 Correct 11 ms 4688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 5336 KB Output is correct
2 Correct 7 ms 1368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 13396 KB Output is correct
2 Correct 70 ms 30260 KB Output is correct
3 Correct 40 ms 15444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 10332 KB Output is correct
2 Correct 88 ms 35920 KB Output is correct
3 Correct 45 ms 17744 KB Output is correct
4 Correct 70 ms 34128 KB Output is correct