Submission #982726

# Submission time Handle Problem Language Result Execution time Memory
982726 2024-05-14T16:42:29 Z vjudge1 Type Printer (IOI08_printer) C++17
30 / 100
432 ms 37576 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(bool isEnd, bool isLast) {
        for (int i = 0; i < 26; i++) {
            this->child[i] = NULL;
        }
        this->isEnd = isEnd;
        this->isLast = isLast;
    }
};

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

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 >> n;
    vector<string> arr;
    int longest;
    int maxLen = 0;
    Node* root = new Node(false, true);
    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() << endl;
    for (int i = 0; i < out.size(); i++) {
        cout << out[i] << endl;
    }
    return 0;
}

Compilation message

printer.cpp: In function 'void insertTrie(Node*, std::string&, bool)':
printer.cpp:37:16: 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 (idx < str.length()) {
      |            ~~~~^~~~~~~~~~~~~~
printer.cpp:41:53: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |             curNode->child[childIdx] = new Node(idx == str.length() - 1, false);
      |                                                 ~~~~^~~~~~~~~~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:85:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |         if (maxLen < arr[i].length()) {
      |             ~~~~~~~^~~~~~~~~~~~~~~~~
printer.cpp:93:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |     for (int i = 0; i < out.size(); i++) {
      |                     ~~^~~~~~~~~~~~
printer.cpp:90:33: warning: 'longest' may be used uninitialized in this function [-Wmaybe-uninitialized]
   90 |     insertTrie(root, arr[longest], true);
      |                                 ^
# 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 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 348 KB didn't print every word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 348 KB didn't print every word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 1880 KB didn't print every word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 65 ms 6136 KB didn't print every word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 163 ms 15084 KB didn't print every word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 432 ms 37576 KB didn't print every word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 329 ms 29636 KB didn't print every word
2 Halted 0 ms 0 KB -