最长公共子序列 java实现,动态规划之最长公共子序列Java实现,规划最长序列java,public class...

动态规划之最长公共子序列Java实现,规划最长序列java,public classpublic class lcs { /** * @param args */ public static void main(String[] args) { String str1 = "ACCGGTCGAGTGCGCGGAAGCCGGCCGAA"; String str2 = "GTCGTTCGGAATGCCGTTGCTCTGTAAA"; int m = str1.length(); int n = str2.length(); int[][] c = new int[m+1][n+1]; System.out.println("lcs长度为"+lcs_len(str1,str2,c)); System.out.println("lcs序列为"); print_lcs(str1,str2,c); } public static int lcs_len(String str1,String str2,int[][] c){ if(str1==null||str2==null){ return -1; } int m = str1.length(); int n = str2.length(); for(int i =1;i<=m;i++){ c[i][0]=0; } for(int i=0;i<=n;i++){ c[0][i]=0; } for(int i =1;i<=m;i++){ for(int j =1;j<=n;j++){ if(str1.charAt(i-1) == str2.charAt(j-1)){ c[i][j] = c[i-1][j-1]+1; } else if(c[i-1][j]>=c[i][j-1]){ c[i][j] = c[i-1][j]; } else c[i][j] = c[i][j-1]; } } return c[m][n]; } public static void print_lcs(String str1,String str2,int[][] c){ int k=0; int i=str1.length(); int j=str2.length(); char[] temp = new char[c[i][j]]; while(c[i][j]>0){ if(str1.charAt(i-1) == str2.charAt(j-1)){ temp[k] = str1.charAt(i-1); k++; i--; j--; } else if(c[i][j] == c[i-1][j]) i--; else j--; } for(int l=temp.length-1;l>=0;l--){ System.out.print(temp[l]); } } }运行结果:

lcs长度为20lcs序列为GTCGTCGGAAGCCGGCCGAA

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值