The longest palindrome algorithm 2

Source: Internet
Author: User

Problem:

The longest palindrome subsequence of the given input string (the subsequence does not require continuous).

LPs (I,J) is used to denote the length of the longest palindrome sequence from the string I character to the J character, the length of the string is N, and LPs (1,n) is required:

LPS (I,J) = 0; i>j;

LPS (I,J) = 1; I==j;

LPS (i,j) =lps (i+1,j-1) +2; STR[I]==STR[J];

LPs (I,J) =max (LPs (I+1,J), LPs (i,j-1)); STR[I]!=STR[J];

Zuichanghuiwen2.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <string>using namespace Std;int c[20][20];void LPS ( String str) {int m = str.length (); int j;for (int j = 0; J < m; J + +) for (int i = j + 1, i < m; i++) c[i][j] = 0;for (in t i = 0; I < m; i++) C[i][i] = 1;for (int l=2;l<=m;l++)       //This must be calculated by length from short start for (int i = 0; i<m-l+1; i++) {j = i + l-1;if (str[i] = = S TR[J]) C[i][j] = C[i + 1][j-1] + 2;elsec[i][j] = c[i + 1][j] > c[i][j-1]? C[i + 1][j]: c[i][j-1];}} void rebuild (String str) {int m = c[0][str.length ()-1];int mi;for (Auto i = 0; i < str.length (); i++) if (str[i] = = str [i + m-1]) {mi = i;break;} for (int i = mi; i < Mi + m; i++) cout << str[i];} int main () {string str ("DGDFGDFABCACBAGFGFG"); LPS (str); rebuild (str); while (1);    return 0;}

  

The longest palindrome algorithm 2

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.