Dynamic Planning-Longest Common subsequence (LCS)

Source: Internet
Author: User

----- Edit by ZhuSenlin HDU

Returns the longest common subsequence. Given two sequences and
To find the largest common subsequences of X and Y.

1) determine the optimal sub-structure of LCS. If any LCS is set to X and Y

If xm = yn, then zk = xm = yn and the Zk-1 is an lcs of the Xm-1 and Yn-1;

If xm! = Yn, so zk! = Xm contains Z is an LCS of Xm-1 and Y;

If xm! = Yn, so zk! = Yn contains Z is an LCS of X and Yn-1

2) Search for Recursive Solutions

Set C [I, j] to the length of an LCS of the sequence Xi and Yj. Then

3) Calculate the length

Create a Table to store the length of each (I, j) LCS. Then C [Length (X)] [Length (Y)] is the LCS Length of X and Y.

Create another table Path record Path. If Path [I] [j] = 'X' (oblique ), if Path [I] [j] = 'U' (up), if Path [I] [j] = 'l' (left ).

 

The Code is as follows:

template <class T>void CreateTable(T**& pTable, int nH, int nW){DeleteTable(pTable,nH);pTable = new T* [nH];for(int i = 0; i < nH; i++){pTable[i] = new T[nW];memset(pTable[i],0,nW*sizeof(T));}}template <class T>void DeleteTable(T**& pTable, int nH){if(!pTable)return;for(int i = 0; i < nH; i++)if(pTable[i])delete pTable[i];delete pTable;}int LengthLCS(const char* pStrA, const char* pStrB, int**& pTable, char**& pPath){int nLengthA = strlen(pStrA);int nLengthB = strlen(pStrB);for(int i = 1; i<= nLengthA; i++){for(int j = 1; j <= nLengthB; j++) {if(pStrA[i-1] == pStrB[j-1]) {pTable[i][j] = pTable[i-1][j-1]+1;pPath[i][j] = 'x';}else if(pTable[i-1][j] < pTable[i][j-1]){pTable[i][j] = pTable[i][j-1];pPath[i][j] = 'l';}else {pTable[i][j] = pTable[i-1][j];pPath[i][j] = 'u';}}}return pTable[nLengthA][nLengthB];}void PrintLCS(char** pPath, const char* pStrA,int nLengthA, int nLengthB){if(nLengthA == 0 || nLengthB == 0)return;if(pPath[nLengthA][nLengthB] == 'x') {PrintLCS(pPath, pStrA, nLengthA-1, nLengthB-1);cout << pStrA[nLengthA-1] << " ";}else if(pPath[nLengthA][nLengthB] == 'l')PrintLCS(pPath,pStrA,nLengthA,nLengthB-1);elsePrintLCS(pPath,pStrA,nLengthA-1,nLengthB);}

The test code is as follows:

int main(){const char* pStrA = "abcbdab";const char* pStrB = "bdcaba";int** pTable = NULL;char** pPath = NULL;int nLengthA = strlen(pStrA);int nLengthB = strlen(pStrB);CreateTable(pTable,nLengthA+1,nLengthB+1);CreateTable(pPath,nLengthA+1,nLengthB+1);cout << LengthLCS(pStrA,pStrB,pTable,pPath) << endl;PrintLCS(pPath,pStrA,strlen(pStrA),strlen(pStrB));cout << endl;DeleteTable(pTable,strlen(pStrA)+1);DeleteTable(pPath,strlen(pStrA)+1);return 0;}

 

Reference books: Introduction to Algorithms

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.