Longest palindrome string: leetcode:longest palindromic Substring

Source: Internet
Author: User

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.


Maximum palindrome string: leetcode:longest palindromic Substring

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.