답안 #445769

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
445769 2021-07-19T14:10:35 Z dxz05 Type Printer (IOI08_printer) C++14
100 / 100
105 ms 43712 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")

using namespace std;

#define SZ(x) (int)(x).size()

const int MAXN = 1e6 + 3e2;

pair<int, string> str[MAXN];

int main(){
    //ios_base::sync_with_stdio(false);
    //cin.tie(nullptr); cout.tie(nullptr);

    int n;
    scanf("%d", &n);

    int ind = 0;
    for (int i = 1; i <= n; i++){
        char c[25];
        scanf("%s", &c);
        int len = strlen(c);
        str[i].second.resize(len);
        for (int j = 0; j < len; j++) str[i].second[j] = c[j];
        if (str[i].second.size() > str[ind].second.size()) ind = i;
    }

    string longest = str[ind].second;

    for (int i = 1; i <= n; i++){
        int j = 0;
        while (j < str[i].second.size() && str[i].second[j] == longest[j]) j++;
        str[i].first = j;
    }

    sort(str + 1, str + n + 1);

    vector<char> ans;
    for (int it = 1; it <= n; it++){
        string s = str[it].second, t = str[it - 1].second;

        if (SZ(t) > SZ(s)){
            int need = SZ(t) - SZ(s);
            while (need--) ans.push_back('-');
            t.resize(s.size());
        }

        int cnt = 0;
        for (int i = 0; i < min(SZ(t), SZ(s)); i++){
            if (t[i] == s[i]) cnt++; else
                break;
        }

        int need = max(0, SZ(t) - cnt);
        while (need--){
            ans.push_back('-');
        }

        for (int i = cnt; i < s.size(); i++){
            ans.push_back(s[i]);
        }
        ans.push_back('P');

    }

    printf("%d\n", SZ(ans));
    for (char c : ans) printf("%c\n", c);

    return 0;
}

/**
3
print
the
poem
*/

Compilation message

printer.cpp: In function 'int main()':
printer.cpp:22:17: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char (*)[25]' [-Wformat=]
   22 |         scanf("%s", &c);
      |                ~^   ~~
      |                 |   |
      |                 |   char (*)[25]
      |                 char*
printer.cpp:33:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |         while (j < str[i].second.size() && str[i].second[j] == longest[j]) j++;
      |                ~~^~~~~~~~~~~~~~~~~~~~~~
printer.cpp:60:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |         for (int i = cnt; i < s.size(); i++){
      |                           ~~^~~~~~~~~~
printer.cpp:17:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
printer.cpp:22:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |         scanf("%s", &c);
      |         ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 39372 KB Output is correct
2 Correct 25 ms 39372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 39452 KB Output is correct
2 Correct 26 ms 39444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 39372 KB Output is correct
2 Correct 23 ms 39372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 39364 KB Output is correct
2 Correct 24 ms 39444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 39352 KB Output is correct
2 Correct 24 ms 39448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 39508 KB Output is correct
2 Correct 25 ms 39544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 39672 KB Output is correct
2 Correct 33 ms 39940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 39996 KB Output is correct
2 Correct 34 ms 39756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 40744 KB Output is correct
2 Correct 89 ms 42808 KB Output is correct
3 Correct 65 ms 42176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 40280 KB Output is correct
2 Correct 105 ms 43456 KB Output is correct
3 Correct 71 ms 42568 KB Output is correct
4 Correct 99 ms 43712 KB Output is correct