LCS problem (longest common subsequence)-Dynamic planning implementation

Source: Internet
Author: User

Problem Description:

Problem "The longest common character sequence for two-character sequences

Attention:

Characters that do not require a substring (string one) must appear consecutively in string two.

Thinking Analysis: The optimal substructure and overlapping sub-problem properties have, so to take the dynamic programming of the longest common sub-sequence structure

Set Sequence x=

Recursive structure of xm-1= sub-problem

The optimal substructure property of the longest common sub-sequence problem is known, to find out the x=

By this recursive structure it is easy to see that the longest common subsequence problem has sub-problem overlapping properties.

For example, when calculating the longest common subsequence of x and Y, you might want to calculate the longest common subsequence of X and Yn-1 and Xm-1 and Y. These two sub-problems contain a common sub-problem, the longest common subsequence that computes Xm-1 and Yn-1.

与矩阵连乘积最优计算次序问题类似,我们来建立子问题的最优值的递归关系。用c[i,j]记录序列Xi和Yj的最长公共子序列的长度。其中Xi=<x1, x2, …, xi>,Yj=<y1, y2, …, yj>。
When i=0 or j=0, the empty sequence is the longest common sub-sequence of Xi and YJ, so c[i,j]=0. In other cases, the recursive relationship can be established by the theorem as follows:

Code:
 Public classCOMSUBSTR { Public Static void Main(string[] Arg) {String A ="Blog.csdn.net"; String B ="Csdn.blogt";      Comsubstring (A, b); }Private Static void comsubstring(String str1, String str2) {Char[] A = Str1.tochararray ();Char[] B = Str2.tochararray ();intA_length = A.length;intB_length = B.length;int[] LCS =New int[A_length +1][b_length +1];//Initialize array         for(inti =0; I <= b_length; i++) { for(intj =0; J <= A_length; J + +) {Lcs[j][i] =0; }          } for(inti =1; I <= a_length; i++) { for(intj =1; J <= B_length; J + +) {if(A[i-1] = = B[j-1]) {Lcs[i][j] = lcs[i-1][j-1] +1; }if(A[i-1]! = b[j-1]) {Lcs[i][j] = lcs[i][j-1] > Lcs[i-1][J]? Lcs[i][j-1]: Lcs[i-1][J]; }              }          }//output array results to observe         for(inti =0; I <= a_length; i++) { for(intj =0; J <= B_length; J + +) {System. out. Print (lcs[i][j]+","); } System. out. println (""); }Minimum common string constructed by array        intMax_length = Lcs[a_length][b_length];Char[] Comstr =New Char[Max_length];intI =a_length, J =b_length; while(max_length>0){if(lcs[i][j]!=lcs[i-1][j-1]){if(lcs[i-1][j]==lcs[i][j-1]){//Two characters equal, for common characterscomstr[max_length-1]=a[i-1];                      max_length--;                  i--;j--; }Else{//Take the older elders as the longest common sub-sequence of A and B                    if(lcs[i-1][j]>lcs[i][j-1]) {i--; }Else{j--; }                  }              }Else{i--;j--; }} System. out. Print ("The longest common string is:"); System. out. print (COMSTR); }  }
Output Result:
0,0,0,0,0,0,1,2,2,2,2,0,0,0,0,0,0,1,2,3,3,3,0,0,0,0,0,0,1,2,3,4,4,0,0,0,0,0,1,1,2,3,4,4,0,1,1,1,1,1,1,2,3,4,4,0,1,2,2,2,2,2,2,3,4,4,0,1,2,3,3,3,3,3,3,4,4,0,1,2,3,4,4,4,4,4,4,4,0,1,2,3,4,5,5,5,5,5,5,0,1,2,3,4,5,5,5,5,5,5,0,1,2,3,4,5,5,5,5,5,5,0,1,2,3,4,5,5,5,5,5,6The longest common string is: csdn. T  

# # My QR code is as follows, welcome to Exchange Discussion # #

You are welcome to pay attention to the "It question summary" subscription number. Every day to push the classic face test and interview tips, are dry! The QR code of the subscription number is as follows:

Reference:
http://blog.csdn.net/v_JULY_v/article/details/6110269
http://www.programgo.com/article/74411986718/

LCS problem (longest common subsequence)-Dynamic planning implementation

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.