답안 #589461

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
589461 2022-07-04T17:48:20 Z Mamedov Type Printer (IOI08_printer) C++17
100 / 100
154 ms 120128 KB
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define pii pair<int, int>
#define piii pair<pii, int>
#define vi vector<int>
#define vvi vector<vi>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define f first
#define s second
#define oo 1000000001
#define eb emplace_back
#define pb push_back
#define mpr make_pair
#define size(v) (int)v.size()
#define ln '\n'
#define ull unsigned long long
#define ll long long
#define all(v) v.begin(), v.end()

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

struct Trie {
  bool isEnd;
  int depth;
  vector<Trie*>link;
  Trie(int dep): isEnd(false), depth(dep), link(vector<Trie*>(26, nullptr)) {}
  void add(string &s) {
    Trie *node = this;
    int len = size(s);
    for (char c : s) {
      if (node->link[c - 'a']) {
        node = node->link[c - 'a'];
        node->depth = max(node->depth, len);
      } else {
        node = node->link[c - 'a'] = new Trie(len);
      }
    }
    node->isEnd = true;
  }
  string traverse(Trie *node) {
    string res = "";
    if (node->isEnd) res += 'P';
    int imaxDep = -1;
    for (int i = 0; i < 26; ++i) {
      if (node->link[i] && (imaxDep == -1 || node->link[i]->depth > node->link[imaxDep]->depth)) {
        imaxDep = i;
      }
    }
    if (imaxDep != -1) {
      for (int i = 0; i < 26; ++i) {
        if (i != imaxDep && node->link[i]) {
          res += ('a' + i);
          res += traverse(node->link[i]);
          res += '-';
        }
      }
      res += ('a' + imaxDep);
      res += traverse(node->link[imaxDep]);
      res += '-';
    }
    return res;
  }
};

void solve() {
  int n;
  cin >> n;
  string s;
  Trie *root = new Trie(0);
  for (int i = 0; i < n; ++i) {
    cin >> s;
    root->add(s);
  }
  string res = root->traverse(root);
  while (res.back() == '-') res.pop_back();
  cout << size(res) << ln;
  for (char c : res) {
    cout << c << ln;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 2 ms 1236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2132 KB Output is correct
2 Correct 4 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7124 KB Output is correct
2 Correct 27 ms 15044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 17692 KB Output is correct
2 Correct 8 ms 3924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 43944 KB Output is correct
2 Correct 134 ms 100540 KB Output is correct
3 Correct 73 ms 51724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 34292 KB Output is correct
2 Correct 154 ms 120128 KB Output is correct
3 Correct 82 ms 59120 KB Output is correct
4 Correct 140 ms 113452 KB Output is correct