最長公共子子串 java

2021-07-10 18:04:44 字數 1081 閱讀 7250

package maxcommon;

/** * 找到最長公共子串,可以不連續

* agta和aggct最長公共子串為agt

* @author root

*/public class maxcommonunseries

/* * 普通遞迴方法

*/public static string find(char s1, int s1_b, char s2, int s2_b)

if(s1[s1_b]==s2[s2_b])else

result = temp1.length()>temp2.length()?temp1:temp2;

result = result.length()>te***.length()?result:te***;

"temp1:"+temp1+" "+"temp2:"+temp2+" "+"te***:"+te***+" ");

return result;

} /*

* 動態規劃,path的值是[1-i][1-j]的字串重複次數

*/public static string find(char ss1, char ss2)else

}} printmatrix(path, b);

findpath(s1, b, b.length-1, b[0].length-1);

return "";

} /*

* 動態規劃-回溯法找路徑

*/public static void findpath(char s1, int b, int i, int j)

if(b[i][j]==0)else if(b[i][j]==1)else

} /*

* 輸出矩陣

*/public static void printmatrix(int path, int b)

system.out.println();

} system.out.println();

for(int i=1; isystem.out.println();

} }}

最長公共子串行 最長公共子串

1 最長公共子串行 採用動態規劃的思想,用乙個陣列dp i j 記錄a字串中i 1位置到b字串中j 1位置的最長公共子串行,若a i 1 b j 1 那麼dp i j dp i 1 j 1 1,若不相同,那麼dp i j 就是dp i 1 j 和dp i j 1 中的較大者。class lcs el...

最長公共子串行 最長公共子串

1.區別 找兩個字串的最長公共子串,這個子串要求在原字串中是連續的。而最長公共子串行則並不要求連續。2 最長公共子串 其實這是乙個序貫決策問題,可以用動態規劃來求解。我們採用乙個二維矩陣來記錄中間的結果。這個二維矩陣怎麼構造呢?直接舉個例子吧 bab 和 caba 當然我們現在一眼就可以看出來最長公...

最長公共子串 最長公共子串行

子串要求連續 子串行不要求連續 之前的做法是dp求子序列 include include include using namespace std const int inf 0x3f3f3f3f const int mod 1000000007 string s1,s2 int dp 1010 10...