# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
982678 | 2024-05-14T15:55:25 Z | vjudge1 | Type Printer (IOI08_printer) | C++17 | 406 ms | 37848 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); } if (isLongest) { curNode->child[childIdx]->isLast = true; } curNode = curNode->child[childIdx]; idx++; } } void traverseTrie(Node *root) { Node* cur = root; 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(root->child[i]); out.push_back('-'); } } if (last != -1) { char chr = (char) (last + (int) 'a'); out.push_back(chr); traverseTrie(root->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]; if (maxLen < arr[i].length()) { maxLen = (int) arr[i].length(); longest = i; } } for (int i = 0; i < arr.size(); i++) { insertTrie(root, arr[i], i == longest); } traverseTrie(root); cout << out.size() << endl; for (int i = 0; i < out.size(); i++) { cout << out[i] << endl; } return 0; }
Compilation message
# | 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 | 348 KB | Output is correct |
2 | Correct | 1 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 | 504 KB | didn't print every word |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 17 ms | 1896 KB | didn't print every word |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 64 ms | 6228 KB | didn't print every word |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 159 ms | 15292 KB | didn't print every word |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 406 ms | 37848 KB | didn't print every word |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 321 ms | 30076 KB | didn't print every word |
2 | Halted | 0 ms | 0 KB | - |