Submission #652565

# Submission time Handle Problem Language Result Execution time Memory
652565 2022-10-23T06:55:56 Z alvinpiter Type Printer (IOI08_printer) C++17
80 / 100
634 ms 262144 KB
/*
* Insert each word into a trie
* "Add" operation is the same as going an edge downward, while "Remove" operation is the same as
  going an edge upward. If there is a requirement that the printer must be empty after all operations
  are performed, then the answer is 2 * numberOfEdgesInTheTrie + numberOfWords.
* However, we are allowed to leave some letters in the printer, so it is best if we leave the longest word in
  the printer.
*/

#include<bits/stdc++.h>
using namespace std;

struct TrieNode {
  int maxDepth;
  bool isEndOfAWord;
  map<int, TrieNode*> children;

  TrieNode(): maxDepth(0), isEndOfAWord(false) {}
};

vector<char> result;

void insert(TrieNode* root, string s) {
  TrieNode* currentNode = root;
  for (int i = 0; i < s.length(); i++) {
    int charIndex = (s[i] - 'a');

    currentNode->maxDepth = max(currentNode->maxDepth, (int) s.length() - i);
    if (currentNode->children.find(charIndex) == currentNode->children.end()) {
      currentNode->children[charIndex] = new TrieNode();
    }

    currentNode = currentNode->children[charIndex];
  }

  currentNode->isEndOfAWord = true;
}

void traverse(TrieNode* root) {
  if (root->isEndOfAWord) {
    result.push_back('P');
  }

  int deepestChildIdx = -1;
  for (int i = 0; i < 26; i++) {
    if (root->children[i] != NULL && (deepestChildIdx == -1 || root->children[deepestChildIdx]->maxDepth < root->children[i]->maxDepth)) {
      deepestChildIdx = i;
    }
  }

  for (int i = 0; i < 26; i++) {
    if (root->children[i] != NULL && i != deepestChildIdx) {
      result.push_back((char) (i + 'a'));
      traverse(root->children[i]);
    }
  }

  if (deepestChildIdx != -1) {
    result.push_back((char) (deepestChildIdx + 'a'));
    traverse(root->children[deepestChildIdx]);
  }

  result.push_back('-');
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  int n;
  cin >> n;

  TrieNode* trieRoot = new TrieNode();
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;

    insert(trieRoot, s);
  }

  traverse(trieRoot);

  while (result.back() == '-') {
    result.pop_back();
  }

  cout << result.size() << endl;
  for (auto r: result) {
    cout << r << endl;
  }
}

Compilation message

printer.cpp: In function 'void insert(TrieNode*, std::string)':
printer.cpp:25:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |   for (int i = 0; i < s.length(); i++) {
      |                   ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 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 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1236 KB Output is correct
2 Correct 18 ms 6488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 11628 KB Output is correct
2 Correct 43 ms 14780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 42316 KB Output is correct
2 Correct 264 ms 91200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 323 ms 107636 KB Output is correct
2 Correct 82 ms 21836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 396 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 634 ms 211392 KB Output is correct
2 Runtime error 359 ms 262144 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -