G面经prepare: Maximum Subsequence in Another String's Order
求string str1中含有string str2 order的 subsequence 的最小长度
DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shortest substring的长度,Int_Max表示不存在
package ShortestSubsequenceIncluding;
public class Solution {
public String findShortest(String a, String b){
if(a==null || b==null || a.length()==0 || b.length()==0) throw new IllegalArgumentException();
int lena = a.length(), lenb = b.length();
int[][] dp = new int[lenb][lena];
for(int i=0; i<lenb; i++){
char bc = b.charAt(i);
for(int j=0; j<lena; j++){
char ac = a.charAt(j);
dp[i][j] = Integer.MAX_VALUE;
if(ac==bc){
if(i==0) dp[i][j] = 1;
else {
for (int t = 0; t < j; t++) {
if (dp[i - 1][t] == Integer.MAX_VALUE) continue;
else dp[i][j] = Math.min(dp[i][j], dp[i - 1][t] + j - t);
}
}
}
}
}
int min = Integer.MAX_VALUE;
int end = -1;
for(int j=0; j<lena; j++){
if(dp[lenb-1][j] < min) {
min = dp[lenb-1][j];
end = j;
}
}
if(end==-1) return "no match!";
return a.substring(end-min+1, end+1);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
String res = sol.findShortest("acbacbc", "abc");
System.out.println(res);
}
}
G面经prepare: Maximum Subsequence in Another String's Order的更多相关文章
- G面经Prepare: Valid Preorder traversal serialized String
求问下各位大神,怎么判断一个按照Preorder traversal serialized的binary tree的序列是否正确呢?不能deserialize成树比如 A) 9 3 4 # # 1 # ...
- 1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)
1, N2N_2N2, ..., NKN_KNK }. A continuous subsequence is defined to be { NiN_iNi, Ni+1N_{i ...
- Maximum Subsequence Sum(接上篇)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- PAT 解题报告 1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- 最大子列和CT 01-复杂度2 Maximum Subsequence Sum
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 连续子数组的最大和/1007. Maximum Subsequence Sum (25)
题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...
- Algorithm for Maximum Subsequence Sum z
MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...
- 数据结构练习 01-复杂度2. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
随机推荐
- java语言中数值自动转换的优先顺序
转换原则:从低精度向高精度转换byte .short.int.long.float.double.char数据类型的转换,分为自动转换和强制转换.自动转换是程序在执行过程中“悄然”进行的转换,不需要用 ...
- 256 terabytes random-access memory
Computer Systems A Programmer's Perspective Second Edition As we will discuss, the extension of IA32 ...
- 【转】深入浅出 JavaScript 中的 this
Java 等面向对象的语言中,this 关键字的含义是明确且具体的,即指代当前对象.一般在编译期确定下来,或称为编译期绑定.而在 JavaScript 中,this 是动态绑定,或称为运行期绑定的,这 ...
- nodejs爬虫
前言 几个月之前,有同事找我要PHP CI框架写的OA系统.他跟我说,他需要学习PHP CI框架,我建议他学习大牛写的国产优秀框架QeePHP. 我上QeePHP官网,发现官方网站打不开了,GOOGL ...
- Solr分页与高亮(使用SolrNet实现)
Solr分页与高亮(使用SolrNet实现) 本节我们使用Asp.net MVC实现Solr客户端查询,建议使用SolrNet这个客户端,开源地址在:https://github.com/mausch ...
- JS实现一个简单的计算器
使用JS完成一个简单的计算器功能.实现2个输入框中输入整数后,点击第三个输入框能给出2个整数的加减乘除.效果如上: 第一步: 创建构建运算函数count(). 第二步: 获取两个输入框中的值和获取选择 ...
- .net加载到vb 进程
.net加载到vb 进程时,总是不能加载进去,什么原因呢? 要尝试三个步骤, 首先调试vb ,没有问题,代码能够调试 然后注册.net的dll,生成tlb文件,生成解决方案,调整附加到进程时的选项. ...
- cname和CDN
http://blog.csdn.net/crazw/article/details/8986504 先说一下DNS的几个基本概念: 一. 根域 就是所谓的“.”,其实我们的网址www.baidu.c ...
- FPGA最小系统分析与电路设计
<FPGA最小系统分析与电路设计> 部分节选自<FPGA应用开发入门与典型.pdf > FPGA最小系统包括:FPGA芯片.下载电路.外部时钟.复位电路和电源. 如果使用NIO ...
- windows server 2008服务器IIS绑定阿里云域名
一.打开Internet 信息服务(IIS)管理器 二.将你的网站放到服务器目录下,比如D盘下的WWW文件夹. 三.在IIS中,添加网站,网站的物理路径指向第二部中创建的网站. 五.在绑定 ...