Dynamic programming _ Common eldest son sequence Java

Source: Internet
Author: User

Given two strings, the public oldest oldest sequence is obtained. Unlike substrings, a subsequence simply guarantees that the relative order of the elements is the same as the original string, and that the elements are not required to be contiguous. If the problem is with

Violent law to do, you can imagine a long as M with a long n string, to compare C (n,j) (j=1→n) and C (m,i) (i=1→m) have the same and record the longest sequence, the total number of comparisons is

C (n,1) *c (m,1) *1+c (n,2) *c (m,2) +...+c (n,j) *c (m,j) *j j=min (m,n). If you do it with dynamic planning, the time complexity is only O (MN).

Importjava.util.LinkedList; Public classLongestcommonsubsequence { Public Static voidMain (string[] args) {String S1= "123456789"; String S2= "724765626";    Longestcommonsubsequence (S1,S2); }    //The function of finding the oldest sequence     Public Static voidlongestcommonsubsequence (String s1,string s2) {Char[] arr1=S1.tochararray (); Char[] arr2=S2.tochararray (); intm=arr1.length; intn=arr2.length; //C[i][j] represents the longest common subsequence length of the two substrings of elements 0 to J in elements 0 through I and arr2 in arr1        int[] c=New int[M+1] [N+1]; //b[i][j] means c[i][j] how it came about, three cases, discussed in detail belowString[][] B=NewString[m+1][n+1]; //Initialize c[][], because the length of the longest common subsequence of an empty string and any string is 0         for(inti=0;i<m;i++) c[m][0]=0;  for(inti=0;i<n;i++) c[0][n]=0;  for(inti=1;i<=m;i++)        {             for(intj=1;j<=n;j++)            {                //if two strings i-1 and j-1 are the same, then c[i][j] can be obtained from its nearest top-left value plus 1 .                if(arr1[i-1]==arr2[j-1]) {C[i][j]=c[i-1][j-1]+1; B[I][J]=""; }                //if different, then c[i][j] takes the maximum value above or to the left adjacent to it                Else                {                    if(c[i-1][j]>c[i][j-1]) {C[i][j]=c[i-1][j]; B[I][J]= "↑"; }                    Else{C[i][j]=c[i][j-1]; B[I][J]= "←"; }                    }            }        }        //store data in stacks to facilitate sequential output of resultsLinkedlist<character> l=NewLinkedlist<>();  while(b[m][n]!=NULL)        {            if(B[m][n].equals ("") {l.add (arr1[m-1]); M--; N--; }                Else if(B[m][n].equals ("←")) n--; Else if(B[m][n].equals ("↑")) M--; }        //elements in the output stack         while(L.size ()!=0) {System.out.print (L.removelast ()); }    }}

Print results

Dynamic programming _ Common eldest son sequence Java

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.