Algorithm _longest palindromic Substring (looking for the longest back text string)

Source: Internet
Author: User

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)

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.