답안 #685619

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

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

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 << ans.size() << "\n";
  for (int i = 0; i <= r; 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 Incorrect 0 ms 212 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 1108 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 3540 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 18 ms 8700 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 54 ms 21416 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 40 ms 16708 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -