To find the longest common subsequence of two sequences by dynamic programming, a longest common subsequence needs to be constructed.

Title description

To find the longest common subsequence of two sequences using a dynamic programming algorithm, a longest common subsequence needs to be constructed.

Input

Each set of input includes two lines, and each line includes a string.

Output

A longest common subsequence of two character sequences. (The input has ensured the uniqueness of the longest common subsequence)

Sample input Copy

acdbxx
ccdxx

Sample output Copy

cdxx

method one:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;  
  
public class Main {  

 static char []a=new char[500];
 static char []b=new char[500];
    public static void main(String[] args) {  
        Scanner cin = new Scanner(System.in);  
        while(cin.hasNext()) {
        String str1=cin.next();
        String str2=cin.next();
        a=str1.toCharArray();
        b=str2.toCharArray();
        int n1=a.length;
        int n2=b.length;
        int dp[][]=new int[n1+1][n2+1];
        int drection[][]=new int[n1+1][n2+1];//记录方向
        for(int i=0;i<=n1;i++) {
        	drection[i][0]=0;
        	
        }
        for(int i=0;i<=n2;i++) {
        	drection[0][i]=0;
        	
        }
        int i,j;
        for(i=1;i<=n1;i++)
        {
            for(j=1;j<=n2;j++)
            {
                if(a[i-1]==b[j-1])   ///注意这里的下标是i-1与j-1
                {
                    dp[i][j]=dp[i-1][j-1]+1;
                    drection[i][j]=1;  ///斜向下标记
                }
                else if(dp[i][j-1]>dp[i-1][j])
                {
                	dp[i][j]=dp[i][j-1];
                	drection[i][j]=2;  ///向右标记
                }
                else
                {
                	dp[i][j]=dp[i-1][j];
                	drection[i][j]=3;  ///向下标记
                }
            }
        }
        printlcs(n1,n2,a,drection);
        
        
        
        }
        }
    public static void printlcs(int i,int j, char[]a,int [][]drection) {
    	if(i==0||j==0)return;
    	if(drection[i][j]==1) {
    		printlcs(i-1,j-1,a,drection);
    		System.out.print(a[i-1]);
    	}
    	else if(drection[i][j]==2) {
    		printlcs(i,j-1,a,drection);
    	}else {
    		printlcs(i-1,j,a,drection);
    	}
    	
    }
    	
  

      
}

Method Two:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;  
   
public class Main {  
 
 static char []a=new char[500];
 static char []b=new char[500];
    public static void main(String[] args) {  
        Scanner cin = new Scanner(System.in);  
        while(cin.hasNext()) {
        String str1=cin.next();
        String str2=cin.next();
        a=str1.toCharArray();
        b=str2.toCharArray();
        int n1=a.length;
        int n2=b.length;
        int dp[][]=new int[n1+1][n2+1];
        int drection[][]=new int[n1+1][n2+1];//记录方向
        for(int i=0;i<=n1;i++) {
            drection[i][0]=0;
             
        }
        for(int i=0;i<=n2;i++) {
            drection[0][i]=0;
             
        }
        int i,j;
        for(i=1;i<=n1;i++)
        {
            for(j=1;j<=n2;j++)
            {
                if(a[i-1]==b[j-1])   
                {
                    dp[i][j]=dp[i-1][j-1]+1;
                    drection[i][j]=1;  ///斜向下标记
                }
                else if(dp[i][j-1]>dp[i-1][j])
                {
                    dp[i][j]=dp[i][j-1];
                    drection[i][j]=2;  ///向右标记
                }
                else
                {
                    dp[i][j]=dp[i-1][j];
                    drection[i][j]=3;  ///向下标记
                }
            }
        }
        printlcs(n1,n2,drection);
         
         
         
        }
        }
    public static void printlcs(int i,int j,int [][]drection) {
        int k=0;
        char arr[]=new char[500];
        while(i>0 && j>0)
        {
            if(drection[i][j]==1)   ///斜向下标记
            {
                arr[k]=a[i-1];
                k++;
                i--;
                j--;
            }
            else if(drection[i][j]==2)  ///斜向右标记
                j--;
            else if(drection[i][j]==3)  ///斜向下标记
                i--;
        }
        for(i=k-1;i>=0;i--)
           System.out.print(arr[i]);
             
    }
   
 
       
}

Published 81 original articles · Liked 65 · Visitors 7554

Guess you like

Origin blog.csdn.net/qq_45353823/article/details/105373353
Recommended