Submission #876981

# Submission time Handle Problem Language Result Execution time Memory
876981 2023-11-22T15:47:30 Z StefanL2005 Type Printer (IOI08_printer) C++14
50 / 100
49 ms 4808 KB
#include <bits/stdc++.h>
using namespace std;
 
struct LetterPoz{
    int poz, length;
};
 
bool rule(LetterPoz A, LetterPoz B)
{
    if (A.length == B.length)
        return A.poz < B.poz;
    return A.length < B.length;
}
 
int Similarities(string A, string B)
{
    int lA = A.length(), lB = B.length(), nr = 0;
 
    for (int i = 0; i < min(lA, lB); i++)
        if (A[i] == B[i])
            nr++;
        else
            break;
 
    return nr;
}

void MoveBiggestLast (vector<string> &EndStrings, int MaxSize, vector<char> &Commands)
{
    int GetPoz = 0;
    for (int i = 0; i < EndStrings.size(); i++)
        if (EndStrings[i].size() > EndStrings[GetPoz].size())
            GetPoz = i;
    
    vector<string> Str = EndStrings; 
    int level =  2;
    string Last = "";
    do
    {
        vector<string> Copy;
        for (int i = 0; i < Str.size(); i++)
        {
            if (EndStrings[GetPoz].substr(0, level) == Str[i].substr(0, level))
            {    
                Copy.push_back(EndStrings[i]);
                continue;
            }

            int sim = Similarities(Str[i], Last);
 
            for (int j = 0; j < Last.length() - sim; j++)
                Commands.push_back('-');
            for (int j = sim; j < Str[i].length(); j++)
                Commands.push_back(Str[i][j]);
            Commands.push_back('P');

            Last = Str[i];
        }
        level++;
        Str = Copy;
    }while (Str.size() > 1 && level < EndStrings[GetPoz].size());

    int sim = Similarities(Last, EndStrings[GetPoz]);

    for (int j = 0; j < Last.length() - sim; j++)
        Commands.push_back('-');
    for (int j = sim; j < EndStrings[GetPoz].length(); j++)
        Commands.push_back(EndStrings[GetPoz][j]);
    Commands.push_back('P');
}
 
int main()
{
    int N;
    cin>> N;
    vector<string> To_Print(N);
    vector<char> Commands;
    for (int i = 0; i < N; i++)
        cin>> To_Print[i];
 
    sort(To_Print.begin(), To_Print.end());
 
    char FinalLetter;
    int MaxSize = 0;

    for (int i = 0; i < N; i++)
    {
        if (To_Print[i].size() > MaxSize)
        {
            MaxSize = To_Print[i].size();
            FinalLetter = To_Print[i][0];
        }
    }   

    string Last = "";
    for (int i = 0; i < N; i++)
    {
        if (To_Print[i][0] == FinalLetter)
            continue;

        int sim = Similarities(To_Print[i], Last);
 
        for (int j = 0; j < Last.length()- sim; j++)
            Commands.push_back('-');
        for (int j = sim; j < To_Print[i].length(); j++)
            Commands.push_back(To_Print[i][j]);
        Commands.push_back('P');
 
        Last = To_Print[i];
    }
    
    if (Last != "")
        for (int i = 0; i < Last.size(); i++)
            Commands.push_back('-');

    vector<string> EndStrings;
    for (int i = 0; i < N; i++)
        if (To_Print[i][0] == FinalLetter)
            EndStrings.push_back(To_Print[i]);

    MoveBiggestLast(EndStrings, MaxSize, Commands);

    cout<< Commands.size() << "\n";
    for (int i = 0; i < Commands.size(); i++)
        cout<< Commands[i] << "\n";
    
    return 0;
}

Compilation message

printer.cpp: In function 'void MoveBiggestLast(std::vector<std::__cxx11::basic_string<char> >&, int, std::vector<char>&)':
printer.cpp:31:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |     for (int i = 0; i < EndStrings.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~~
printer.cpp:41:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |         for (int i = 0; i < Str.size(); i++)
      |                         ~~^~~~~~~~~~~~
printer.cpp:51:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |             for (int j = 0; j < Last.length() - sim; j++)
      |                             ~~^~~~~~~~~~~~~~~~~~~~~
printer.cpp:53:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |             for (int j = sim; j < Str[i].length(); j++)
      |                               ~~^~~~~~~~~~~~~~~~~
printer.cpp:61:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |     }while (Str.size() > 1 && level < EndStrings[GetPoz].size());
      |                               ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:65:23: 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 j = 0; j < Last.length() - sim; j++)
      |                     ~~^~~~~~~~~~~~~~~~~~~~~
printer.cpp:67:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |     for (int j = sim; j < EndStrings[GetPoz].length(); j++)
      |                       ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:88:32: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   88 |         if (To_Print[i].size() > MaxSize)
      |             ~~~~~~~~~~~~~~~~~~~^~~~~~~~~
printer.cpp:103:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |         for (int j = 0; j < Last.length()- sim; j++)
      |                         ~~^~~~~~~~~~~~~~~~~~~~
printer.cpp:105:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |         for (int j = sim; j < To_Print[i].length(); j++)
      |                           ~~^~~~~~~~~~~~~~~~~~~~~~
printer.cpp:113:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |         for (int i = 0; i < Last.size(); i++)
      |                         ~~^~~~~~~~~~~~~
printer.cpp:124:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |     for (int i = 0; i < Commands.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~
printer.cpp:118:9: warning: 'FinalLetter' may be used uninitialized in this function [-Wmaybe-uninitialized]
  118 |         if (To_Print[i][0] == FinalLetter)
      |         ^~
# 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 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB printed invalid word
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 604 KB Output is correct
2 Incorrect 8 ms 1116 KB printed invalid word
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 11 ms 1236 KB printed invalid word
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 2516 KB Output is correct
2 Correct 49 ms 4808 KB Output is correct
3 Incorrect 32 ms 3792 KB printed invalid word
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 2260 KB printed invalid word
2 Halted 0 ms 0 KB -