最长公共子序列问题
作者:互联网
动态规划解决最长公共子序列的问题
动态规划:将大问题分析划分为小问题,解决小问题即解决了大问题
Java 实现代码如下:正确性仍需进一步验证
public static String commonStr(char[] ch1,char[] ch2,int i,int j){ if(i < 0 || j < 0){ return ""; } if(ch1[i] == ch2[j]){ char ch = ch1[i]; return commonStr(ch1,ch2,--i,--j) + ch; }else{ return maxStr(commonStr(ch1,ch2,i,--j),commonStr(ch1,ch2,--i,j)); } } public static String maxStr(String s1,String s2){ return s1.length() > s2.length() ? s1:s2; } public static void main(String[] args) { char[] ch1 = {'b','d','c','a','b','a'}; char[] ch2 = {'a','b','c','b','d','a','b'}; System.out.println(commonStr(ch1,ch2,ch1.length-1,ch2.length-1)); }
标签:commonStr,length,char,ch1,ch2,公共,序列,最长,String 来源: https://www.cnblogs.com/byebai95/p/15846554.html