溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java最長公共子序列是什么

發(fā)布時間:2021-12-20 14:10:09 來源:億速云 閱讀:135 作者:iii 欄目:云計算

這篇文章主要介紹“Java最長公共子序列是什么”,在日常操作中,相信很多人在Java最長公共子序列是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java最長公共子序列是什么”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

public class LongestCommonSubsequence3 {


    public static void main(String[] args) {
        LongestCommonSubsequence3 lcs = new LongestCommonSubsequence3();
        System.out.println(lcs.compute("ABCBDAB","BDCABA"));
    }

    public static int compute(char[] str1, char[] str2)
    {
        int substringLength2 = str1.length;
        int substringLength3 = str2.length;

        // 構造二維數組記錄子問題A[i]和B[j]的LCS的長度,默認初始化為0
        int[][] chess = new int[substringLength2 + 1][substringLength3 + 1];

        // 從從前到后,動態(tài)規(guī)劃計算所有子問題。也可從前向后
        for (int i = 1; i <= substringLength2; i++)
        {
            for (int j = 1; j <= substringLength3; j++)
            {
                if (str1[i - 1] == str2[j - 1])
                    chess[i][j] = chess[i - 1][j - 1] + 1;// 狀態(tài)轉移方程
                else
                    chess[i][j] = Math.max(chess[i - 1][j], chess[i][j - 1]);// 狀態(tài)轉移方程
            }
        }
        System.out.println("substring1:" + new String(str1));
        System.out.println("substring2:" + new String(str2));
        System.out.print("LCS:");

        int i = str1.length, j = str2.length;
        String temp = "";
        while (i != 0 && j != 0)
        {
            if (str1[i - 1] == str2[j - 1])
            {
                temp += str1[i - 1];
                i--;
                j--;
            }
            else{
                if (chess[i][j - 1] > chess[i - 1][j])
                    j--;
                else
                    i--;
            }
        }
        for (int k = temp.length() - 1; k >= 0; k--) {
            System.out.print(temp.toCharArray()[k]);
        }
        System.out.println();
        return chess[str1.length][str2.length];
    }

    public int compute(String str1, String str2)
    {
        return compute(str1.toCharArray(), str2.toCharArray());
    }
}


//================


public class LongestCommonSubsequence2 {


    public static void main(String[] args) {
        LongestCommonSubsequence2 lcs = new LongestCommonSubsequence2();
        System.out.println(lcs.compute("ABCBDAB","BDCABA"));
    }

    public static int compute(char[] str1, char[] str2)
    {
        int substringLength2 = str1.length;
        int substringLength3 = str2.length;

        // 構造二維數組記錄子問題A[i]和B[j]的LCS的長度
        int[][] opt = new int[substringLength2 + 1][substringLength3 + 1];

        // 從后向前,動態(tài)規(guī)劃計算所有子問題。也可從前到后。
        for (int i = substringLength2 - 1; i >= 0; i--)
        {
            for (int j = substringLength3 - 1; j >= 0; j--)
            {
                if (str1[i] == str2[j])
                    opt[i][j] = opt[i + 1][j + 1] + 1;// 狀態(tài)轉移方程
                else
                    opt[i][j] = Math.max(opt[i + 1][j], opt[i][j + 1]);// 狀態(tài)轉移方程
            }
        }
        System.out.println("substring1:" + new String(str1));
        System.out.println("substring2:" + new String(str2));
        System.out.print("LCS:");

        int i = 0, j = 0;
        while (i < substringLength2 && j < substringLength3)
        {
            if (str1[i] == str2[j])
            {
                System.out.print(str1[i]);
                i++;
                j++;
            }
            else if (opt[i + 1][j] >= opt[i][j + 1])
                i++;
            else
                j++;
        }
        System.out.println();
        return opt[0][0];
    }

    public int compute(String str1, String str2)
    {
        return compute(str1.toCharArray(), str2.toCharArray());
    }
}


//====================


package com.lifeibigdata.algorithms.string;

/**
 * Created by lifei on 16/5/25.
 */
public class LongestCommonSubsequence {


    public static void main(String[] args) {
        char[] x = {' ','A','B','C','B','D','A','B'};
        char[] y = {' ','B','D','C','A','B','A'};
        LongestCommonSubsequence lcs = new LongestCommonSubsequence();
        lcs.printLCS(lcs.lcsLength(x, y), x, x.length-1, y.length-1);
    }

    void printLCS(int[][] b,char[] x,int i,int j){
        if(i == 0 || j == 0)
            return;
        if(b[i][j] == 1){
            printLCS(b,x,i - 1,j - 1);
            System.out.print(x[i] + "\t");
        }else if(b[i][j] == 2)
            printLCS(b,x,i - 1,j);
        else
            printLCS(b,x,i,j - 1);
    }


    int[][] lcsLength(char[] x,char[] y){
        int m = x.length;
        int n = y.length;
        int i,j;
        int[][] c = new int[m][n];
        int[][] b = new int[m][n];
        for(i = 1;i < m;i++)
            c[i][0] = 0;
        for(j = 0;j < n;j++)
            c[0][j] = 0;


        for(i = 1;i < m;i++)
            for(j = 1;j < n;j++){
                if(x[i] == y[j]){
                    c[i][j] = c[i - 1][j - 1] + 1;
                    b[i][j] = 1;
                }
                else if(c[i - 1][j] >= c[i][j - 1]){
                    c[i][j] = c[i - 1][j];
                    b[i][j] = 2;
                }else{
                    c[i][j] = c[i][j - 1];
                    b[i][j] = 3;
                }
            }
        return b;
    }
}


/**
 * 滾動數組只求大小,可以降低空間復雜度,時間復雜度不變
 * 求全部的lcs,使用深搜或廣搜        
 * 求有幾個lcs,即只求lcs數目,計算有多少分支,即2的多少次方
 *
 *
 */

Java最長公共子序列是什么
Java最長公共子序列是什么
Java最長公共子序列是什么
Java最長公共子序列是什么
Java最長公共子序列是什么
Java最長公共子序列是什么 Java最長公共子序列是什么 Java最長公共子序列是什么
Java最長公共子序列是什么
Java最長公共子序列是什么
Java最長公共子序列是什么
Java最長公共子序列是什么

到此,關于“Java最長公共子序列是什么”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI