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