AIBOHP - Aibohphobia

BuggyD suffers from AIBOHPHOBIA - the fear of Palindromes. A palindrome is a string that reads the same forward and backward.

To cure him of this fatal disease, doctors from all over the world discussed his fear and decided to expose him to large number of palindromes. To do this, they decided to play a game with BuggyD. The rules of the game are as follows:

BuggyD has to supply a string S. The doctors have to add or insert characters to the string to make it a palindrome. Characters can be inserted anywhere in the string.

The doctors took this game very lightly and just appended the reverse of S to the end of S, thus making it a palindrome. For example, if S = "fft", the doctors change the string to "ffttff".

Nowadays, BuggyD is cured of the disease (having been exposed to a large number of palindromes), but he still wants to continue the game by his rules. He now asks the doctors to insert the minimum number of characters needed to make S a palindrome. Help the doctors accomplish this task.

For instance, if S = "fft", the doctors should change the string to "tfft", adding only 1 character.


The first line of the input contains an integer t, the number of test cases. t test cases follow.

Each test case consists of one line, the string S. The length of S will be no more than 6100 characters, and S will contain no whitespace characters.


For each test case output one line containing a single integer denoting the minimum number of characters that must be inserted into S to make it a palindrome.




hide comments
nilabja16180: 2017-02-22 16:45:49

LCS is the key, AC in ONE GO!

nilabja16180: 2017-02-22 16:45:49

LCS is the key, AC in ONE GO!

iareeb: 2017-01-22 08:31:24


akashranjan28: 2016-12-07 10:35:00

runtime error (SIGSEGV) in 1st submission. wasted 1 more hour to find the mistake.
Got AC 2nd time with the "same code".
can't figure out what was wrong with 1st time

kira28: 2016-12-05 15:45:50

too easy!!!
simplest dp ever

up79: 2016-11-26 07:02:36

easy one ! AC in one go :)

rraj001: 2016-10-10 16:40:17

simple lcs

NIKHIL KUMAR SINGH: 2016-10-09 19:28:23

Had a look at the problem 1 year ago and no guess about it. And today, I solved it in just a single attempt. Hoping to be a good coder every passing day

lalywr: 2016-09-30 00:47:53

20th :D with a DP :D

jasbir_220b2: 2016-08-21 10:34:14

can be done without lcs... simple recursive algo along with memoization .. :-)

Added by:Matthew Reeder
Time limit:1.940s
Source limit:30000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Al-Khawarizm 2006