OJ Practice 44--t5 Longest palindormic Substring

Source: Internet
Author: User

The longest palindrome substring of the string.

Ideas

1. Start from both sides to the middle

2. From the middle start to the two sides development

3. From the middle of the variant, more complex, see http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html

"Other code1-Ideas 2"

 stringLongestpalindrome (strings) {intn=s.size (); if(n==0)return ""; stringRE=S.SUBSTR (0,1);  for(intI=0; i<n; i++){            stringp1=Testpalindrome (s,i,i); if(P1.size () >re.size ()) Re=P1; stringP2=testpalindrome (s,i,i+1); if(P2.size () >re.size ()) Re=P2; }        returnre; }    stringTestpalindrome (stringSintC1,intC2) {        intL=C1; intR=C2; intn=s.size ();  while(l>=0&&r<=n-1&&s[l]==S[r]) {L--; R++; }        returnS.SUBSTR (L +1, r-l-1); }

"Result 1"

Time is O (n^2), 100+ms, ranked back.

"Other code2-Ideas 3"

//Transform S into T.//for example, S = "ABBA", T = "^ #a #b#b#a#$".//^ and $ signs is sentinels appended to the end to avoid bounds checkingstringPreprocess (strings) {intn =s.length (); if(n = =0)return "^$"; stringRET ="^";  for(inti =0; I < n; i++) ret+="#"+ S.SUBSTR (i,1); RET+="#$"; returnret;} stringLongestpalindrome (strings) {stringT =preprocess (s); intn =t.length (); int*p =New int[n]; intC =0, R =0;  for(inti =1; I < n1; i++) {    intI_mirror =2*c-i;//equals to I ' = C-(i-c)P[i]= (R > i)? Min (r-i, P[i_mirror]):0; //attempt to expand palindrome centered at I     while(T[i +1+ P[i]] = = T[i-1-P[i]]) P[i]++; //If palindrome centered at I expand past R,//Adjust center based on expanded palindrome.    if(i + p[i] >R) {C=i; R= i +P[i]; }  }   //Find The maximum element in P.  intMaxLen =0; intCenterindex =0;  for(inti =1; I < n1; i++) {    if(P[i] >maxlen) {MaxLen=P[i]; Centerindex=i; }  }  Delete[] P; returnS.substr (Centerindex-1-MaxLen)/2, MaxLen);}

"Result 2"

Time is O (n), 12ms, ranked first

For the hair mid-range problem is so difficult! (? •?_•?)? ┻━┻

Afternoon analysis.

OJ Practice 44--t5 Longest palindormic 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.