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

2021-09-07 18:37:40 字數 557 閱讀 3386

最長公共子串行:

class

solution

for(int i = 0;i <= len2;i++)

for(int i = 1;i <= len1;i++)

}return

result[len1][len2];

}};

最長公共子串

class

solution

for(int i = 0;i <= len2;i++)

for(int i = 1;i <= len1;i++)

}int maxnum = 0x80000000

;

for(int i = 1;i <= len1;i++)

}return

maxnum;

}};

相同:都要建立m+1,n+1的二維陣列

區別:1. 最長公共子串要求連續的,最長公共子串行可以不連續,所以dp的遞推公式第二項不同

2. 最長公共子串行最後乙個值就是最長,最長公共子串要比較哪個位置最長

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

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...