classSolution { Public: stringLongestpalindrome (strings) {intLength=s.length (); intmaxlen=0; intstart=0; BOOLflag[ -][ -]={false}; //initializes the dynamic state of a character's palindrome string for(intI=0; i<length;i++) {Flag[i][i]=true; if(maxlen<1) {MaxLen=1; Start=i; } } //Initialize the state matrix, here is a palindrome string of length 2 for(intI=0; i<length-1; i++){ if(s[i]==s[i+1]) {flag[i][i+1]=true; if(maxlen<2) {MaxLen=2; Start=i; } } } for(intlen=3; len<=length;len++) {//the length of the palindrome, starting from 3, the length of the string is initialized for(intI=0; i<=length-len;i++) {//substring start Address intj=i+len-1;//substring End Address if(s[i]==s[j]&&flag[i+1][j-1]) {Flag[i][j]=true; if(maxlen<Len) {MaxLen=Len; Start=i; } } } } returns.substr (Start,maxlen);}};
View Code
Note the issue:
The state equation and the transfer equation of the dynamic programming problem must be known that the equation has the initialization condition
Here is a palindrome string of characters and a palindrome string of two characters,
All other lengths of palindrome strings are based on this two initialization condition.
http://blog.csdn.net/feliciafay/article/details/16984031
Maximum palindrome string: leetcode:longest palindromic Substring