最长公共子序列dp
2012-11-14 11:22
176 查看
public class DP { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String s1 = "abcbdab"; String s2 = "bdcaba"; int m = s1.length(); int n = s2.length(); int [][] c = new int[100][100]; int [][] b = new int[100][100]; LCSLength(s1,s2,b,c,m,n); printLCS(s1,b,m,n); } public static void LCSLength(String s1,String s2,int b[][],int c[][],int m,int n) { for(int i = 0;i<=m;i++) c[i][0]=0; for(int j=1;j<=n;j++) c[0][j]=0; for(int i = 1;i<=m;i++) { for(int j=1;j<=n;j++) { if(s1.charAt(i-1)==s2.charAt(j-1)) { c[i][j] = c[i-1][j-1]+1; b[i][j] =0; } else if(c[i-1][j]>=c[i][j-1]) { c[i][j] = c[i-1][j]; b[i][j] = 1; } else { c[i][j] = c[i][j-1]; b[i][j] =-1; } } } } public static void printLCS(String s1,int b[][],int i,int j) { if(i==0||j==0) { return; } if(b[i][j]==0) { printLCS(s1,b,i-1,j-1); System.out.print(s1.charAt(i-1)); } else if(b[i][j] == 1) { printLCS(s1,b,i-1,j); } else { printLCS(s1,b,i,j-1); } } }
相关文章推荐
- hdu4681 DP,最长公共子序列
- UVA——10405(dp之最长公共子序列)
- 基于DP的LCS(最长公共子序列)问题
- poj 1458 dp [最长公共子序列 O(n*m)]
- 最长公共子序列(dp)
- 暑期dp46道(29) HDOJ 1503 Advanced Fruits 最长公共子序列
- NYOJ - 最长公共子序列(经典dp)
- !HDU 1513 Palindrome--dp--(最长公共子序列模型)
- (Relax DP1.6)UVA 10405 Longest Common Subsequence(使用DP来求解最长公共子序列LCS)
- 最长公共子序列与最长公共子串以及他们的打印(dp)
- POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)
- 暑假集训第四周阶段二DP A - 最长公共子序列
- nyoj36 最长公共子序列(经典dp四)
- POJ 1458 Common Subsequence (DP+LCS,最长公共子序列)
- PKU 1458 Common Subsequence(最长公共子序列,dp,简单)
- 回文字符串【最长公共子序列】【DP】
- nyoj 36 最长公共子序列 dp问题最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共
- UVA - 10723 Cyborg Genes DP:最长公共子序列扩展
- poj——1159(dp之最长公共子序列)
- 最长公共子序列 DP