Submission #688607

# Submission time Handle Problem Language Result Execution time Memory
688607 2023-01-27T20:36:27 Z danikoynov Type Printer (IOI08_printer) C++14
100 / 100
153 ms 106376 KB
/**
 ____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|

**/

#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int alphabet = 26;

struct trie
{
    trie *nxt[alphabet];
    int max_depth, depth, word_cnt, front_move;

    trie(int _depth = 0)
    {
        for (int i = 0; i < alphabet; i ++)
            nxt[i] = NULL;
        word_cnt = 0, depth = _depth;
        max_depth = depth;
        front_move = -1;
    }
};

void add(trie *root, string &word, int pos)
{

    if (pos == word.size())
    {
        root -> word_cnt ++;
        return;
    }

    int ch = word[pos] - 'a';
    if (root -> nxt[ch] == NULL)
        root -> nxt[ch] = new trie(root -> depth + 1);

    add(root -> nxt[ch], word, pos + 1);
    if (root -> nxt[ch] -> max_depth > root -> max_depth)
    {
        root -> front_move = ch;
        root -> max_depth = root -> nxt[ch] -> max_depth;
    }

}

vector < char > path;
void print(trie *root, bool save)
{
    ///cout << root -> depth
    while(root -> word_cnt > 0)
    {
        path.push_back('P');
        root -> word_cnt --;
    }

    for (int i = 0; i < alphabet; i ++)
    {
        if (root -> nxt[i] == NULL)
            continue;
        if (i != root -> front_move)
        {
            path.push_back((char)(i + 'a'));
            print(root -> nxt[i], false);
            path.push_back('-');
        }
    }

    if (root -> front_move != -1)
    {
            path.push_back((char)(root -> front_move + 'a'));
            print(root -> nxt[root -> front_move], save);
            if (!save)
            path.push_back('-');
    }
}

int n;
trie *tree = new trie(0);
void solve()
{
    string word;
    cin >> n;
    for (int i = 1; i <= n; i ++)
    {
        cin >> word;
        add(tree, word, 0);
    }

    print(tree, true);

    cout << path.size() << endl;
    for (char c : path)
        cout << c << endl;

}

int main()
{
    solve();
    return 0;
}

Compilation message

printer.cpp: In function 'void add(trie*, std::string&, int)':
printer.cpp:42:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     if (pos == word.size())
      |         ~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1876 KB Output is correct
2 Correct 4 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 6356 KB Output is correct
2 Correct 22 ms 13388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 15696 KB Output is correct
2 Correct 10 ms 3540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 39148 KB Output is correct
2 Correct 123 ms 89184 KB Output is correct
3 Correct 69 ms 45764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 30540 KB Output is correct
2 Correct 153 ms 106376 KB Output is correct
3 Correct 81 ms 52332 KB Output is correct
4 Correct 134 ms 100436 KB Output is correct