(algorithm) the longest palindrome substring

Source: Internet
Author: User

Topic:

To find the longest palindrome substring of a string

Ideas:

1. Violent enumeration

The most easy to think of is brute force, enumerate each substring, and then according to the definition of palindrome is not a palindrome, find the longest one.

The time complexity of each substring is O (n^2) and the time complexity of the substring is O (N), so the time complexity is O (n^3).

2. Dynamic planning

The substring of the back character string is also a palindrome, such as p[i,j] (indicating that the substring ending with J begins with I) is a palindrome string, then P[i+1,j-1] is also a palindrome string. So the longest palindrome string can be decomposed into a series of sub-problems.

This requires an extra space of O (n^2), and the time complexity is O (n^2).

State transition equation:

When Str[i]=str[j] dp[i][j]=dp[i+1][j-1]

When Str[i]!=str[j] Dp[i][j]=0

Initial state:

Dp[i][i]=1

Dp[i][i+1]=1 if STR[I]=STR[I+1]

3. Center expansion

The central extension is to take each character of a given string as a center and extend it to both sides to find the longest child palindrome. The time complexity is O (n^2).

But there are two things to consider:

such as ABA, this is an odd length.

such as ABBA, so that the length is even.

4. Manacher method

Cond

Code:
#include <iostream>using namespace std;//Brute forcestring findlongestpalindrome_1 (string &str) {int length= Str.size (); int maxlength=0;int start;for (int i=0;i<length;i++) {for (int j=i+1;j<length;j++) {int left,right;for (left=i,right=j;left<right;left++,right--) {if (str[left]!=str[right]) break;} if (Left>=right && (j-i) >maxlength) {maxlength=j-i+1;start=i;}}} if (maxlength>0) return str.substr (start,maxlength); return NULL;} Dynamic programmingstring findlongestpalindrome_2 (string &str) {const int length=str.size (); int Maxlength=1;int Start;bool dp[length][length];for (int i=0;i<length;i++) for (int j=0;j<length;j++) dp[i][j]=false;for (int i=0;i <length;i++) {dp[i][i]=true;if (i<length-1 && str[i]==str[i+1]) {dp[i][i+1]=true;start=i;maxlength=2;}} for (int len=3;len<=length;len++) {for (int i=0;i<=length-len;i++) {int j=i+len-1;if (dp[i+1][j-1] && str[ I]==str[j]) {Dp[i][j]=true;start=i;maxlength=len;}}} if (maxlength>=2) return str.substr(start,maxlength); return NULL;} Pivot expandstring findlongestpalindrome_3 (string &str) {const int length=str.size (); int maxlength=1;int start; for (int i=0;i<length;i++) {int Left=i-1;int right=i+1;while (left>=0 && right<=length-1 && str [Left]==str[right]) {if (right-left+1>maxlength) {start=left;maxlength=right-left+1;} left--;right++;}} for (int i=0;i<length;i++) {int Left=i;int right=i+1;while (left>=0 && right<=length-1 && str[ Left]==str[right]) {if (right-left+1>maxlength) {start=left;maxlength=right-left+1;} left--;right++;}} if (maxlength>0) return str.substr (start,maxlength); return NULL;} int main () {string str= "ABABCDDCBBD"; cout<<findlongestpalindrome_1 (str) <<endl;cout<< Findlongestpalindrome_2 (str) <<endl;cout<<findlongestpalindrome_3 (str) <<endl;return 0;}

  

(algorithm) the longest palindrome substring

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.