Longest Common subsequence Problem

Source: Internet
Author: User

The longest public subsequence problem has been seen in many forums for a long time. I saw a post posted a few days ago and I went to the dynamic planning section in the introduction to algorithms. I will not elaborate on this issue, paste the specific code of the C ++ implementation directly. [Cpp] // large public subsequence # pragma once # include <string> using std: string; # define OVER 1 // indicated by an arrow in the book, here, use macros instead of # define LEFT 2 # define LEFTOVER 3 class LCS {private: string sX; // used to store the sub-sequence string sY; int ** c; // these two-dimensional pointers are used to maintain a dynamic table. The longest common subsequence int ** B; int m will be generated recursively Based on the table; // represents the length of the two strings respectively. In order to be consistent with the pseudocode in the book, the int n; public: LCS ();~ LCS (); void ProRun (); // only the dual-loop void Running () of the dynamic planning part; // for main () to call void PrintResult (int, int ); // recursive output}; [cpp] # include "LCS. h "# include <iostream> # include <fstream> using namespace std; LCS: LCS () {sX =" ABCBDAB "; sY =" BDCABA "; m = sX. size (); n = sY. size (); c = new int * [m + 1]; // the serial number of the character in the book is considered as a subscript, therefore, we need to open up a space B = new int * [m + 1]; // an empty c [0] [0] and B [0] [0] for (int I = 0; I <= m; I ++) {c [I] = New int [n + 1]; B [I] = new int [n + 1];} for (int I = 0; I <= m; I ++) {for (int j = 0; j <= n; j ++) {c [I] [j] = 0; // assign a value to B [I] [j] = 0 ;}} LCS ::~ LCS () {for (int I = 0; I <= m; I ++) {delete c [I]; // release space delete B [I];} delete c; delete B;} void LCS: ProRun () {// the time complexity of this algorithm is O (mn) for (int I = 1; I <= m; I ++) {for (int j = 1; j <= n; j ++) {if (sX [I-1] = sY [J-1]) {c [I] [j] = c [I-1] [J-1] + 1; B [I] [j] = LEFTOVER ;} else if (c [I-1] [j]> = c [I] [J-1]) {c [I] [j] = c [I-1] [j]; B [I] [j] = OVER;} else {c [I] [j] = c [I] [J-1]; B [I] [j] = LEFT ;}}} void LCS: PrintResult (int I, int j) {if (I = 0 | j = 0) {return;} if (B [I] [j] = LEFTOVER) {PrintResult (I-1, J-1); cout <sX [I-1] <endl ;} else if (B [I] [j] = OVER) {PrintResult (I-1, j);} else {PrintResult (I, J-1);} void LCS :: running () {LCS: ProRun (); LCS: PrintResult (m, n);} [cpp] # include <iostream> # include "ALSPro. h "# include" LCS. h "using namespace std; void main () {// ASL as; //. printResult (); LCS ls; ls. running ();}

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.