Longest palindromic Substring
Problem:
Given a string S, find the longest palindromic substring in s. The maximum length of S is assume, and there exists one unique longest palindromic substring.
Ideas:
Dynamic planning
My Code:
Public classSolution { Publicstring Longestpalindrome (string s) {if(s = =NULL|| S.length () = = 0)returns; intLen =s.length (); Boolean[] DP =New Boolean[Len][len]; intStart = 0; intmax = 1; for(inti = 0; i < Len; i++) {Dp[i][i]=true; if(I < len-1 && S.charat (i) = = S.charat (i+1) {Max= 2; Start=i; Dp[i][i+1] =true; } } for(intsize = 3; Size <= Len; size++) { for(inti = 0; I <= len-size; i++) { intj = i+size-1; if(Dp[i+1][j-1] && s.charat (i) = =S.charat (j)) {Max=size; Start=i; DP[I][J]=true; } } } returnS.substring (start,start+max); }}View Code
The Learning Place:
- The equation of dp[i][j] = (S.charat (i) = = S.charat (j)) && Dp[i+1][j-1]? True:false;
- Can't think of the dynamic programming of the equation, first think of the DFS how to do, and then reverse thinking to draw the dynamic programming equation, dynamic planning is fast, because the reduction of a lot of repetitive calculation.
Longest palindromic Substring