15-2 find the length of the maximum palindrome

Source: Internet
Author: User

  
 

  1. #ifndef PALINDROME_H_
  2. #define PALINDROME_H_
  3. #include<iostream>
  4. #include<string>
  5. int palindrome_longest(char *str,int front,int back);
  6. #endif
  
 
  1. #include"Palindrome.h"
  2. #define Max(a,b) a>b? a:b
  3. int palindrome_longest(char *str,int front,int back){
  4. int pali_count=0;
  5. if(front==back)
  6. return pali_count+1;
  7. if(str[front]==str[back]){
  8. pali_count=palindrome_longest(str,front+1,back-1)+1;
  9. Span class= "pun" >} else {
  10. pali_count=Max(palindrome_longest(str,front+1,back),palindrome_longest(str,front,back-1));
  11. }
  12. return pali_count;
  13. }
  
 
  1. #include "LongPath.h"
  2. #include "Palindrome.h"
  3. int main(){
  4. char *str="civic";
  5. char *str0="racecar";
  6. char *str1="character";
  7. std::cout<<palindrome_longest(str,0,4)<<std::endl;
  8. std : cout << palindrome_longest ( str0 0 Span class= "pun", 6 ) << std : endl ;
  9. std::cout<<palindrome_longest(str1,0,8)<<std::endl

In the algorithm above, we use the same algorithm for the longest common subsequence, which is the third edition of the introduction to the algorithm, section 15-4. Its thought is worth drawing on. It is instructive to deal with these "irregular" sub-string problems, and for the longest common subsequence, not the longest common subsequence of the continuity that we used to be familiar with, but the common subsequence that does not require continuity, so we know we cannot start indenting or Backoff from two subsequence.

This is the characteristic of these problems. The palindrome for this problem is that we use similar ideas, if equal, then good, their palindrome length plus 1, if not equal, then only one end of the sacrifice, it to return a character, and then beg the two the longest of the line.
  
 
  1. if(str[front]==str[back]){
  2. pali_count=palindrome_longest(str,front+1,back-1)+1;
  3. Span class= "pun" >} else {
  4. pali_count=Max(palindrome_longest(str,front+1,back),palindrome_longest(str,front,back-1));
  5. }
The above is the core code. Always sacrifice one end. Both ends are not simultaneously retreated.









From for notes (Wiz)

15-2 find the length of the maximum palindrome

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.