LintCode : 最长公共子序列
来源:互联网 发布:watershed算法 编辑:程序博客网 时间:2024/06/02 21:33
LintCode : 最长公共子序列
给出两个字符串,找到最长公共子序列(LCS),返回LCS的长度。
动态规划。大神July讲的很详细,请大家移步这里。
class Solution {public: /** * @param A, B: Two strings. * @return: The length of longest common subsequence of A and B. */ int longestCommonSubsequence(string A, string B) { // write your code here int m = A.size(); int n = B.size(); vector<vector<int> > dp(m+1, vector<int>(n+1)); for(int i=0; i<=m; i++){ for(int j=0; j<=n; j++){ if(i==0 || j==0){ dp[i][j] = 0; } else{ if (A[i-1] == B[j-1]){ dp[i][j] = dp[i-1][j-1] + 1; } else{ dp[i][j] = max(max(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]); } } } } return dp[m][n]; }};
string A = “bedaacbade”;
string B = “dccaeedbeb”;
dp数组如下:
0 0
- LintCode : 最长公共子序列
- LintCode 最长公共子序列
- 最长公共子序列-LintCode
- lintcode--最长公共子序列
- lintcode-最长公共子序列-77
- LintCode 77 最长公共子序列
- [LintCode] 最长公共子序列 Longest Common Subsequence
- lintcode longest-common-subsequence 最长公共子序列 证明
- [Lintcode]Longest Common Subsequence 最长公共子序列
- LintCode-最长公共子串
- LintCode 最长公共子串
- LintCode : 最长公共子串
- LintCode : 最长公共子串
- LintCode 最长公共子串
- 最长公共子串-LintCode
- lintcode -- 最长公共子串
- 最长公共子序列
- 最长公共子序列
- 20151012
- MyISAM InnoDB 区别
- 为 Zedboard安装Linux系统
- CWebBrowser2去除边框、滚动条、右键菜单
- Impala003-Impala shell
- LintCode : 最长公共子序列
- Linux基础(五)fread, gets,getchar(),pread,getline(),scanf()
- 输入流和输出流同时操作一个文件的问题
- highcharts 坐标轴标签样式调整
- 解读ASP.NET 5 & MVC6系列(10):Controller与Action
- qt error: C2001: 常量中有换行符
- 在Eclipse中使用JUnit4进行单元测试
- 学习html5到那个学校比较好
- iOS AutoLayout 详解