Java longest back text string

Source: Internet
Author: User

 Packagestring.string1_6; Public classlongestpalidrome{/*** Use regular methods to judge every character in a string as a center, including odd and even cases * worst time complexity O (N), Space complexity O (1)*/     Public Static intLongestpalidrome (String s) {if(s = =NULL|| S.length () <= 0)            return0 ; intMax = 0 ;  for(inti=0; I<s.length (); i++)        {             for(inttime=0; time<2; time++)            {                intLength = Getmax (s, I, time+i); if(Max <length) Max=length; }        }        returnMax; }    Private Static intGetmax (String S,intLeft,intRight ) {         while(left >= 0 && right <= s.length ()-1) {            if(S.charat (left)! =S.charat (right)) Break; Left--; Right++; }        returnRight-left-1; }    /*** Using the Menacher algorithm (which is actually a case of dynamic programming) * Time complexity O (n), spatial complexity O (n)*/     Public Static intlongestPalidrome2 (String s) {string Judge=Init (s); intid = 0;//the center of the largest palindrome        intmx = 0;//Id+p[id], which is the second half of the largest palindrome        int[] p =New int[Judge.length ()];  for(intI=1; I<judge.length ()-1; i++)        {            if(I <mx) {P[i]= Min (p[2*id-i], mx-i);//prediction of P[i]            }            Else{P[i]= 1;//predictions cannot be made using previous conclusions, so you can only assume that p[i] is only 1 of the length of the element itself.            }             while(Judge.charat (i-p[i]) = = Judge.charat (I+p[i]))//whether a palindrome still exists outside of MXp[i]++ ; //update MX and ID            if(P[i]+i-1 >mx) {mx= P[i] + i-1; ID=i; }        }        intMXID = 0 ;  for(intI=1; I<p.length; i++)        {            if(P[i] >P[mxid]) Mxid=i; }        returnP[mxid]-1 ; }    /*** Preprocessing: The original string str is populated as a $#...#* format * Because Java does not have an end flag for the string, so at the end use * as the end flag * where the $ and * are used when the Sentinel's*/    Private Staticstring init (String str) {StringBuilder SB=NewStringBuilder (Str.length () *2+2) ; Sb.append ("$#") ;  for(inti=0; I<str.length (); i++) {sb.append (Str.charat (i)). Append ("#") ; } sb.append ("*") ; returnsb.tostring (); }    Private Static intMinintAintb) {returna > B?b:a; }     Public Static voidMain (string[] args) {//DSJFKLDSABABASDLKFJSD//String str = "SKJFLKDSJFKLDSABABASDLKFJSDWIEOWOWWPW";    SYSTEM.OUT.PRINTLN (LongestPalidrome2 (str)); }}

Reference://https://yq.aliyun.com/articles/3739

Java longest back text string

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.