답안 #982736

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
982736 2024-05-14T16:52:19 Z vjudge1 Type Printer (IOI08_printer) C++17
100 / 100
110 ms 101192 KB
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iterator>
#include <utility>
using namespace std;

typedef long long int ll;

int n;
vector<char> out;

class Node {
    public:
    Node* child[26];
    bool isEnd;
    bool isLast;
    Node() {
        for (int i = 0; i < 26; i++) {
            this->child[i] = NULL;
        }
        this->isEnd = false;
        this->isLast = false;
    }
};

void insertTrie(Node* root, string& str, bool isLongest) {
    int i = 0;
    Node* curNode = root;
    while (i < str.length()) {
        int childIdx = (int) str[i] - (int)'a';
        if (curNode->child[childIdx] == NULL) {
            curNode->child[childIdx] = new Node();
        }
        curNode = curNode->child[childIdx];
        curNode->isLast = isLongest;
        i++;
    }
    
    curNode->isEnd = true;
    curNode->isLast = isLongest;
}

void traverseTrie(Node *cur) {
    if (cur->isEnd) {
        out.push_back('P');
    }
    
    int last = -1;
    for (int i = 0; i < 26; i++) {
        if (cur->child[i] != NULL) {
            if (cur->child[i]->isLast) {
                last = i;
                continue;
            }
            char chr = (char) (i + (int) 'a');
            out.push_back(chr);
            traverseTrie(cur->child[i]);
            out.push_back('-');
        }
    }
    
    if (last != -1) {
        char chr = (char) (last + (int) 'a');
        out.push_back(chr);
        traverseTrie(cur->child[last]);
    }
}

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    
    cin >> n;
    vector<string> arr;
    int longest;
    int maxLen = 0;
    Node* root = new Node();
    for (int i = 0; i < n; i++) {
        arr.push_back("");
        cin >> arr[i];
        insertTrie(root, arr[i], false);
        if (maxLen < arr[i].length()) {
            maxLen = (int) arr[i].length();
            longest = i;
        }
    }
    insertTrie(root, arr[longest], true);
    traverseTrie(root);
    cout << out.size() << '\n';
    for (int i = 0; i < out.size(); i++) {
        cout << out[i] << '\n';
    }
    return 0;
}

Compilation message

printer.cpp: In function 'void insertTrie(Node*, std::string&, bool)':
printer.cpp:37:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |     while (i < str.length()) {
      |            ~~^~~~~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:90:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |         if (maxLen < arr[i].length()) {
      |             ~~~~~~~^~~~~~~~~~~~~~~~~
printer.cpp:98:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |     for (int i = 0; i < out.size(); i++) {
      |                     ~~^~~~~~~~~~~~
printer.cpp:95:33: warning: 'longest' may be used uninitialized in this function [-Wmaybe-uninitialized]
   95 |     insertTrie(root, arr[longest], true);
      |                                 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1884 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 6140 KB Output is correct
2 Correct 13 ms 12760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 15060 KB Output is correct
2 Correct 7 ms 3796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 37660 KB Output is correct
2 Correct 94 ms 85060 KB Output is correct
3 Correct 53 ms 45000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 29640 KB Output is correct
2 Correct 110 ms 101192 KB Output is correct
3 Correct 59 ms 50884 KB Output is correct
4 Correct 94 ms 95620 KB Output is correct