# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
639885 | PoonYaPat | Type Printer (IOI08_printer) | C++14 | 128 ms | 99588 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int n,mmax;
bool havemax=false;
const int sz=26;
vector<char> ans;
struct Trienode {
struct Trienode *children[26];
int level;
char x;
bool Endword,longest;
};
struct Trienode *getnode(char x, int l) {
struct Trienode *pnode = new Trienode;
for (int i=0; i<sz; ++i) pnode->children[i]=NULL;
pnode->level=l;
pnode->x=x;
pnode->Endword=false;
pnode->longest=false;
return pnode;
};
void insert(struct Trienode *root, string key) {
struct Trienode *pnode = root;
for (int i=0; i<key.size(); ++i) {
int idx=key[i]-'a';
if (!pnode->children[idx]) pnode->children[idx]=getnode(key[i],pnode->level+1);
pnode=pnode->children[idx];
}
pnode->Endword=true;
}
bool fm(struct Trienode *root) { //if it has the longest path, it will return true;
if (root->level==mmax && !havemax) {
havemax=true;
root->longest=true;
return true;
}
for (int i=0; i<sz; ++i) if (root->children[i] && fm(root->children[i])) {
root->longest=true;
return true;
}
return false;
}
void dfs(struct Trienode *root) {
if (root->x != '-') ans.push_back(root->x);
if (root->Endword) ans.push_back('P');
struct Trienode *pnode;
bool haveP=false;
for (int i=0; i<sz; ++i) if (root->children[i]) {
if (!root->children[i]->longest) dfs(root->children[i]);
else pnode=root->children[i], haveP=true;
}
if (haveP) dfs(pnode);
if (!root->longest) ans.push_back('-');
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
struct Trienode *root = getnode('-',0);
while (n--) {
string s; cin>>s;
mmax=max(mmax,(int)(s.size()));
insert(root,s);
}
fm(root);
dfs(root);
cout<<ans.size()<<"\n";
for (auto s : ans) cout<<s<<"\n";
}
컴파일 시 표준 에러 (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... |