leetcode@ [72/115] Edit Distance & Distinct Subsequences (Dynamic Programming)
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)的更多相关文章
- (LeetCode 72)Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- [Leetcode 72]编辑距离 Edit Distance
[题目] Given two words word1 and word2, find the minimum number of operations required to convert word ...
- LeetCode(72) Edit Distance
题目 Given two words word1 and word2, find the minimum number of steps required to convert word1 to wo ...
- [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 ...
- [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 ...
- ✡ leetcode 161. One Edit Distance 判断两个字符串是否是一步变换 --------- java
Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到 ...
- [LeetCode#161] One Edit Distance
Problem: Given two strings S and T, determine if they are both one edit distance apart. General Anal ...
- 【leetcode刷题笔记】Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [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 ...
随机推荐
- SGU 180
求逆序数对 归并排序 #include <cstdio> #include <cstring> #include <cmath> #include <a ...
- spring事物的传播行为
1.spring事物的传播行为,主要是用来解决业务层拥有事物的方法,相互调用的问题. 2.声明事物, 在代码执行前,开启事务.代码执行完,提交事务 3.spring并没有提供事务具体的处理,而只是调用 ...
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-001-SpringSecurity简介(DelegatingFilterProxy、AbstractSecurityWebApplicationInitializer、WebSecurityConfigurerAdapter、@EnableWebSecurity、@EnableWebMvcS)
一.SpringSecurity的模块 At the least, you’ll want to include the Core and Configuration modules in your ...
- Android 应用开发性能优化完全分析
1 背景 其实有点不想写这篇文章的,但是又想写,有些矛盾.不想写的原因是随便上网一搜一堆关于性能的建议,感觉大家你一总结.我一总结的都说到了很多优化注意事项,但是看过这些文章后大多数存在一个问题就是只 ...
- knowledge about apache
http://wenku.baidu.com/link?url=6O51BQJdtFRFWDGszKfN3aK7IY92QTCpuc7miBhRLazXvxL5gXb18B_TqIdi3EruX1o_ ...
- CentOS7.1 Xshell 经常掉线 Connection closed by foreign host
XShell如果经常对CentOS掉线,则VNC肯定连接不上 但是ping CentOS的IP又能ping通,主要原因还是因为sshd的设置问题 #进入ssh目录 cd /etc/ssh #修改ssh ...
- Git教程(3)命令行使用git简单示例
基础 Git系统下的的文件有3种状态: 已修改(modified):已修改表示修改了文件,但还没保存到数据库中. 已暂存(staged) : 已暂存表示对一个已修改文件的当前版本做了标记,使之包含在下 ...
- C#中string.Format()和ToString()格式化方法
C#数字格式化输出是我们在编程中经常需要处理的事情,那么这里向你介绍了一些C#数字格式化输出的例子,这样就会方便你来选择和比较,什么方式是比较适合自己项目的. int a = 12345678; C# ...
- MAC 上搭建lua环境
一.下载并安装 (1)最新release版下载地址 http://www.lua.org/ftp/lua-5.3.1.tar.gz (2)编译 Building Lua is implemented ...
- Codeforces 374B - Inna and Nine
原题地址:http://codeforces.com/problemset/problem/374/B 这道题没什么难度,但是考场上就是没写对.Round #220彰显了它的逗比性质——这道题的“标算 ...