답안 #746178

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
746178 2023-05-21T16:55:30 Z khulegub Type Printer (IOI08_printer) C++17
0 / 100
22 ms 21460 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 100000;


/** Node count*/
int k = 1;

int to[N][26];
char a[N];
vector<char> ans;

void dfs(int u) {
  if (u > 0) ans.push_back(a[u]);

  bool is_leaf = true;
  for (int i = 0; i < 26; i++) {
    if (to[u][i] != -1) {
      is_leaf = false;
      dfs(to[u][i]);
    }
  }

  if (is_leaf) {
    ans.push_back('P');
  }
  ans.push_back('-');
}

int main() {
  int n;
  cin >> n;

  memset(to, -1, sizeof to);

  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;

    int curr = 0;

    for (int j = 0; j < s.length(); j++) {
      int c = s[j] - 'a';
      if (to[curr][c] == -1) {
        // Create a new node.
        to[curr][c] = k;
        k++;
      }
      a[to[curr][c]] = s[j];
      
      curr = to[curr][c];
    }
  }

  dfs(0);

  vector<pair<int, string> > v;

  string last = "";

  int longest = -1;
  int p_count = 0;

  for (int i = 0; i < ans.size(); i++) {
    if (ans[i] == '-') {
      if (last != "") {
        v.push_back({1, last});
        last = "";
      } else {
        v.back().first++;
        longest = max(longest, v.back().first);
      }
    } else {
      if (ans[i] == 'P') {
        p_count++;
      }
      last += (ans[i]);
    }
  }

  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());

  cout << ans.size() - longest - p_count << endl;
  for (int i = 0; i < v.size(); i++) {
    for (int j = 0; j < v[i].second.length(); j++) {
      cout << v[i].second[j] << '\n';
    }
    
    if (i < v.size() -1) {
      for (int j = 0; j < v[i].first - 1; j++) {
        cout << '-' << '\n';
      }
    }
  }
}

Compilation message

printer.cpp: In function 'int main()':
printer.cpp:44:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |     for (int j = 0; j < s.length(); j++) {
      |                     ~~^~~~~~~~~~~~
printer.cpp:66:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |   for (int i = 0; i < ans.size(); i++) {
      |                   ~~^~~~~~~~~~~~
printer.cpp:87:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::__cxx11::basic_string<char> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |   for (int i = 0; i < v.size(); i++) {
      |                   ~~^~~~~~~~~~
printer.cpp:88:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |     for (int j = 0; j < v[i].second.length(); j++) {
      |                     ~~^~~~~~~~~~~~~~~~~~~~~~
printer.cpp:92:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::__cxx11::basic_string<char> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |     if (i < v.size() -1) {
      |         ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 10452 KB Expected EOF
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 10452 KB printed invalid word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 10452 KB printed invalid word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 10452 KB printed invalid word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 10452 KB printed invalid word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 10580 KB printed invalid word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 10940 KB printed invalid word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 22 ms 11552 KB printed invalid word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 19 ms 21460 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 20 ms 21356 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -