2011年11月18日 星期五

353 - Pesky Palindromes

一個迴文字(palindrome)表示從前面讀過來與從後面讀過來都一樣,例如Z, TOT, MADAM為迴文字,但ADAM並不是。
 
給定一個字串,請你計算該字串的所有子字串共有幾個不同的迴文字。

Input and Output

輸入會有多組測試資料,每組一列字串,字串長度最長80個字元。

請參考範例資料格式輸出該字串共有幾個不同的迴文字子字串,例如ADAM,的迴文字子字串為A, D, M, ADA,共四個,請輸出:
The string 'ADAM' contains 4 palindromes.

Sample input

boy 
adam 
madam 
tot

Sample output

The string 'boy' contains 3 palindromes. 
The string 'adam' contains 4 palindromes.
The string 'madam' contains 5 palindromes.
The string 'tot' contains 3 palindromes.

Note

The 3 unique palindromes in 'boy' are 'b', 'o' and 'y'.
The 4 unique palindromes in 'adam' are 'a', 'd', 'm', and 'ada'.
The 5 unique palindromes in 'madam' are 'm', 'a', 'd', 'ada', and 'madam'.
The 3 unique palindromes in 'tot' are 't', 'o' and 'tot'.


沒有留言:

張貼留言