Submission #782832

# Submission time Handle Problem Language Result Execution time Memory
782832 2023-07-14T10:09:36 Z canadavid1 Type Printer (IOI08_printer) C++14
100 / 100
73 ms 17084 KB
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x),end(x)

vector<string> words;

int common_substring(string a,string b)
{
    int i;
    for(i = 0;i < a.size() && i < b.size() && a[i]==b[i]; i++);
    return i;
}


void sort2(int start, int end, int off, string lw)
{
    if (end - start < 2) return;
    vector<string> s[26];
    string lone_word = "";
    for(int i = start; i < end; i++)
    {
        if(words[i].size()==off) {lone_word = words[i];continue;}
        char a = words[i][off];
        s[a-'a'].push_back(words[i]);
    }
    int c = start;
    if(lone_word.size()) words[c++] = lone_word;
    for(int i = 0; i < 26; i++)
    {
        if(i==lw[off]-'a') continue;
        int pc = c;
        for(auto && v : s[i]) words[c++] = v;
        sort2(pc,c,off+1,lw);
    }
    int i = lw[off]-'a';
    int pc = c;
    for(auto && v : s[i]) words[c++] = v;
    sort2(pc,c,off+1,lw);
}

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int N;
    cin >> N;
    words.resize(N);
    for(int i = 0; i < N; i++)
    {
        cin >> words[i];
    }
    string longest_word = "";
    for(auto i : words) if (i.size() > longest_word.size()) longest_word = i;
    // sort lexicographically, but have the characters in the longest word be the last ones
    sort2(0,N,0,longest_word);
    string curr = "";
    string ops = "";
    for(auto i : words)
    {
        int l = common_substring(curr,i);
        while(curr.size()>l)
        {
            curr.pop_back();
            ops.push_back('-');
        }
        curr += i.substr(l);
        ops += i.substr(l);
        ops.push_back('P');
    }
    cout << ops.size() << "\n";
    for(auto i : ops) cout << i << "\n";

}

Compilation message

printer.cpp: In function 'int common_substring(std::string, std::string)':
printer.cpp:10:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   10 |     for(i = 0;i < a.size() && i < b.size() && a[i]==b[i]; i++);
      |               ~~^~~~~~~~~~
printer.cpp:10:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   10 |     for(i = 0;i < a.size() && i < b.size() && a[i]==b[i]; i++);
      |                               ~~^~~~~~~~~~
printer.cpp: In function 'void sort2(int, int, int, std::string)':
printer.cpp:22:27: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   22 |         if(words[i].size()==off) {lone_word = words[i];continue;}
      |            ~~~~~~~~~~~~~~~^~~~~
printer.cpp: In function 'int main()':
printer.cpp:60:26: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   60 |         while(curr.size()>l)
      |               ~~~~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 852 KB Output is correct
2 Correct 8 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 1612 KB Output is correct
2 Correct 21 ms 1952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 3484 KB Output is correct
2 Correct 46 ms 6220 KB Output is correct
3 Correct 38 ms 15096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 2416 KB Output is correct
2 Correct 53 ms 6628 KB Output is correct
3 Correct 44 ms 17084 KB Output is correct
4 Correct 73 ms 6912 KB Output is correct