直接上**,需要使用時直接呼叫即可。
//求字串的最長公共子串
public
class
maxstringdemo
*/public
static
intset(string aa, string bb)
public
static
stringbuilder maxutil2(string str1, string str2)
//初始化二維矩陣中的第一列
for (int j = 0; j < arr1.length; j++)
//巢狀for迴圈:比較二維矩陣中每個點對應行列字元中否相等,相等的話值設定為1,否則設定為0
for (int i = 1; i < arr1.length; i++)
} else
temp[i][j] = 0;}}
//求出最長公共子串的起始點
start=end-length+1;
stringbuilder sb=new
stringbuilder();
//通過查詢出值為1的最長對角線就能找到最長公共子串
for (int j = start; j < end+1; j++)
return
sb; }
}
求兩個字串的最長公共子串
問題 有兩個字串str和str2,求出兩個字串中最長公共子串長度。比如 str acbcbcef,str2 abcbced,則str和str2的最長公共子串為bcbce,最長公共子串長度為5。演算法思路 1 把兩個字串分別以行和列組成乙個二維矩陣。2 比較二維矩陣中每個點對應行列字元中否相等,相等的...
求兩個字串的最長公共子串
def longestcommonsequence str one,str two,case sensitive true str one 和 str two 的最長公共子串行 param str one 字串1 param str two 字串2 正確結果 param case sensitive...
求兩個字串的最長公共子串
採用乙個二維矩陣來記錄中間結果,矩陣的橫座標為字串1的各個字元,矩陣的縱座標為字串2的各個字元。舉例說明 假設兩個字串分別為 bab 和 caba 當然我們現在一眼就可以看出來最長公共子串是 ba 或 ab b a b c 0 0 0 a 0 1 0 b 1 0 1 a 0 1 0 可以看出,矩陣的...