First, explain a simple and easy-to-understand method of violence: O (n^2)
The idea of solving the problem is: first, define a pstr point to the string str, and then define a p point Pstr,q point to pstr+1;
Second, find a character *p with the next character *q the same position, such as Oo,num++,index = P, and then compare the two same characters *p,*q both sides of the characters are equal, if the equality is further extended to both sides of P--, q++ (p>str&&q!= ' '). If P is pointing to the header, i.e. P=str, then the while loop is called, and then the IF (*p = = *q) is compared, num++,index = Q.
Third, if the two consecutive characters are found to be unequal, let Pstr++,p=pstr,q =pstr+1.
The number and position of the longest palindrome are recorded by Maxnum and Maxindex.
#include <iostream>using namespace std;void getlongestsymmetricallength (char* str) {if (str==null) return; char* pStr = str; char* p = pStr; char* q = pstr+1; int num=0; int maxnum=0;; char* index = PSTR; char* Maxindex = index; while (*pstr! = ') {while ((*p! = *q)) {num = 0; pstr++; p = pStr; Q = pstr+1; } while ((*p = = *q) && (P > str) && (*q! = ') ") {num++; index = p; p--; q++; } if ((p = = str) && (*p = = *q) && (*q! = ")") {num++; index = p; } if (num > maxnum) {maxnum = num; Maxindex = index; } pstr++; p = pStr; Q = pstr+1; } cout << "Result:"; for (int i=0;i<2*maxnum;++i) cout << *maxindex++ << ""; cout << Endl; Cout << "Maxnum:" << maxnum << Endl;} int main () {char* str = "Abbacaacab"; Getlongestsymmetricallength (str); return 0;}
The following spatial complexity and time complexity are O (n)
Ideas:
The complexity is O (n), which refers to traversing through the array, thus having to create an array to hold the length information of the traversed characters (the longest palindrome of each character).
At the same time, such as "ABA" the longest palindrome length of 3, the center is ' b '; "AA" the longest palindrome length is 2, the center is a two-character center, so in the statistical length, the odd and even string length should be compared, what ways to make it unified? --inserting special characters, such as ' # ', "ABA" as "#a #b#a#", "AA" as "#a #a#", so that the longest length of the parity case has the corresponding center respectively. Specific as follows:
1, insert the special character ' # ' in each character putting, convert the character s to T, such as S = "Abaaba", T = "#a #b#a#a#b#a#".
2. Define array p[length], where p[i] represents half of the longest palindrome length centered on ti (because T adds the character ' # ', so it is generally the longest palindrome string in S, i.e. the distance of TI's longest boundary to TI)
Such as:
T = # a # b # a # a # b # # a #
P = 0 1 0 3 0 1 6 1 0 3 0 1 0
The longest length in P is 6, while 6 is the longest palindrome substring of S. Obviously, when the length is even, the corresponding in T is ' # '; when odd, the corresponding is the original character, which is why to add additional characters-uniform parity case.
The key to the following question is how to get the P array.
Assuming that you have now traversed to the position of i = 13, the longest palindrome length of the original string s in T corresponds to position c = 11, corresponding to the length of the original string s is 9, and the string is "ABCBABCBA". The two boundaries of C are L = 2,r = 20;i about C corresponds to the position of I ' = 9. Now how to determine the size of p[i]?
In the diagram, the green solid line below I ' indicates that the left and right boundary of I ' is centered, and I ' boundary is contained between L and R, so the green solid line area below I must be symmetrical. (i.e. I ', I with C symmetry, I ' Centers the length edge less than the boundary L, R, then p[i] = P[i ']), so p[i] = P[i '] = 1.
Another case is discussed below (i.e. I ', I with C symmetry, I ' Centers the length edge beyond the boundary L or R), as
Assuming that you have now traversed to the position of i = 15, the longest palindrome length of the original string s in T corresponds to position c = 11, corresponding to the length of the original string s is 9, and the string is "ABCBABCBA". The two boundaries of C are L = 2,r = 20;i about C corresponds to the position of I ' = 7.
The green solid line in the figure shows that I, I ' are centered in a certain symmetrical region of the boundary L, R; The red solid line indicates that the area beyond the boundary L, R may not be able to guarantee I symmetry; the green dashed lines represent areas across C.
Obviously, we can only determine the green solid line part is symmetrical, that is p[i] >= 5, but the rest of the match we need one by one comparison.
In conclusion, the following conclusions are drawn:
If p[i ']≤r–i,
Then p[i]←p[i ']
else p[i]≥p[i ']. (which we have to expand past the right edge (R) to find p[i].
It is easier to update the position of C in one step, and when the boundary of the current position I exceeds the original R it needs to be updated, i.e.
If p[i] + i > R
C = i;
R = i + p[i].
Class Solution{public://88/88 test Cases passed. Runtime:18 ms String Longestpalindrome (string s) {if (S.size () < 2) {return s; }//Change the string to the desired form string pStr; for (int i = 0; i < s.size (); ++i) {pstr.push_back (' # '); Pstr.push_back (S[i]); } pstr.push_back (' # '); Look for palindrome int length = (int) pstr.size (); int *arrindex = new int[length];//each subscript corresponding to the palindrome length, P int mid = 0;//palindrome substring central position, C int mx = 0;//palindrome substring boundary, R int maxindex = 0; int maxLength = 0; for (int i = 0; i < length; ++i) {arrindex[i] = 0; int mirror = (mid << 1)-i;//i symmetrical position about the center, I ' if (mx > i) {//Whether out of bounds arrindex[i] = (Arrindex[mirror] < Mx-i)? Arrindex[i]: mx-i; } while ((i + arrindex[i] + 1) < length && (I-arrindex[i]-1) >= 0 && pstr[i + arrindex[i] + 1] = = Pstr[i-arrindex[i]-1]) {++arrindex[i]; } if (Arrindex[i] + i > MX) {//If the edge is outside the current, you need to update mid = i; MX = i + arrindex[i]; } if (Arrindex[i] > maxLength) {maxLength = Arrindex[i]; Maxindex = (i-1) >> 1; }} delete[] Arrindex; String Outputstr (S, Maxindex-(maxLength-1)/2, maxLength); return outputstr; }};
Finding the longest palindrome string