LintCode 77 最长公共子序列
来源:互联网 发布:淘宝客用红包购物券 编辑:程序博客网 时间:2024/05/21 00:00
题目:longestCommonSubsequence
要求:
给出两个字符串,找到最长公共子序列(LCS),返回LCS的长度。
样例:
给出”ABCD” 和 “EDCA”,这个LCS是 “A” (或 D或C),返回1
给出 “ABCD” 和 “EACB”,这个LCS是”AC”返回 2
算法要求:
无
解题思路:
dp[i][j]数组存的是从str[0]到str[i]与str2[0]到str2[j]之间公共的子序列的长度
算法如下:
int longestCommonSubsequence(string str, string str2) { // write your code here int dp[101][101] = {0}; int size = str.length(); int size2 = str2.length(); for (int i = 0; i < size; i++) { for (int j = 0; j < size2; j++) { if (str[i] == str2[j]) { dp[i + 1][j + 1] = dp[i][j] + 1; } else { dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]); } } } return dp[size][size2]; }
阅读全文
0 0
- lintcode-最长公共子序列-77
- LintCode 77 最长公共子序列
- LintCode : 最长公共子序列
- LintCode 最长公共子序列
- 最长公共子序列-LintCode
- lintcode--最长公共子序列
- [LintCode] 最长公共子序列 Longest Common Subsequence
- lintcode longest-common-subsequence 最长公共子序列 证明
- [Lintcode]Longest Common Subsequence 最长公共子序列
- LintCode-最长公共子串
- LintCode 最长公共子串
- LintCode : 最长公共子串
- LintCode : 最长公共子串
- LintCode 最长公共子串
- 最长公共子串-LintCode
- lintcode -- 最长公共子串
- 最长公共子序列
- 最长公共子序列
- linux后端诊断与调试技术
- 设置div背景颜色透明度,内部元素不透明
- #比特币病毒#来袭,教你关闭445端口(附打开方法)
- 数据库连接池--c3p0
- STM32之USB(1)
- LintCode 77 最长公共子序列
- 【大雄游戏API接口】寄予厚望的HTML5技术
- MFC中添加变量为control和value的区别
- JS语言精粹
- python3_访问url、json、读写文件
- sublime text2注册码
- vs2013和15下编译matio库文件
- Tensorflow简介
- 把系统配置写入文件做文件配置