Submission #1039389

# Submission time Handle Problem Language Result Execution time Memory
1039389 2024-07-30T19:42:04 Z ThommyDB Type Printer (IOI08_printer) C++17
100 / 100
63 ms 5572 KB
#include<bits/stdc++.h>

using namespace std;

int n;
vector<string> words;
string largest_word, ans;

bool compare(string x, string y){
    int a = 0;
    for(int i = 0; i < x.size(); i++){
        if(x[i]!=largest_word[i]){
            a=i;
            break;
        }
        if(i==x.size()-1)a=x.size();
    }
    int b = 0;
    for(int i = 0; i < y.size(); i++){
        if(y[i]!=largest_word[i]){
            b=i;
            break;
        }
        if(i==y.size()-1)b=y.size();
    }
    return a==b ? x < y : a < b;
}

signed main(){
    cin >> n;
    words.resize(n);
    for(int i = 0; i < n; i++){
        cin >> words[i];
        if(largest_word.size() < words[i].size()) largest_word = words[i]; 
    }
    sort(words.begin(), words.end(), compare);
    ans = words[0] + 'P';
    for(int i = 1; i < n; i++){
        string& a = words[i-1];
        string& b = words[i];
        int hold;
        for(int j = 0; j < min(a.size(), b.size()); j++){
            if(a[j]!=b[j]){
                hold = j;
                break;
            }
            if(j == min(a.size(), b.size())-1) hold = min(a.size(), b.size());
        }
        for(int j = a.size()-1; j >= hold; j--) ans.push_back('-');
        for(int j = hold; j < b.size(); j++) ans.push_back(b[j]);
        ans.push_back('P');
    }
    cout << ans.size() << "\n";
    for(auto u : ans){
        cout << u << "\n";
    }
}

Compilation message

printer.cpp: In function 'bool compare(std::string, std::string)':
printer.cpp:11:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |     for(int i = 0; i < x.size(); i++){
      |                    ~~^~~~~~~~~~
printer.cpp:16:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |         if(i==x.size()-1)a=x.size();
      |            ~^~~~~~~~~~~~
printer.cpp:19:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |     for(int i = 0; i < y.size(); i++){
      |                    ~~^~~~~~~~~~
printer.cpp:24:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |         if(i==y.size()-1)b=y.size();
      |            ~^~~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:42:26: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
   42 |         for(int j = 0; j < min(a.size(), b.size()); j++){
      |                        ~~^~~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:47:18: warning: comparison of integer expressions of different signedness: 'int' and 'long unsigned int' [-Wsign-compare]
   47 |             if(j == min(a.size(), b.size())-1) hold = min(a.size(), b.size());
      |                ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:50:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |         for(int j = hold; j < b.size(); j++) ans.push_back(b[j]);
      |                           ~~^~~~~~~~~~
printer.cpp:49:35: warning: 'hold' may be used uninitialized in this function [-Wmaybe-uninitialized]
   49 |         for(int j = a.size()-1; j >= hold; j--) ans.push_back('-');
      |                                 ~~^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 600 KB Output is correct
2 Correct 11 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 1260 KB Output is correct
2 Correct 11 ms 1112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 2148 KB Output is correct
2 Correct 57 ms 4676 KB Output is correct
3 Correct 41 ms 3732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 2032 KB Output is correct
2 Correct 63 ms 5572 KB Output is correct
3 Correct 46 ms 4240 KB Output is correct
4 Correct 60 ms 5380 KB Output is correct