답안 #871839

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
871839 2023-11-11T17:23:32 Z Matjaz Type Printer (IOI08_printer) C++14
100 / 100
46 ms 5644 KB
//
//  IOI2008Printer.cpp
//  
//
//  Created by Matjaz Leonardis on 11/11/2023.
//

#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int shared_prefix_length(string s1, string s2){
    
    for (int i=0;;i++){
        if (i >= s1.size()) return s1.size();
        if (i >= s2.size()) return s2.size();
        if (s1[i] != s2[i]) return i;
    }
}

int main(){
    
    int N;
    scanf("%d", &N);
    vector<string> s(N+1);
    for (int i=0;i<N;i++){
        char tmp[25];
        scanf("%s", tmp);
        s[i] = tmp;
    }
    s[N] = "";
    
    int max_len = 0;
    int max_index = -1;
    for (int i=0;i<=N;i++) if (s[i].size() > max_len){
        max_len = s[i].size();
        max_index = i;
    }
    
    string target = s[max_index];
    
    for (int i=0;i<=N;i++){
        for (int j=0;j<s[i].size();j++){
            if (s[i][j] != target[j]) s[i][j] -= 26;
        }
    }
    
    sort(s.begin(), s.end());
    
    string ans = "";
    
    for (int i=1;i<=N;i++){
        int l = shared_prefix_length(s[i-1], s[i]);
        for (int j=l;j<s[i-1].size();j++) ans.push_back('-');
        for (int j=l;j<s[i].size();j++){
            if (s[i][j] < 'a') ans.push_back(s[i][j] + 26); else ans.push_back(s[i][j]);
        }
        ans.push_back('P');
    }
    
    printf("%d\n", ans.size());
    for (int i=0;i<ans.size();i++) printf("%c\n", ans[i]);
    
    return 0;
}

Compilation message

printer.cpp: In function 'int shared_prefix_length(std::string, std::string)':
printer.cpp:18:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |         if (i >= s1.size()) return s1.size();
      |             ~~^~~~~~~~~~~~
printer.cpp:19:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |         if (i >= s2.size()) return s2.size();
      |             ~~^~~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:38:44: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   38 |     for (int i=0;i<=N;i++) if (s[i].size() > max_len){
      |                                ~~~~~~~~~~~~^~~~~~~~~
printer.cpp:46:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |         for (int j=0;j<s[i].size();j++){
      |                      ~^~~~~~~~~~~~
printer.cpp:57:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |         for (int j=l;j<s[i-1].size();j++) ans.push_back('-');
      |                      ~^~~~~~~~~~~~~~
printer.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |         for (int j=l;j<s[i].size();j++){
      |                      ~^~~~~~~~~~~~
printer.cpp:64:14: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wformat=]
   64 |     printf("%d\n", ans.size());
      |             ~^     ~~~~~~~~~~
      |              |             |
      |              int           std::__cxx11::basic_string<char>::size_type {aka long unsigned int}
      |             %ld
printer.cpp:65:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |     for (int i=0;i<ans.size();i++) printf("%c\n", ans[i]);
      |                  ~^~~~~~~~~~~
printer.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     scanf("%d", &N);
      |     ~~~~~^~~~~~~~~~
printer.cpp:31:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |         scanf("%s", tmp);
      |         ~~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 416 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 604 KB Output is correct
2 Correct 7 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 1260 KB Output is correct
2 Correct 6 ms 1112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 2540 KB Output is correct
2 Correct 39 ms 4828 KB Output is correct
3 Correct 33 ms 3760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 2288 KB Output is correct
2 Correct 45 ms 5644 KB Output is correct
3 Correct 29 ms 4088 KB Output is correct
4 Correct 46 ms 5388 KB Output is correct