이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//https://oj.uz/problem/view/IOI08_printer
#include <bits/stdc++.h>
using namespace std;
struct Node {
bool leaf;
int cnt_of_strings;
unordered_map<int, Node*> alphabet;
Node() {
leaf = false;
cnt_of_strings = 0;
for(int i = 0; i < 26; i++) {
alphabet[i] = NULL;
}
}
} *root;
void trie_insert(string s) {
Node *curr = root;
for(int i = 0; i < s.size(); i++) {
if(curr->alphabet[s[i] - 'a'] == NULL) {
curr->alphabet[s[i] - 'a'] = new Node();
}
curr = curr->alphabet[s[i] - 'a'];
curr->cnt_of_strings++;
}
curr->leaf = true;
}
string max_length = "";
void trie_Search(Node *curr, string s) {
for(int i = 0; i < 26; i++) {
if(curr->alphabet[i] != NULL) {
s += (char)(i + 'a');
trie_Search(curr->alphabet[i], s);
s.pop_back();
}
}
if(max_length.size() < s.size())
max_length = s;
}
string ans = "";
void FindAnswer(Node *curr, bool edge, int idx) {
if(curr->leaf == true) {
ans += "P";
return;
}
//cout << idx << endl;
//cout << "Inside the loop" << endl;
for(int i = 0; i < 26; i++) {
//cout << i << " ";
if(edge && i == max_length[idx] - 'a')
continue;
if(curr->alphabet[i] != NULL) {
ans += (i + 'a');
FindAnswer(curr->alphabet[i], false, idx + 1);
ans += "-";
}
}
//cout << "Have come out of the " << idx << " loop" << endl;
if(edge) {
int x = max_length[idx] - 'a';
ans += max_length[idx];
FindAnswer(curr->alphabet[x], true, idx + 1);
}
}
void Solve() {
int n;
cin >> n;
root = new Node();
for(int i = 1; i <= n; i++) {
string s;
cin >> s;
trie_insert(s);
}
string s = "";
trie_Search(root, s);
FindAnswer(root, true, 0);
cout << ans.size() << endl;
for(int i = 0; i < ans.size(); i++) {
cout << (char)(ans[i]) << endl;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
Solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
printer.cpp: In function 'void trie_insert(std::__cxx11::string)':
printer.cpp:21:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < s.size(); i++) {
~~^~~~~~~~~~
printer.cpp: In function 'void Solve()':
printer.cpp:85:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < ans.size(); i++) {
~~^~~~~~~~~~~~
# | 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... |