#include #include #include #include #include #include #include #include using namespace std;
setall_lcs; //注意這裡要用set去除重複的lcs
//最長公共子串(lcs)
//二維陣列veca[i][j]記錄的是兩個字串xi和yj的lcs長度
int lcs_length(const string &str1, const string &str2, vector> &veca)
for (j = 0; j <= str2.length(); j++)
for (i = 1; i <= str1.length(); i++)
else
} }return veca[str1.length()][str2.length()];}
//該函式找出所有的lcs的序列,並將其存在vector中
void printalllcs(string &str1, string &str2, int i, int j,
vector> &veca, string lcs_str)
else
} }cout << " " << lcs_str << endl;
all_lcs.insert(lcs_str);
}int main()
return 0;
}
最長公共子串行 最長公共子串
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...