0

So, I was solving a string-related problem. Judge link: www.urionlinejudge.com.br/judge/en/problems/view/1235

Problem Description:

Your printer has been infected by a virus and is printing gibberish. After staring at several printed pages for a while, you realize that it is printing every line inside-out. In other words, the left half of each line is being printed starting in the middle of the page and proceeding out toward the left margin. Similarly, the right half of each line is being printed starting at the right margin and proceeding in toward the middle of the page.

For example, the line: THIS LINE IS GIBBERISH is being printed as: I ENIL SIHTHSIREBBIG S

In the same way, the line " MANGOS " is being printed as "NAM SOG".Your task is to unscramble a String line from its printed form back into its original order. You can assume that line contains an even number of characters.

I tested many different test cases, and the solutions were exactly as expected. But whenever I tried to submit, the judge saying Wrong Answer 100%.

Is there anything wrong with my code? Or is it my compiler that has issues?

#include <bits/stdc++.h>
using namespace std;
    
int main() {
    int t; cin >> t;
    
    cin >> ws;
    
    while(t--)
    {
        string str;
        string str1, str2;
        getline(cin, str);
        int n = str.length();
    
        str1 = str.substr(0, n/2);
        str2 = str.substr(n/2, n/2);
    
        reverse(str1.begin(), str1.end());
        reverse(str2.begin(), str2.end());
    
        cout << str1 << str2 << endl;
    }
    
    return 0;
}
  • 2
    _Or is it my compiler that has issues?_ Unlikely. Can you add what the code is supposed to do so we don't have to go to an external website? – 001 Jul 07 '21 at 17:39
  • 3
    Also, `include ` is missing a `#` but you shouldn't use that anyway. [Why should I not #include ?](https://stackoverflow.com/q/31816095) – 001 Jul 07 '21 at 17:40
  • 1
    if the entered word is "ABWCD", do you expect `str.substr(0, n/2)` to return "ABV" ? Like, the letters being split in two ? If not, think about what you expect instead ? (having now read the question, I see this is not an issue, but it highlight the importance of stating the problem in the question) – Jeffrey Jul 07 '21 at 17:40
  • 1
    The code works in coliru, and passes all the sample cases http://coliru.stacked-crooked.com/a/b8b0bd723577ca18 – Mooing Duck Jul 07 '21 at 17:41
  • Perhaps the online compiler doesn't even have the non-standard `bits/stdc++.h` header file as the link @JohnnyMopp shared will tell you. – Ted Lyngmo Jul 07 '21 at 17:43
  • All of the sample input/output pairs have an even number of characters, but there's one other in the _description_ with an odd number of characters which reveals the bug in your code. – Mooing Duck Jul 07 '21 at 17:44
  • @JohnnyMopp I didn't notice while copying the code here! *Edited – Shafayath Jamil Jul 07 '21 at 17:44
  • 1
    @MooingDuck It is said in the problem that each test case will contain an even number of characters! – Shafayath Jamil Jul 07 '21 at 17:52
  • @ShafayathJamil *Or is it my compiler that has issues?* -- If a C++ compiler could not generate the proper code for such a toy program, the compiler vendor would be laughed out of the business. The persons responsible for developing the compiler are some of the best programmers in the world. Then they have to thoroughly test, since thousands, if not millions of people are using their software to build applications. – PaulMcKenzie Jul 07 '21 at 18:02
  • Did you even try to include the proper header files before you started suspecting that the compiler may be broken? – Ted Lyngmo Jul 07 '21 at 18:10
  • 1
    Thanks, everyone! Someone posted a link about how cin and getline() mess up each other! All I needed to add was **cin.ignore();** it solved the problem! Thanks – Shafayath Jamil Jul 07 '21 at 18:12
  • 2
    Please explain that in an answer you create yourself. – Yunnosch Jul 07 '21 at 18:14
  • I thought my compiler might have issues because Online Compilers and my compiler showing different answers! So that's why I got confused. @PaulMcKenzie – Shafayath Jamil Jul 07 '21 at 18:15
  • @ShafayathJamil -- Even if the code were exactly the same between the online compiler and your compiler, different answers would mean that your code is invoking undefined behavior somewhere, and the probability of it being an actual compiler bug is tiny (especially for a simple program). – PaulMcKenzie Jul 07 '21 at 18:18

1 Answers1

2

The problem comes from the line cin >> ws; that removes whitespaces.

For example on the input cited in the problem

1
 MANGOS 

your code gives NAMSOG because the leading whitespace of MANGOS is removed.

A possible solution is to read the first line with getline for example replacing the code before the while loop with

int t;

string total;
getline(cin, total);
stringstream ss(total);

ss >> t;
LGrementieri
  • 760
  • 4
  • 12