(1)理论部分:

(2)习题:

  最长公共子串:

 1 package month7.dp;
 2
 3 //https://www.nowcoder.com/questionTerminal/181a1a71c7574266ad07f9739f791506
 4 public class 最长公共子串 {
 5     public static void main(String[] args) {
 6         最长公共子串 s = new 最长公共子串();
 7         String str1 = "ABABCGGG";
 8         String str2 = "BABCAH";
 9         System.out.println(s.Longest_Common_Substring(str1, str2));
10     }
11 //将二维数组table[i][j]用来记录具有这样特点的子串——结尾同时也为为串x1x2⋯xi与y1y2⋯yj的结尾——的长度
12     public int Longest_Common_Substring(String str1, String str2) {
13         int len1 = str1.length();
14         int len2 = str2.length();
15         int result = 0;
16         int num = 0;
17         int[][] table = new int[len1 + 1][len2 + 1];
18
19         for (int i = 0; i <= len1; i++) {
20             table[i][0] = 0;
21         }
22         for (int j = 0; j <= len2; j++) {
23             table[0][j] = 0;
24         }
25         for (int i = 1; i <= len1; i++) {
26             for (int j = 1; j <= len2; j++) {
27                 char c1 = str1.charAt(i-1);
28                 char c2 = str2.charAt(j-1);
29                 if (c1 != c2) {
30                     table[i][j] = 0;
31                 } else { // c1 == c2
32                     table[i][j] = table[i - 1][j - 1] + 1;
33                     if (result < table[i][j]) {
34                         result = table[i][j];
35                         num = i;
36                         System.out.println("-----" + num + "--");
37
38                     }
39                 }
40             }
41         }
42         System.out.println(str1.substring(num - result , num ));
43         return result;
44     }
45
46     public int Longest_Common_Substring2(String str1, String str2) {
47         int len1 = str1.length();
48         int len2 = str2.length();
49         int result = 0;
50         int num = 0;
51         int[][] table = new int[len1 + 1][len2 + 1];
52         for (int i = 0; i <= len1; i++) {
53             for (int j = 0; j <= len2; j++) {
54                 if (i == 0 || j == 0) {
55                     table[i][j] = 0;
56                 } else if (str1.charAt(i - 1) != str2.charAt(j - 1)) {
57                     table[i][j] = 0;
58                 } else { // c1 == c2
59                     table[i][j] = table[i - 1][j - 1] + 1;
60                     if (result < table[i][j]) {
61                         result = table[i][j];
62                         num = i;
63                         System.out.println("-----" + num + "--");
64
65                     }
66                 }
67             }
68         }
69         System.out.println(str1.substring(num - result , num ));
70         return result;
71     }
72
73 }

View Code

  最长公共子序列:

 1 package month7.dp;
 2
 3 public class 最长公共子序列 {
 4     int[][] b = new int[10][10];
 5     public static void main(String[] args) {
 6         最长公共子序列 s = new 最长公共子序列();
 7 //        String str1 ="ABCD";
 8 //        String str2 = "EACB";
 9         String str1 ="AGGTHAB";
10         String str2 = "GXTXAYB";
11         System.out.println(s.Longest_Common_Subsequence(str1, str2));
12         s.print_lsc(str1.length(), str2.length(), str1);
13
14     }
15     //用二维数组c[i][j]记录串x1x2⋯xi与y1y2⋯yj的LCS长度
16     public int Longest_Common_Subsequence(String str1,String str2){
17         int len1 = str1.length();
18         int len2 = str2.length();
19         int[][] table = new int[str1.length()+1][str2.length()+1];
20 //        int[][] b = new int[str1.length()+1][str2.length()+1];
21         for(int i=0;i<=len1;i++){
22             for(int j=0;j<=len2;j++){
23                 if(i==0 || j == 0){
24                     table[i][j] = 0;
25                 }else if(str1.charAt(i-1) != str2.charAt(j-1)){
26                     if(table[i-1][j] > table[i][j-1]){
27                         table[i][j] = table[i-1][j];
28                         b[i][j] = 1;
29                     }else{
30                         table[i][j] = table[i][j-1];
31                         b[i][j] = 2;
32                     }
33 //                    table[i][j] = Math.max(table[i-1][j], table[i][j-1]);
34                 }else{ // Xi = Yj
35                     table[i][j] = table[i-1][j-1] +1;
36                     b[i][j] = 3;
37                 }
38             }
39         }
40         int commomLength =  table[len1][len2];
41         return commomLength;
42     }
43     public void print_lsc(int i,int j,String str1){
44         if(i ==0 || j == 0)
45             return ;
46         if(b[i][j] == 3){
47             print_lsc(i-1, j-1, str1);
48             System.out.print(str1.charAt(i-1));
49         }else if(b[i][j] == 2){
50             print_lsc(i, j-1, str1);
51         }else if(b[i][j] == 1){
52             print_lsc(i-1, j, str1);
53         }
54     }
55 }

View Code

  最短编辑距离

 1 package month7.dp;
 2
 3 //https://www.cnblogs.com/masterlibin/p/5785092.html
 4 public class 最短编辑距离 {
 5     public static void main(String[] args) {
 6         最短编辑距离 s = new 最短编辑距离();
 7
 8     }
 9
10     public int editionDistance(String word1, String word2) {
11         if (word1 == null || "".equals(word1)) {
12             return word2.length();
13         }
14         if (word2 == null || "".equals(word1)) {
15             return word1.length();
16         }
17         int len1 = word1.length();
18         int len2 = word2.length();
19         int[][] table = new int[len1 + 1][len2 + 1];
20         for (int i = 0; i <= len1; i++) {
21             table[i][0] = i;
22         }
23         for (int j = 0; j <= len2; j++) {
24             table[0][j] = j;
25         }
26         for (int i = 1; i <= len1; i++) {
27             for (int j = 1; j <= len2; j++) {
28                 char c1 = word1.charAt(i - 1);
29                 char c2 = word2.charAt(j - 1);
30                 if (c1 == c2) {
31                     table[i][j] = table[i - 1][j - 1];
32                 } else {
33                     int add = table[i][j - 1] + 1;
34                     int delate = table[i - 1][j] + 1;
35                     int edition = table[i - 1][j - 1] + 1;
36                     table[i][j] = Math.min(add, Math.min(delate, edition));
37                 }
38             }
39         }
40         return table[len1][len2];
41     }
42
43 }

View Code

转载于:https://www.cnblogs.com/vincentbnu/p/9486600.html

动态规划最常见的习题 (最长公共子串、最长公共子序列、最短编辑距离)相关推荐

  1. 【To Understand】动态规划:求最长公共子串/最长公共子序列

    动态规划:求最长公共子串/最长公共子序列 本博客转载自:https://blog.csdn.net/u013074465/article/details/45392687 该博客中详细讲解了求最长公共 ...

  2. 动态规划:最长公共子串 最长公共子序列

    一.最长公共子串 1. 题目 给定两个序列 X 和 Y,如果 Z 即是 X 的子串,又是 Y 的子串,我们就称它是 X 和 Y 的公共子串,注意子串是连续的. 例如 X = { A, B, C, D, ...

  3. Algorithm:C++/python语言实现之求旋转数组最小值、求零子数组、求最长公共子序列和最长公共子串、求LCS与字符串编辑距离

    Algorithm:C++/python语言实现之求旋转数组最小值.求零子数组.求最长公共子序列和最长公共子串.求LCS与字符串编辑距离 目录 一.求旋转数组最小值 1.分析问题 2.解决思路 二.求 ...

  4. 最长公共子序列|最长公共子串|最长重复子串|最长不重复子串|最长回文子串|最长递增子序列|最大子数组和...

    最长公共子序列|最长公共子串|最长重复子串|最长不重复子串|最长回文子串|最长递增子序列|最大子数组和 文章作者:Yx.Ac   文章来源:勇幸|Thinking (http://www.ahathi ...

  5. 【恋上数据结构】动态规划(找零钱、最大连续子序列和、最长上升子序列、最长公共子序列、最长公共子串、0-1背包)

    动态规划(Dynamic Programming) 练习1:找零钱 找零钱 - 暴力递归 找零钱 - 记忆化搜索 找零钱 - 递推 思考题:输出找零钱的具体方案(具体是用了哪些面值的硬币) 找零钱 - ...

  6. 最长公共子串问题-Java:解法一

    分享一个大牛的人工智能教程.零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请轻击http://www.captainbed.net package live.every.day.Pro ...

  7. 采用顺序结构存储串,设计实现求串S和串T的一个最长公共子串的算法。

    算法分析 先固定字符串str1,取其第一个字符str1[0],(KMP算法)查找str1和str2中有没有以该字符开头的公共子串:即将str[0]与str2中的字符挨个比较,若遇到相等的,再接着比较s ...

  8. 最长公共子串LCS (Longest Common Subsequence) 算法

    三个方法都有所借鉴,但代码部分是自己试着写出来的,虽然最后的运行结果都是正确的,但此过程中难免会有考虑不周全的地方,如发现代码某些地方有误,欢迎指正.同时有新的想法,也可以提出! 采用顺序结构存储串, ...

  9. 【动态规划】最长公共子序列与最长公共子串

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

最新文章

  1. 图神经网络的解释性综述!
  2. PostgreSQL — 外键关联操作
  3. JAVA中对日期格式的处理
  4. Python3学习笔记(urllib模块的使用)
  5. 亚信科技数据库AntDB通过金融分布式事务数据库标准测试
  6. Qt工作笔记-QXmlStreamReader中的字符编码的坑
  7. asp.net request获取url各个部分
  8. java 输出字符串的所有排列_JAVA 输出指定字符串所有排列组合
  9. setuptools Command Reference
  10. 2015第28周六SVN和Git
  11. python函数的使用
  12. 便于理解假设检验的好例子
  13. wlan mac地址 network interface IPv6 IPv4
  14. 设置iPhone来电铃声(图文教程)
  15. C# signtool error:no certificates were found that met all the given criteria 错误解决方案
  16. 04 分布式文件系统以及MapReduce入门程序
  17. Warshall算法的实现(两种方式)
  18. html邮箱图标代码,CSS3 单元素邮件图标
  19. nacos registry, gateway register failed java.lang.IllegalArgumentException: no server available
  20. js vue 设置excel单元格样式_vue+elementui 项目纯前端Export2Excel导出excel,并利用xlsx-style设置单元格样式...

热门文章

  1. sqlServer MD5
  2. Python 二分查找
  3. c 链表之 快慢指针 查找循环节点(转)
  4. 针对于多线程概念的理解
  5. 第一天--来个占位符,让自己有一席之地
  6. 士兵杀敌(二)(线段树+树状数组)
  7. 隐藏快捷方式扩展名(.lnk)
  8. 一次实现可以在某些场合替代菱形继承?
  9. 8086汇编学习小记-王爽汇编语言实验12
  10. Java 多线程初探(二) - 通讯与协调