Submission #648459

# Submission time Handle Problem Language Result Execution time Memory
648459 2022-10-06T15:30:24 Z Elias_Obeid Type Printer (IOI08_printer) C++17
60 / 100
11 ms 6224 KB
#include <bits/stdc++.h>
using namespace std;

const int LEN = 25'000;
const int ALPHABET = 26;

struct TrieNode
{
    bool is_leaf;
    int longest_word, longest_letter;
    array<int, ALPHABET> next_nodes;
    TrieNode()
    {
        is_leaf = false;
        longest_word = -1;
        longest_letter = -1;
        next_nodes.fill(-1);
    }
};

int current_trie_node = 0;
array<TrieNode, LEN + 1> trie;

int fetchNode()
{
    trie[current_trie_node] = TrieNode();
    return current_trie_node++;
}

const int ROOT = fetchNode();

void addWord(const string &word)
{
    int current_node = 0;
    for (int i = 0; i < (int)word.size(); ++i)
    {
        int letter = (word[i] - 'a');
        if (trie[current_node].next_nodes[letter] == -1)
        {
            trie[current_node].next_nodes[letter] = fetchNode();
        }

        if (trie[current_node].longest_word < (int)word.size() - 1 - i)
        {
            trie[current_node].longest_word = (int)word.size() - 1 - i;
            trie[current_node].longest_letter = letter;
        }
        current_node = trie[current_node].next_nodes[letter];
    }
    trie[current_node].is_leaf = true;
}

vector<char> answer;

void depthFirstSearch(int node)
{
    if (trie[node].is_leaf)
    {
        answer.emplace_back('P');
    }

    for (int letter = 0; letter < ALPHABET; ++letter)
    {
        if (trie[node].next_nodes[letter] != -1 && letter != trie[node].longest_letter)
        {
            answer.emplace_back(char(letter + 'a'));
            depthFirstSearch(trie[node].next_nodes[letter]);
        }
    }

    if (trie[node].longest_letter != -1)
    {
        answer.emplace_back(char(trie[node].longest_letter + 'a'));
        depthFirstSearch(trie[node].next_nodes[trie[node].longest_letter]);
    }
    answer.emplace_back('-');
}

int main()
{
    int N;
    cin >> N;

    for (int i = 0; i < N; ++i)
    {
        string S;
        cin >> S;
        addWord(S);
    }
    depthFirstSearch(ROOT);

    while (!answer.empty() && answer.back() == '-')
    {
        answer.pop_back();
    }

    cout << (int)answer.size() << "\n";
    for (char letter : answer)
    {
        cout << letter << "\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3136 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 3 ms 3156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3156 KB Output is correct
2 Correct 4 ms 3156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 3284 KB Output is correct
2 Runtime error 11 ms 6224 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 6 ms 6100 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 7 ms 6100 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 7 ms 6100 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -