i_love_emilia_clarke's blog

By i_love_emilia_clarke, history, 7 years ago, In English

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. :)

  • Vote: I like it
  • -8
  • Vote: I do not like it

| Write comment?
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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.

7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Sorry,but i don't know.

7 years ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

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 using getline(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 variable rev 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:

while(!cin.eof()) {
    // your code...

This block is executed exactly N times, where N is the number of lines. False! This block of code runs N + 1 times when you use cin >> rev.


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.


A preferred way of reading input until reach the end of file is:

while (cin >> rev) {
    //  your code...

I hope this helps you.