Title:

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

a) Insert a character
b) Delete a character
c) Replace a character

这题仍可以使用动态规划,问题是,如何得到转移方程。

if a[i] == b[j] then d[i,j] = d[i-1,j-1]

但是不相等的情况下如何计算呢

题目给出了三种可能方式,结果就是根据这三种方式进行

if a[i] != b[j] then min(

a[i-1,j]//相当于删除a[i-1]

a[i,j-1]//相当于插入a的末尾,插入的为b的末尾,这样a,b的末尾相等,所以,要同时减1

a[i-1,j-1]//相当于替换

)

https://blog.csdn.net/zxzxzx0119/article/details/82054807

int minDistance(string word1,string word2){
int m = word1.size();
int n = word2.size();
vector<vector<int> > result(m+,vector<int>(n+));
for (int i = ; i <= m ; i++)
result[i][] = i;
for (int j = ; j <= n ; j++)
result[][j] = j;
for (int i = ; i < m; i++){
for (int j = ; j < n ; j++){
if (word1[i] == word2[j])
result[i+][j+] = result[i][j];
else
result[i+][j+] = min(result[i][j+],min(result[i+][j],result[i][j]) )+; }
}
return result[m][n];
}

其他相关问题:

(1)

最长公共字串(连续)

string a= "abcdef";

string b = "abdef";

可以使用动态规划来解决,使用一个二维数组,状态d[i,j]表示到a[i]和b[j]的最长公共字串,这样问题就是要找出状态转移方程。

如果a[i] = b[j] 那么,d[i,j] = d[i-1,j-1]+1

如果a[i] != b[j] 那么 ,d[i,j] = 0

最后再遍历一下数组,来找出最大的字串。

优化,首先,遍历找出最大字串这一步可以放到计算过程中。

string LCS(string s1, string s2){
int len1 = s1.length();
int len2 = s2.length();
int maxLength = ;
int index = ;
int table[][];
for (int i = ; i < len1+ ; i++)
table[i][] = ;
for (int i = ; i < len2+ ; i++)
table[][i] = ;
for (int i = ; i <= len1 ; i++){
for (int j = ; j <= len2 ; j++){
if (s1[i-] == s2[j-]){
table[i][j] = table[i-][j-] + ;
}else{
table[i][j] = ;
//table[i][j] = (table[i-1][j] > table[i][j-1]) ? table[i-1][j] : table[i][j-1];
}
if (table[i][j] > maxLength ){
maxLength = table[i][j];
index = i;
} }
}
return s1.substr(index-maxLength,maxLength);
}

例外,一般的动态规划的计算空间都可以降低。将二维空间降至一维空间。

降维对于j一般是正序和逆序,关键是看,如果在计算过程中j-1会被提前计算,则要以相反的顺序进行。比如上面,状态转移是

table[i][j] = table[i-1][j-1] + 1;
如果j是从0 到 len2进行,那么table[j-1]就会被先计算,可是从状态转移我们知道,应该在计算table[j]时,这一行的table[j-1]仍是上一行的,所以应该倒过来进行。
string LCS_continue(string s1,string s2){
int len1 = s1.size();
int len2 = s2.size();
vector<int> result(len2+);
int longest = ;
int index = ;
for (int i = ; i < len2+; i++)
result[i] = ;
for (int i = ; i < len1; i++){
for (int j = len2- ; j >=; j--){
if (s1[i] == s2[j]){
cout<<i<<" "<<j<<endl;
result[j+] = result[j]+;
}else{
result[j+] = ;
}
if (result[j+] > longest){
longest = result[j+];
index = j+;
}
}
}
return s2.substr(index-longest,longest);
}

(2)公共最长子序列(非连续)

非连续的状态转移也很容易得到。

d[i,j] = d[i-1,j-1]+1 (a[i] == b[j])

d[i,j] = max(d[i-1,j],d[i,j-1]) (a[i] != b[j])

同样,在降维的时候,j仍是要逆序进行。

int LCS_not_continue(string s1,string s2){
int len1 = s1.size();
int len2 = s2.size();
vector<int> result(len2+);
for (int i = ; i < len2+; i++)
result[i] = ;
for (int i = ; i < len1; i++){
for(int j = len2- ; j >= ; j--){
if (s1[i] == s2[j]){
result[j+] = result[j]+;
}else{
result[j+] = max(result[j],result[j+]);
}
}
}
return result[len2];
}

(3)最长上升子序列

对于一个序列如1,-1,2,-3,4,-5,6,-7,其最长第增子序列为1,2,4,6

定义递推关系:

dp[i]: 以a_i 为末尾的最长上升子序列的长度

dp[i] = max(1,dp[j]+1) (j < i && a[j] < a[i])

#include <iostream>
#include <vector>
using namespace std; class Solution{
public:
int LIS(vector<int> nums){
vector<int> v(nums.size()+,);
v[] = ;
int result = INT_MIN;
for (int i = ; i < nums.size(); i++){
for (int j = ; j < i; j++){
if (nums[j] < nums[i])
v[i+] = max(v[i+],v[j+]+);
}
/*for (int j = 0; j < i+1; j++){
if (j-1 >= 0 && nums[j-1] < nums[i])
v[i+1] = max(v[i+1],v[j]+1);
}*/
result = max(result,v[i+]);
}
return result;
}
};
int main(){
int a[] = {,,,,};
int size = sizeof(a)/sizeof(int);
vector<int> nums(a,a+size);
Solution solution;
cout<<solution.LIS(nums);
system("pause");
}

LeetCode: Edit Distance && 子序列题集的更多相关文章

  1. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  2. Leetcode:Edit Distance 解题报告

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  3. [leetcode]Edit Distance @ Python

    原题地址:https://oj.leetcode.com/problems/edit-distance/ 题意: Given two words word1 and word2, find the m ...

  4. [LeetCode] Edit Distance 字符串变换为另一字符串动态规划

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  5. Leetcode Edit Distance

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  6. [LeetCode] Edit Distance(很好的DP)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  7. LeetCode——Edit Distance

    Question Given two words word1 and word2, find the minimum number of steps required to convert word1 ...

  8. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  9. LeetCode One Edit Distance

    原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...

随机推荐

  1. gcd,lcm,ext_gcd,inv

    Least Common Multiple http://acm.hdu.edu.cn/showproblem.php?pid=1019 #include<cstdio> int gcd( ...

  2. 关于struts2如何去掉默认的后缀(.action)

    struts2是可以配置默认的后缀名的,如http://localhost:8080/test.action,这个是默认的,但是也可以通过配置去修改这个.action为别的. 这里是通过一个常量配置改 ...

  3. linux 下 apache启动、停止、重启命令

    基本的操作方法: 本文假设你的apahce安装目录为/usr/local/apache2,这些方法适合任何情况 apahce启动命令: 推荐/usr/local/apache2/bin/apachec ...

  4. lintcode:Fibonacci 斐波纳契数列

    题目: 斐波纳契数列 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: 前2个数是 0 和 1 . 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列的前10个数字是: 0, ...

  5. [hankerrank]Counter game

    https://www.hackerrank.com/contests/w8/challenges/counter-game 关键是二分查找等于或最接近的小于target的数.可以使用和mid+1判断 ...

  6. 【Linux常识篇(1)】所谓的正向代理与反向代理

    正向代理的概念 正向代理,也就是传说中的代理,他的工作原理就像一个跳板,简单的说,我是一个用户,我访问不了某网站,但是我能访问一个代理服务器,这个代理服务器呢,他能访问那个我不能访问的网站,于是我先连 ...

  7. C# DataGridView控件 动态添加新行

    DataGridView控件在实际应用中非常实用,特别需要表格显示数据时.可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行.假如需要动态为DataGridView控件添加新行, ...

  8. c扩展调用php的函数(调用实现php函数的c函数)

    上一次是写的c扩展调用c的标准函数,但是只能调用头文件中申明的函数,今天来说下c扩展调用实现php函数的c函数,比方说,c扩展要用到php中ip2long这个函数,但是c不可能去php中调用,肯定是去 ...

  9. yeoman错误提示

    运行 yo angular 出现如下提示: $ yo angular grunt-cli: The grunt command line interface. (v0.1.9) Fatal error ...

  10. C结构体之位域(位段)

    C结构体之位域(位段) 有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可.为了节省存储空间,并使处理简便,C ...