답안 #546885

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
546885 2022-04-08T18:33:02 Z ussef_abdallah Type Printer (IOI08_printer) C++14
100 / 100
149 ms 101016 KB
#include <bits/stdc++.h>

using namespace std;

const int ALPHABET_SIZE = 26;

class TrieNode {
public:
    TrieNode* children[ALPHABET_SIZE];
    bool isLeaf;

    TrieNode() {
        for (int i = 0; i < ALPHABET_SIZE; i++) {
            children[i] = nullptr;
        }
        isLeaf = false;
    }

};

class Trie {
private:
    TrieNode* root;
public:
    Trie() {
        root = new TrieNode();
    }

    void insert(string &word) {
        TrieNode* cur = root;
        for (char &c : word) {
            if (cur->children[c - 'a'] == nullptr)
                cur->children[c - 'a'] = new TrieNode();
            cur = cur->children[c - 'a'];
        }
        cur->isLeaf = true;
    }

    void traverse(string &longest, vector<char> &ans) {
        dfs(root, longest, ans, 0);
    }

    void dfs(TrieNode* node, string &longest, vector<char> &ans, int pos) {
        if (node == nullptr)
            return;
        if (node->isLeaf)
            ans.push_back('P');
        for (int i = 0; i < ALPHABET_SIZE; i++) {
            if (node->children[i] != nullptr && (pos == -1 || i + 'a' != longest[pos])) {
                ans.push_back('a' + i);
                dfs(node->children[i], longest, ans, -1);
                ans.push_back('-');
            }
        }
        if (pos != -1 && node->children[longest[pos] - 'a']) {
            ans.push_back(longest[pos]);
            dfs(node->children[longest[pos] - 'a'], longest,
                ans, pos + 1);
        }
    }
};

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    vector<string> a(n);
    string longest = "";
    Trie* trie = new Trie();
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        if (a[i].length() > longest.length())
            longest = a[i];
        trie->insert(a[i]);
    }
    vector<char> ans;
    trie->traverse(longest, ans);
    cout << ans.size() << "\n";
    for (auto &c : ans) {
        cout << c << "\n";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 3 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1876 KB Output is correct
2 Correct 4 ms 2280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6100 KB Output is correct
2 Correct 17 ms 12756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 14980 KB Output is correct
2 Correct 7 ms 3796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 37156 KB Output is correct
2 Correct 111 ms 84916 KB Output is correct
3 Correct 59 ms 44452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 29284 KB Output is correct
2 Correct 149 ms 101016 KB Output is correct
3 Correct 71 ms 50476 KB Output is correct
4 Correct 117 ms 95688 KB Output is correct