求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的更多相关文章

  1. G面经Prepare: Valid Preorder traversal serialized String

    求问下各位大神,怎么判断一个按照Preorder traversal serialized的binary tree的序列是否正确呢?不能deserialize成树比如 A) 9 3 4 # # 1 # ...

  2. 1007. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

  3. PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)

    1​​, N2N_2N​2​​, ..., NKN_KN​K​​ }. A continuous subsequence is defined to be { NiN_iN​i​​, Ni+1N_{i ...

  4. Maximum Subsequence Sum(接上篇)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

  5. PAT 解题报告 1007. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

  6. 最大子列和CT 01-复杂度2 Maximum Subsequence Sum

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  7. 连续子数组的最大和/1007. Maximum Subsequence Sum (25)

    题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...

  8. Algorithm for Maximum Subsequence Sum z

    MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...

  9. 数据结构练习 01-复杂度2. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

随机推荐

  1. docker安装与启动

    安装docker [root@localhost /]# yum -y install docker-io     更改配置文件 [root@localhost /]# vi /etc/sysconf ...

  2. linux下利用curl监控网页shell脚本

    #!/bin/bash smail() {mail -s "$1" gjw_apparitor@gmail.com <<EOF$1$2====report time: ...

  3. nginx 配置多个二级域名

    server { server_name domain.com www.domain.com *.domain.com ; set $subdomain ''; if ($host ~* (\b(?! ...

  4. pdo封装类

    <?php //http://www.imavex.com/php-pdo-wrapper-class/ class db extends PDO { private $error; priva ...

  5. java FileWriter and FileReader

    import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FWFRD ...

  6. Java反射机制深入研究

    ava 反射是Java语言的一个很重要的特征,它使得Java具体了“动态性”.   在Java运行时环境中,对于任意一个类,能否知道这个类有哪些属性和方法?对于任意一个对象,能否调用它的任意一个方法? ...

  7. docker confluence

    http://wuyijun.cn/shi-yong-dockerfang-shi-an-zhuang-he-yun-xing-confluence/ https://hub.docker.com/r ...

  8. Qt和KDevelop在Linux下安装(qt-x11-commercial-src-4.3.1和kdevelop-3.5.0)

    qt-x11-commercial-src-4.3.1.tar.gz.kdevelop-3.5.0.tar.bz2在Fedora 8下安装. 安装KDevelop:安装KDevelop3.5要求,KD ...

  9. shopping cart<代码>

    i = ["iphone 6000", "bicycle 1000", "coffee 50", "python book 100 ...

  10. Prestashop 页面空白

    Advanced Parameters > Performance页面空白,无任何提示错误,解决方法: 更改文件/cache/class_index.php 权限为666