动态规划+回溯法查找:两字符串的最长公共子序列LCS


测试

bdcaba

abcbdab

最长公共子序列长度:4

状态转移矩阵如下:

* * * * * * * *

* ^ \ < \ < < \

* ^ ^ ^ ^ \ < <

* ^ ^ \ < ^ ^ ^

* \ ^ ^ ^ ^ \ <

* ^ \ ^ \ < ^ \

* \ ^ ^ ^ ^ \ ^

最长公共子序列:b a d b

--------

abcbdab

bdcaba

最长公共子序列长度:4

状态转移矩阵如下:

* * * * * * *

* ^ ^ ^ \ < \

* \ < < ^ \ <

* ^ ^ \ < ^ ^

* \ ^ ^ ^ \ <

* ^ \ ^ ^ ^ ^

* ^ ^ ^ \ ^ \

* \ ^ ^ ^ \ ^

最长公共子序列:a b c b

(本题lcs是逆序输出的)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

enum SrcDir {
    SRC(-1, "源头格网", "*"),
    UP(0, "数据源自上方格网", "^"),
    LEFT(1, "数据源自左边格网", "<"),
    LEFTUP(2, "数据源自左上方格网", "\\");
    public int code;
    public String msg;
    public String dir;
    SrcDir(int code, String msg, String dir) {
        this.code = code;
        this.msg = msg;
        this.dir = dir;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str1 = scanner.nextLine();
        String str2 = scanner.nextLine();
        int len1 = str1.length();
        int len2 = str2.length();
        int[][] dp = new int[len1 + 1][len2 + 1];//动态规划矩阵
        SrcDir[][] status = new SrcDir[len1 + 1][len2 + 1];//状态转移矩阵
        for (int i = 0; i <= len1; i++) {
            for (int j = 0; j <= len2; j++) {
                //初始化
                if (i == 0 || j == 0) {
                    dp[i][j] = 0;
                    status[i][j] = SrcDir.SRC;
                } else {
                    //状态转移
                    if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
                        dp[i][j] = dp[i - 1][j - 1] + 1;
                        status[i][j] = SrcDir.LEFTUP;
                    } else {
                        dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                        status[i][j] = dp[i - 1][j] >= dp[i][j - 1] ? SrcDir.UP : SrcDir.LEFT;
                    }
                }
            }
        }
        //从右下角回溯到0行或者0列结束
        int i = len1, j = len2;
        List<Character> lcs = new ArrayList<>();
        while (i > 0 && j > 0) {
            if (status[i][j].code == 0) {
                i--;
            }
            if (status[i][j].code == 1) {
                j--;
            }
            if (status[i][j].code == 2) {
                //回溯路径上的状态为LEFTUP(2, "数据源自左上方格网", "\\")的即为公共字符
                lcs.add(str1.charAt(i - 1));
                i--;
                j--;
            }
        }
        System.out.println("最长公共子序列长度:" + dp[len1][len2]);
        System.out.println("状态转移矩阵如下:");
        for (SrcDir[] val : status) {
            for (SrcDir val1 : val) {
                System.out.print(val1.dir + " ");
            }
            System.out.println();
        }
        System.out.print("最长公共子序列:");
        for (char val : lcs) {
            System.out.print(val + " ");
        }
    }
}
看网上到处是求lcs长度,没有找lcs的,就补了这个帖子😊

#Java##学习路径#
全部评论

相关推荐

大方的大熊猫准备进厂:1.教育背景:你希望从事什么专业的工作你的主修课就是什么;成绩优秀是你应该做的,没什么可描述的,成绩不优秀也许人家在大学忙着创业呢?(成绩优秀不一定是好事,只能说明多元化的大学你上成了高中,没有真正上明白大学,反而体现了你死板,不爱社交,没有别的突出能力) 2.实践经历:你想表达的意思没有说清楚。你是说你会个性化服务,还是你有实习经历。如果没有带来,经济收益,表彰,更好的发展前景,那你还不如说说提升了自己哪些技能。你说有人给你送锦旗我都能明白你优秀,但是你说你会xxxx,你说这话谁信,证据呢。 3.入伍经历:你描述的就是你的工作职责或者你应该做的,并没有体现出来你把这个事情做好了,而且入伍经历并不能证明你能干好你要应聘的工作,不如只写经历其余所有内容都不写。 4.荣誉技能:重点突出一下,但不要过多描述,这些荣誉的含金量懂得都懂。 重点:你要应聘什么工作(具体岗位,实习生不具体),你的期望薪资
点赞 评论 收藏
分享
评论
2
6
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务