Hi, Codeforces folks, i am stuck at this problem LINK, The problem seems to be quite simple, you just have to make a string palindrome adding minimum number of characters at the end, so basically we need to find the longest suffix palindrome and then add the remaining character at the end. My solution implements the same here is the LINK, i am not able to get on what test cases it is giving me wrong solution. Please Help me correcting the code or suggesting me some test-case so that i can figure out. :)
Your solution is correct.The only problem is with your way of taking input.I replaced cin with getline() and got AC.Btw i think using KMP is more prefferable here.
Sorry,but i don't know.
At first it seems a fool question, but it isn't.
For understanding why your code get WA when using
cin >> rev
and AC when usinggetline(cin, rev)
, it is necessary to understand what these functions actually do.cin >> rev
: This statement skip all the white spaces from the input buffer and stores the following characters until find a white space character (the end of line character is considered a white space character too).getline(cin, rev)
: This statement stores all characters from the input buffer to the variablerev
until it find the end of line character. When this character is found, then it is discarded from the buffer.Now that you know how these functions work, how many times is executed the following block of code:
This block is executed exactly
N
times, whereN
is the number of lines. False! This block of code runsN + 1
times when you usecin >> rev
.Why?
This happens because after you read the last line, the call
cin.eof()
returns false since the next character in the buffer is still the end of line character.If you test your code redirecting the standard input from a file, or using the custom test here in codeforces, you will note that you program prints one more line. This line corresponds to processing the last input string in reversed order.
EDIT:
A preferred way of reading input until reach the end of file is:
I hope this helps you.