It got WA verdict during the contest last week but the similar solution is mentioned in the editorial. LINK TO THE EDITORIAL
When I commented on the same and posted the link to my solution there, the author @panik replied ->
" I am looking into your solution although its giving the correct ans on codechef compiler on the TC on which your solution is showing wrong, i am looking into it."
On codeforces posts, someone suggested with possible test cases and errors, it’s giving correct answer on every such case.
Till now, there is no reply from the Author.
Moral of the story : Can anyone please let me know what’s actually my mistake in the code?
I submitted this solution first: solution_1.
Why did I use cin.ignore() inside the loop? Because the sample test case contains a new line after every string. So I thought I need to ignore it as well. Did not work though.
Then I submitted this: solution_2.
This time I assumed that there are no additional new lines after every string. Something like:
3
T T T
T F F
F F F
Again did not work.
Then I submitted this: solution_3.
This time I assumed that the test cases are somewhat like:
3
[\n]
[\n]
T T T
F F F
T T F
And hence I used cin.ignore() twice before entering the loop. And hurrah! This one passed.
Just wanted to add, using getline and cin.ignore() is just way too cumbersome, so I try to avoid them as much as possible.
Your actual solution had some issues when I tried to input the sample test cases as: ./program < in .
The issue with your solution is that, its input method is OS dependent. newline character can be different for different OS’es, eg- its just \n for unix and \r\n (2 characters) for Windows. It is always urged in the FAQ’s to write code which is independent of such differences among OS’es. The best way to do that is, use a dummy getline(cin,s); to take that extra newline.
I think now you can explain why 2cin.ignore();'s fixed the issue.
Thanks @vijju123 This is the first time I encountered this problem. But just one thing, many times before I’ve used this method for similar inputs on codechef and never got WA. And also this code was running successfully on codechef ide. Why was that then?