title :Given A string S, find the longest palindromic substring in S. Assume that maximum length of S are, and there exists one unique longest palindromic substring.
The first thing you need to know is "palindrome" (do not know this translation right?) String Palindrome string about a certain character symmetry, in the left and right side of the same distance from the center of the word typeface.
It is also important to note that the symmetry center can have multiple identical characters, such as:
Therefore, the implementation of the program can start from the symmetry center, starting from the string position 0 as the symmetry Center in turn to verify. Time complexity O (N2);
Char* Longestpalindrome (Char*s) {intlen=strlen (s); intbegin=0, maxlen=0; for(intI=0; i<len;i++) { intleft=i,right=i; while(right<len-1&& s[right]==s[right+1]) right++; while(left>0&& right<len-1&& s[left-1]==s[right+1]) {left--;right++;} if((right-left+1) >maxlen) {MaxLen=right-left+1; Begin=Left ; } } Char* s_out=malloc((maxlen+1)*sizeof(Char)); for(intI=0; i<maxlen;i++) {S_out[i]=s[begin++]; } S_out[maxlen]=' /'; returns_out;}
Algorithm _longest palindromic Substring (looking for the longest back text string)