# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
982734 | vjudge1 | Type Printer (IOI08_printer) | C++17 | 1077 ms | 101224 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 >> 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() << endl;
for (int i = 0; i < out.size(); i++) {
cout << out[i] << endl;
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |