Codeforces – Problem 798A – Checking the Calendar

After reviewing the Meller watch, it’s time for some more IT related stuff in the blog.  Thus, I have decided to do what I usually do when I want to write some article and I do not know what to write about – I go to the codeforces.com and I take a look at their problems. As far as I have decided to start coding a bit in C++, the idea was to solve a problem with C# and then solve it with C++. Just for the sake of learning something new.

The problem was looking like this:

Mike has a string s consisting of only lowercase English letters. He wants to change exactly one character from the string so that the resulting one is a palindrome.

A palindrome is a string that reads the same backward as forward, for example strings “z“, “aaa“, “aba“, “abccba” are palindromes, but strings “codeforces“, “reality“, “ab” are not.

Input

The first and single line contains string s (1 ≤ |s| ≤ 15).

Output

Print “YES” (without quotes) if Mike can change exactly one character so that the resulting string is palindrome or “NO” (without quotes) otherwise.

It looks pretty much easy as anything. There are at least 10 ways to solve a problem like this with C# (I am not joking), and if I had to come up with the smartest one, it would have been something like this:

  • Reverse the string and compare it to the half with the initial string
  • If there are more than two differences, write “NO”.

Well, it was not that easy. It was two steps more difficult, and this time it was good, that I had the tests to see where I was failing. 🙂 The idea is that you must be able to change exactly one character, in order to write “YES”. If you have a palindrome from the beginning and the number of characters is equal, then you are not able to write “YES”. Anyhow, interesting first problem, just proving that one should never assume that he has completely understood the problem. These were my results:

cf

As you can imagine, the solution with C# is quite trivial, I did not care to write it in a better way:

After all, if it is good enough for the 100+ tests of CodeForces, it is good enough for VitoshAcademy.

The tricky part was to take the solution and to rewrite it with C++. It was not a pleasant task, as far as I had to google almost every second operator. But, it was kind of useful and partially fun. Furthermore, I now know how a C++ solution should look like in order to pass the tests in CodeForces.com.

Are you interested? Here it is:

Yup! 🙂

Tagged with: , ,