https://leetcode.com/problems/edit-distance/

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

用dp[i][j] 来表示 长度为 i 的 word1 经过 dp[i][j]次变换 可以得到长度为 j 的word2,那么我们主要考察两种情况,第一种是:word1[i] == word2[j],那么这个问题的规模便转换成了:dp[i][j] = dp[i-1][j-1]. 第二种情况是:word1[i] != word2[j],那么我们可以删除掉word1中的第 i 个字符,或者我们可以把 word1中的第 i 个字符换成与 word2[j] 相同的字符,或者我们还可以同时 删除 word1[i] 和 word2[j]. 于是状态转移方程为:dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1.

class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.length(), n = word2.length(); vector<vector<int> > dp(m+, vector<int> (n+, )); for(int i=;i<=m;++i) dp[i][] = i;
for(int j=;j<=n;++j) dp[][j] = j; for(int i=;i<=m;++i) {
for(int j=;j<=n;++j) {
if(word1[i-] == word2[j-]) {
dp[i][j] = min(dp[i-][j-], dp[i-][j]+);
}
else {
dp[i][j] = min(dp[i-][j], min(dp[i][j-], dp[i-][j-])) + ;
}
}
} return dp[m][n];
}
};

https://leetcode.com/problems/distinct-subsequences/

Given a string S and a string T, count the number of distinct subsequences of T in S.

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).

Here is an example:
S = "rabbbit", T = "rabbit"

Return 3.

解题报告:用 dp[i][[j] 来表示长度为 i 的 S串 包含了几个 T串。分两种情况考虑,第一种:S[i] != T[j],那么问题转而变成求S[1,...i-1] 包含了几个 T[1...i]。因为S[i] 对解的个数不会产生影响。第二种: S[i] == T[j],那么一种可能是 S[1...i-1] 包含了 若干个 T[1...j-1],或者是S[1...i-1] 包含了 若干个T[1...j]。所以状态转移方程为:

dp[i][j] = dp[i-1][j] + dp[i-1][j-1] (if S[i] == T[j])

dp[i][j] = dp[i-1][j] (if S[i] != T[j])

class Solution {
public:
int numDistinct(string s, string t) {
int m = s.length(), n = t.length();
vector<vector<int> > dp(m+, vector<int>(n+, )); for(int i=;i<=m;++i) dp[i][] = ; for(int i=;i<=m;++i) {
for(int j=;j<=n;++j) {
if(s[i-] == t[j-]) dp[i][j] = dp[i-][j-] + dp[i-][j];
else dp[i][j] = dp[i-][j];
}
} return dp[m][n];
}
};

leetcode@ [72/115] Edit Distance & Distinct Subsequences (Dynamic Programming)的更多相关文章

  1. (LeetCode 72)Edit Distance

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

  2. [Leetcode 72]编辑距离 Edit Distance

    [题目] Given two words word1 and word2, find the minimum number of operations required to convert word ...

  3. LeetCode(72) Edit Distance

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

  4. [leetcode]161. One Edit Distance编辑步数为一

    Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...

  5. [LeetCode] 161. One Edit Distance 一个编辑距离

    Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...

  6. ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java

    Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...

  7. [LeetCode#161] One Edit Distance

    Problem: Given two strings S and T, determine if they are both one edit distance apart. General Anal ...

  8. 【leetcode刷题笔记】Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  9. [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

随机推荐

  1. PHP5 session 详解

    http协议是WEB服务器与客户端(浏览器)相互通信的协议,它是一种无状态协议.所谓无状态,指的是不会维护http请求数据,http请求是独立的,非持久的.而越来越复杂的WEB应用,需要保存一些用户状 ...

  2. 在WIN32 DLL中使用MFC

    最近用WIN32 DLL,为了方便要用到MFC的一些库,又不想转工程,就网上找了很多方法,发现没有详细的介绍,有的也行不通,现在成功在WIN32 DLL中使用了MFC,记录一下以防以后用到忘记 一.修 ...

  3. WinDbg调试DMP格式文件

    前言:WinDbg是微软开发的免费源代码级的调试工具.WinDbg可以用于Kernel模式调试和用户模式调试,还可以调试Dump文件.本文的讨论是在安装了Debugging Tools for Win ...

  4. Java发送post请求

    package com.baoxiu.test; import java.io.BufferedReader;import java.io.InputStreamReader;import java. ...

  5. cocos2d-x3.9 默认是 gnustl_static 配置,但是 这个库缺少c++的基础功能... c++_static 功能全面些

    最近的升级Cocos2d-x 3.2正式版.iOS不管是什么程序编译问题,使用结果cocos compile -p android编译APK计划.结果悲剧,出现以下错误. Android NDK: I ...

  6. JVM内存状况查看方法和分析工具

    Java本身提供了多种丰富的方法和工具来帮助开发人员查看和分析GC及JVM内存的状况,同时开源界和商业界也有一些工具可用于查看.分析GC及JVM内存的状况.通过这些分析,可以排查程序中内存泄露的问题及 ...

  7. HDU4907——Task schedule(BestCoder Round #3)

    Task schedule Description有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务.有m个询问,每个询问有一个数字q,表示如 ...

  8. CodeForces152C——Pocket Book(排列组合问题)

    Pocket Book DescriptionOne day little Vasya found mom's pocket book. The book had n names of her fri ...

  9. http_build_query函数(学习)

    http_build_query函数   http_build_query -- 生成 url-encoded 之后的请求字符串 描述 string http_build_query ( array ...

  10. Wince6 RIL层移植

    RIL移植: 因为不同的模组,支持的AT命令有所不同,或是格式不一样,还有就是返回不一样,我们有必要对不同的模组进行RIL驱动移植. 在Response.cpp,Msg.cpp等 文件中ParseXX ...