Dynamic Programming - Part2
实现如下:
public static void main(String[] args) { String squence1 = "ABCBDAB";
String squence2 = "BDCABC"; int len1 = squence1.length(), len2 = squence2.length();
if (len1 <= 0 || len2 <= 0) {
System.out.println("empty squence!");
return;
}
int[][] allCommonSubLen = new int[len1+1][len2+1];
//print all sub-squences
String subSquence = "";
//初始化i=0, j=0
for (int i = 0; i < len1; i++) {
allCommonSubLen[i][0] = 0;
}
for (int j = 0; j < len2; j++) {
allCommonSubLen[0][j] = 0;
}
//迭代关系
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
char c1 = squence1.charAt(i-1);
char c2 = squence2.charAt(j-1);
if (c1 == c2) {
//subSquence += c1;
allCommonSubLen[i][j] = allCommonSubLen[i-1][j-1] + 1;
//System.out.println("i="+i+" ; j="+j+" ; subSquence="+subSquence);
} else {
allCommonSubLen[i][j] = Math.max(allCommonSubLen[i-1][j], allCommonSubLen[i][j-1]);
if (allCommonSubLen[i][j] == allCommonSubLen[i-1][j]) {
//subSquence += c2;
//System.out.println("i="+i+" ; j="+j+" ; subSquence="+subSquence);
} else {
//subSquence += c1;
//System.out.println("i="+i+" ; j="+j+" ; subSquence="+subSquence);
}
}
}
}
//System.out.println(allCommonSubLen[len1][len2]);
}
Dynamic Programming - Part2的更多相关文章
- 动态规划 Dynamic Programming
March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...
- Dynamic Programming
We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...
- HDU 4223 Dynamic Programming?(最小连续子序列和的绝对值O(NlogN))
传送门 Description Dynamic Programming, short for DP, is the favorite of iSea. It is a method for solvi ...
- hdu 4223 Dynamic Programming?
Dynamic Programming? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 算法导论学习-Dynamic Programming
转载自:http://blog.csdn.net/speedme/article/details/24231197 1. 什么是动态规划 ------------------------------- ...
- Dynamic Programming: From novice to advanced
作者:Dumitru 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg An impo ...
- HDU-4972 A simple dynamic programming problem
http://acm.hdu.edu.cn/showproblem.php?pid=4972 ++和+1还是有区别的,不可大意. A simple dynamic programming proble ...
- [算法]动态规划(Dynamic programming)
转载请注明原创:http://www.cnblogs.com/StartoverX/p/4603173.html Dynamic Programming的Programming指的不是程序而是一种表格 ...
- hdu 4972 A simple dynamic programming problem(高效)
pid=4972" target="_blank" style="">题目链接:hdu 4972 A simple dynamic progra ...
随机推荐
- vue.js插件使用(01) vue-resource
本文的主要内容如下: 介绍vue-resource的特点 介绍vue-resource的基本使用方法 基于this.$http的增删查改示例 基于this.$resource的增删查改示例 基于int ...
- js一些实用例子
1.获取焦点选中文本内容 $("#id").focus(function(){ this.select(); }); 2.表单提交方式 A.自动提交 setTimeout(func ...
- PHP JS HTML ASP页面跳转代码 延时跳转代码 返回到上一界面并刷新 JS弹出指定大小的新窗口
1.PHP延时跳转代码 //跳转到浏览界面 header("Refresh:1;url=machine_list.php"); //不延时 <?php header(&quo ...
- python爬取糗百第一页的笑话
自学python网络爬虫,发现request比urllib还是要好用一些,因此利用request和BeautifulSoup来实现糗百的首页笑话的抓取.BeautifulSoup通过find和find ...
- 007-python基础-pyc是什么
3.1 解释型语言和编译型语言 计算机是不能够识别高级语言的,所以当我们运行一个高级语言程序的时候,就需要一个"翻译机"来从事把高级语言转变成计算机能读懂的机器语言的过程.这个过程 ...
- UICollectionView [NSIndexPath section]: message sent to deallocated instance
在UICollectionView上加UITapGestureRecognizer手势时,点击哪都报 [NSIndexPath section]: message sent to deallocate ...
- LaTex中让页码从正文开始编号
在正文和目录之前这样设置即可 \setcounter{page}{}
- 转载: android 学习架构
http://www.cnblogs.com/forlina/archive/2011/06/29/2093332.html 引言 通过前面两篇: Android 开发之旅:环境搭建及HelloWor ...
- ED/EP系列6《扩展应用》
包括:电子钱包复合应用:电子钱包灰锁应用. 1. 复合应用模式 Ø INITIALIZE FOR CAPP PURCHASE(复合应用消费初始化): Ø UPDATE CAPP DATA CACHE( ...
- android-监听网络状态
1.注册一个广播接收器,监听网络状态private void Start() { IntentFilter filter = new IntentFilter(); filter.addAction( ...