답안 #685626

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
685626 2023-01-24T17:06:26 Z d4xn Type Printer (IOI08_printer) C++17
100 / 100
125 ms 57880 KB
#pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
using namespace std;

struct node {
  map<int, node*> adj;
  bool fin = 0;
};

int n;
string sMx;
vector<char> ans;

void add(node* root, string s, int idx) {
  if (idx == s.size()) {
    root->fin = 1;
    return;
  }

  int x = s[idx] - 'a';
  auto it = root->adj.find(x);
  if (it == root->adj.end()) {
    root->adj[x] = new node;
    it = root->adj.find(x);
  }
  add(it->second, s, idx+1);
}

void dfs(node* root, int idx, bool isMx) {
  if (root->fin) {
    ans.push_back('P');
  }

  int mx = -1;
  for (auto& [i, v] : root->adj) {
    char x = 'a' + i;
    if (isMx && sMx[idx] == x) {
      mx = i;
      continue;
    }
    ans.push_back(x);
    dfs(v, idx+1, 0);
    ans.push_back('-');
  }
  if (mx != -1) {
    auto it = root->adj.find(mx);
    char x = 'a' + mx;
    ans.push_back(x);
    dfs(it->second, idx+1, 1);
    ans.push_back('-');
  }
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  cin >> n;
  node* root = new node;
  int mx = 0;
  for (int i  = 0; i < n; i++) {
    string s;
    cin >> s;
    if (s.size() > mx) {
      mx = s.size();
      sMx = s;
    }
    add(root, s, 0);
  }
  dfs(root, 0, 1);

  int r = ans.size()-1;
  while (ans[r] == '-') r--;

  cout << r+1 << "\n";
  for (int i = 0; i <= r; i++) {
    assert(('a' <= ans[i] && ans[i] <= 'z') || ans[i] == 'P' || ans[i] == '-');
    cout << ans[i] << "\n";
  }
}

Compilation message

printer.cpp: In function 'void add(node*, std::string, int)':
printer.cpp:15:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |   if (idx == s.size()) {
      |       ~~~~^~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:63:18: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   63 |     if (s.size() > mx) {
      |         ~~~~~~~~~^~~~
# 결과 실행 시간 메모리 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 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1108 KB Output is correct
2 Correct 3 ms 1364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3540 KB Output is correct
2 Correct 15 ms 7424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 8660 KB Output is correct
2 Correct 9 ms 2004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 21344 KB Output is correct
2 Correct 105 ms 48704 KB Output is correct
3 Correct 65 ms 25156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 16752 KB Output is correct
2 Correct 125 ms 57880 KB Output is correct
3 Correct 76 ms 28504 KB Output is correct
4 Correct 103 ms 54684 KB Output is correct