首先,很感谢仲达兄提供的算法思想,我是用了他的思想,才写出了这个用java求解的动态规划思想求解最长公共字串
仲达兄的博客地址是:http://blog.csdn.net/liufeng_king/article/details/8528858
再次感谢。
最长公共子串跟最长公共子序列是不一样的。最长字串要求要连接在一起,二子序列是不用的
public class LCSStrFromWen {
public static void main(String[] args) {
String S="hellotcl";
String T="tclhel";
System.out.println(getLCS(S,T));
public static String getLCS(String S, String T) {
>//第一步:创建一个二维的字符串数组,用来保存比较S T两个字符串中每一个char的结果
<span style="white-space:pre"> </span>String[][] temp = new String[T.length()][S.length( </span>char charS;
char charT;
int S_len = S.length();int T_len = T.length();
<int max_len = 0;//这个变量用来存储当前最大字串的长度
<span style="white-space:pre"> </span>for (int i = 0; i < T_len; i++) {//用T串去比较S串中的每一个字符
<span style="white-space:pre"> <