Topic links : http://acm.hdu.edu.cn/showproblem.php?pid=1544
Problem Analysis:
The problem requires finding the number of palindrome substrings in a string's contiguous substring. First, you need to differentiate between successive substrings and sub-sequences.
Successive substrings are strings of contiguous characters, and the subsequence needs to satisfy the relative order of the characters appearing in the subsequence and the relative order in which they appear in the string.
The solution of the problem: according to the length of palindrome sub-string is divided into odd and even two kinds of possibilities;
1. When the palindrome string is an odd length, the middle character is any character, and the other characters of the leftmost and rightmost characters of the string are taken apart, and the two sides are extended to determine
Number of odd palindrome substrings.
2. When the palindrome string length is even, by selecting the other characters other than the last character as the basic characters, expand to both sides to find the number of even palindrome substrings.
3. The number of even-numbered palindrome substrings and the number of odd palindrome substrings, together with the number of characters in the string, are required.
The code is as follows:
#include <stdio.h>#include<string.h>Const intMax_n = the+Ten;CharStr[max_n];intMain () { while(SCANF ("%s", str)! =EOF) { intAns =0; intLen =strlen (str); intLeft , right; for(inti =1; I < Len-1; ++i) { left= i-1; Right= i +1; while(Left >=0&& Right < Len && Str[left] = =Str[right]) ans+ +, left--, right++; } for(inti =0; I < Len-1; ++i) {if(Str[i] = = str[i+1]) {ans++; Left= i-1; Right= i +2; while(Left >=0&& Right < Len && Str[left] = =Str[right]) ans+ +, left--, right++; }} printf ("%d\n", ans +Len); } return 0;}
HDU 1544 Palindromes (palindrome sub-string)