Describe:Given a stringS, find the longest palindromic substring inS. Assume that maximum length of s is, and there exists one unique longest palindromic substring.
Give a string string to find the largest string of strings. This is a typical longest palindrome string problem, there are currently four kinds of solutions.
1. Violence test, test each substring, obviously this is the dumbest method, Time complexity O (n^3)
2. Dynamic planning, refer to http://blog.163.com/zhaohai_1988/blog/static/2095100852012716105847112/and http://www.cnblogs.com/ en-heng/p/3973679.html
3. Center extension method, which is also the first thing I think of when I see the topic, with an element as the center, respectively, to expand forward and backward, to see whether the same, to consider abcba form abccba form the time complexity of these two different palindrome substrings is O (n^2).
1 Public StaticString Matchchar (String s,intLintR) {//match characters with L and R as subscript2 intleft=l;3 intright=R;4 while(Left<=right&&left>=0&&right<s.length () &&s.charat (left) = =S.charat (right)) {5left--;6right++;7 }8 returnS.substring (left+1, right);9 }Ten Publicstring Longestpalindrome (string s) { One intn=s.length (); A if(0==n)return""; -String longeststring=s.substring (0,1);//a character - for(inti=0;i<n-1;i++){ theString Tempstring1=matchchar (S, I, I);//ABCBA Form - if(Tempstring1.length () >longeststring.length ()) -longeststring=tempString1; -String Tempstring2=matchchar (s,i,i+1);//ABCCBA Form + if(Tempstring2.length () >longeststring.length ()) -longeststring=tempString2; + } A returnlongeststring; at}
4. The legendary Manacher algorithm, time complexity O (n), is also a more difficult to understand algorithm. Read a few blog, want to Know can refer
http://blog.csdn.net/ggggiqnypgjg/article/details/6645824
Http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html
LeetCode5 Longest palindromic Substring