0

I'm doing an exercise for fun (not homework), where given a sequence of strings sorted, with one word that appears too soon, we have to print the sequence sorted. The point is that we have to do it with O(1) auxiliary space, so no vector nor list

I tried the following:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string current, next, mal;
    bool trobat = false;
    cin >> current >> next;
    while (next != "END") {

        if (trobat) {
            if (mal > current and mal < next) {
                cout << current << endl;
                cout << mal << endl;
                trobat = false;
            }
            else {
                cout << current << endl;
            }
        }
        else if (current < next) {
            cout << current << endl;
        }
        else {
            trobat = true;
            mal = current;
            cout << next << endl;
        }
        current = next;
        cin >> next;
    }
    if (trobat) {
        cout << mal << endl;
    }
    else {
        cout << current << endl;
    }
}

Basically, I'm trying to have 3 strings: one with the current value to be treated, one with the next and one with the bad placed word, called mal. trobat indicates if the word not sorted has been found but not printed yet.

If the word is correctly placed, we print it with the else if (current < next). If not, I activate the flag trobat and print the next value, because the next has to be sorted. Then, for the first if, if I found the value, I check if mal is in the right position, otherwise I print the current and repeat the process.

I'm having troubles with the following tests:

INP1:

a
b
e
c
d
f
g
END

OUT1:

a
b
c
c
d
e
f
g

expected OUT1:

a
b
c
d
e
f
g

INP2:

f
aaaaaa
bbbbb
cccc
ddd
ee
END

OUT2:

aaaaaa
aaaaaa
bbbbb
cccc
ddd
f

expected OUT2:

aaaaaa
bbbbb
cccc
ddd
ee
f
Norhther
  • 545
  • 3
  • 15
  • 35
  • Note that you are appending all your input strings into `s`. This is definitely not `O(1)` space, and at that point you might as well use a vector. Also, you need to show the output of your program. – cigien Nov 03 '20 at 18:36
  • @cigien wrote in the OP that I'm using s for debugging purposes only. I'm going to edit with the output. – Norhther Nov 03 '20 at 18:41
  • Oh, that's true. Yes, edit in the output you get. – cigien Nov 03 '20 at 18:42
  • @cigien I added the value of `s`, because the terminal is showing the inputs and outputs at the same time and everything is a mess... Hope it makes sense. – Norhther Nov 03 '20 at 18:46
  • *"the terminal is showing the inputs and outputs at the same time"*. You might create file with all input, and then do `cat input.txt | my_app.exe`. that would not mix input/output. (but no longer be interactive). – Jarod42 Nov 03 '20 at 23:30
  • @Jarod42 I'm on a windows machine, using Visual Studio – Norhther Nov 03 '20 at 23:32
  • Then see [piping-input-into-a-c-program-to-debug-in-visual-studio](https://stackoverflow.com/questions/9613059/piping-input-into-a-c-program-to-debug-in-visual-studio) – Jarod42 Nov 03 '20 at 23:38
  • @Jarod42 Interesting, so you can use `>` operator in VS. I'm going ot edit the question. Feel free to give it a try – Norhther Nov 03 '20 at 23:50

1 Answers1

3

You can simplify the code to:

std::string word1;
std::string word2;
 
std::cin >> word1 >> word2;
 
while (word2 != "END") {
    std::cout << std::min(word1, word2) << std::endl;
    word1 = std::max(word1, word2);
    std::cin >> word2;
}
std::cout << word1 << std::endl;

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302