HDU 1544 Palindromes (palindrome sub-string)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.