赞
踩
int longestCommonSubsequence(string &A, string &B) {
// case 1
if(A.empty() || B.empty()) return 0;
// case 2
int n = A.size(), m = B.size();
// init null has 0 common subsequence with others
vector<vector<int>> f(n+1, vector<int>(m+1, 0));
for(int i=1; i<=n; ++i){
for(int j=1; j<=m; ++j){
if(A[i-1] == B[j-1])
f[i][j] = max(max(f[i-1][j], f[i][j-1]), f[i-1][j-1]+1);
else{
f[i][j] = max(max(f[i-1][j], f[i][j-1]), f[i-1][j-1]);
}
}
}
return f[n][m];
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。