# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
555974 | iDoItSaNdWiTcH | Type Printer (IOI08_printer) | C++17 | 1080 ms | 99504 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = 1e9 + 5;
//////////////////////////////////////////////////////////////////////////////////////////
string cur = "";
struct Trie {
int totalsz = 0;
bool isend = 0;
struct Trie* children[26] = {};
};
void tinsert(struct Trie* root, string s) {
//find the bug in tinsert
Trie* cnt = root;
for (int i = 0; i < s.length(); i++) {
if (cnt->children[s[i] - 'a'] == nullptr) {
//create new tree
cnt->children[s[i] - 'a'] = new Trie();
}
//calculate how much is left]
int sz = cnt->children[s[i] - 'a']->totalsz;
if (s.length() - i - 1 > sz) {
(cnt->children[s[i] - 'a']->totalsz) = s.length() - i - 1;
}
cnt = cnt->children[s[i] - 'a'];
}
cnt->isend = 1;
}
void dfs(Trie* root) {
//theres a string that ends here
if (root->isend) {
cur += "P";
}
//first find the one with longest string
int longestsz = 0;
int c = 0;
for (int i = 0; i < 26; i++) {
if (root->children[i] != nullptr) {
if (root->children[i]->totalsz > longestsz) {
longestsz = root->children[i]->totalsz;
c = i;
}
}
}
//do the others
for (int i = 0;i < 26; i++) {
if (root->children[i] != nullptr && i != c) {
cur += char('a' + i);
dfs(root->children[i]);
cur += "-";
}
}
//last do back the longest string
if (root->children[c] != nullptr) {
cur += char('a' + c);
dfs(root->children[c]);
cur += "-";
}
}
void solve(){
int n; cin >> n;
struct Trie* root = new Trie();
while (n--) {
string s; cin >> s;
tinsert(root, s);
}
dfs(root);
int p = 0;
for (int i = cur.length() - 1; i > -1; i--) {
if (cur[i] == '-') p++;
else break;
}
int maxx = cur.length() - p;
cout << maxx << endl;
for (int i = 0; i < maxx; i++) {
cout << cur[i] << endl;
}
}
//////////////////////////////////////////////////////////////////////////////////////////
int main(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
//cin >> t;
while (t--){
solve();
}
}
/*
find a method to dfs the longest strings at last
*/
컴파일 시 표준 에러 (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... |