Topic:
Given A string S, you is allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.
For example:
Given "aacecaaa" , return "aaacecaaa" .
Given "abcd" , return "dcbabcd" .
Test instructions: Precede the original string with a number of letters, so that it forms a palindrome, returning the shortest.
Train of thought 1: The beginning of the idea, the total after sweeping the surface, if [o-i] can form a palindrome, then the n reverse and add to the front of the s can be, so that can be barely AC, but is obviously the worst algorithm.
Code 1 (AC time 850ms or so):
Public classSolution { Publicstring Shortestpalindrome (string s) { for(intI=s.length (); i>=1;i--) if(Ispalindrome (s.substring (0, i))) return NewStringBuilder (s.substring (i)). Reverse () +s; return""; } Static BooleanIspalindrome (String s) {intLeft=0,right=s.length ()-1; while(left<Right )if(S.charat (left++)!=s.charat (right--)) return false; return true; }}
Train of thought 2: And thinking 1 difference is not too much, consider whether can be judged in different ways, just Judge Palindrome way different, take the central point of the way, can be as an idea, but the effect and 1 almost.
Code 2 (AC time 800ms or so):
Public classSolution { Publicstring Shortestpalindrome (string s) {if(s = =NULL|| S.length () < 2) returns; intindex = 0; for(inti = 0; I < s.length () *2-1; i++){ intleft = I/2; intright = I/2; if(i%2 = = 1) Right++; if(helper (s, left, right)) {index= (i%2 = = 1? right*2:right*2+1); } } return NewStringBuilder (s.substring (index)). Reverse () +s; } Private BooleanHelper (String s,intLeftintRight ) { while(left >= 0 && right < S.length () && S.charat (left) = =S.charat (right)) { Left--; Right++; } if(left = =-1) return true; return false; } }
Idea 3 (recommended idea): Using the Manchester algorithm, which uses DP to save the existing results, a large acceleration.
In Manchester palindrome judgment, add judgment, because the topic is mainly to find a palindrome containing the first character, so in the process of the cycle, when the eldest son Palindrome contains the first character, the index is updated, and finally the palindrome behind the part of reverse after adding to the S front can.
Code 3 (AC time 300ms or so):
Public classSolution { Publicstring Shortestpalindrome (string s) {if(s = =NULL|| S.length () < 2) returns; intLen =s.length (); intn = len * 2 + 3; Char[] Tocharry =New Char[n]; tocharry[0] = ' $ '; tocharry[1] = ' # '; for(inti = 0; I < s.length (); i++) {tocharry[2*i + 2] =S.charat (i); tocharry[2*i + 3] = ' # '; } tocharry[n-1] = '! '; int[] p =New int[n]; intin = 0; intMX = 0; intMAXPI = 0; intMaxin = 0; for(inti = 1; i < n-1; i++){ if(I <mx) p[i]= Math.min (Mx-i, P[2*in-i]); ElseP[i]= 1; while(Tocharry[i + p[i]] = = Tocharry[i-p[i]]) p[i]++; if(P[i] + i >MX) {MX= i +P[i]; Inch=i; } if(P[i] > maxpi && in =P[in]) {MAXPI=P[i]; Maxin=In ; }} StringBuilder SB=NewStringBuilder (); for(inti = maxin+p[maxin]; i < n-1; i + = 2) {sb.append (tocharry[i]); } returnSb.reverse () +s; }}
Reference Link: http://www.felix021.com/blog/read.php?2040
http://m.blog.csdn.net/blog/xc889078/10858041
[Leetcode-java] Shortest palindrome