LeetCode OJ-- Edit Distance **
https://oj.leetcode.com/problems/edit-distance/
动态规划,它的小规模问题是:当 word1 word2都比较小的时候,word1变成 word2需要的变化数。
设数组 record[i][j] 表示word1 从0到 i ,word2 从 0 到 j 的 word1 到 word2 需要经过几次变化。
列等式如下:
record[i][j] = record[i-1][j-1] if 'i'=='j'
record[i][j] = record[i-1][j-1] +1 replace if 'i'!='j'
record[i][j-1] +1 delete
record[i-1][j] +1 insert
取它们3个的最小值。
初始化: record[0][j] = j
record[i][0] = i
class Solution {
public:
int minDistance(string word1, string word2) {
//word1 is smaller
if(word1.size()>word2.size())
{
string temp;
temp = word1; word1 = word2; word2 = temp;
}
if(word1.empty())
return word2.size();
if(word1 == word2)
return ; //find the max same bits
int maxSame = sameBits(word1,word2); return maxSame;
}
int sameBits(string &word1,string &word2)
{
vector<vector<int> > record;
record.resize(word1.size()+);
for(int i = ; i<= word1.size();i++)
record[i].resize(word2.size()+); for(int i = ;i<=word1.size();i++)
record[i][] = i;
for(int j = ;j<=word2.size();j++)
record[][j] = j; for(int row = ;row<= word1.size();row++)
for(int col = ;col<=word2.size();col++)
{
if(word1[row-] == word2[col-])
record[row][col] = record[row-][col-];
else
{
int _min = min(record[row-][col],record[row][col-]);
record[row][col] = min(_min,record[row-][col-]) + ;
}
} return record[word1.size()][word2.size()];
}
};
int main()
{
class Solution mys;
string str1 = "distance";
string str2 = "springbok";
cout<<mys.minDistance(str1,str2);
}
可以 const size_t lenword1 = word1.size()
const size_t lenword2 = word2.size()
int record[lenword1+1][lenword2+1];
这样定义数组
LeetCode OJ-- Edit Distance **的更多相关文章
- [LeetCode] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- Java for LeetCode 072 Edit Distance【HARD】
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- LeetCode One Edit Distance
原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...
- [Leetcode Week8]Edit Distance
Edit Distance 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/edit-distance/description/ Description ...
- 【leetcode】Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- leetCode 72.Edit Distance (编辑距离) 解题思路和方法
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- [LeetCode] 72. Edit Distance(最短编辑距离)
传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...
- LeetCode - 72. Edit Distance
最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...
- 【leetcode】Edit Distance (hard)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- CSAPP 缓冲区溢出试验
缓冲区溢出试验是CSAPP课后试验之一,目的是: 更好的理解什么是缓冲区溢出 如何攻击带有缓冲区溢出漏洞的程序 如何编写出更加安全的代码 了解并理解编译器和操作系统为了让程序更加安全而提供的几种特性 ...
- A1012 The Best Rank (25)(25 分)
A1012 The Best Rank (25)(25 分) To evaluate the performance of our first year CS majored students, we ...
- 解决Uva网站打开慢的问题
https://blog.csdn.net/richenyunqi/article/details/80990535
- OpenCV学习笔记(三) 访问像素
转自:OpenCV如何扫描图像.利用查找表和计时 测试代码:opencv\samples\cpp\tutorial_code\core\how_to_scan_images 测试函数耗时 cv::ge ...
- BZOJ 4971: [Lydsy1708月赛]记忆中的背包
神仙构造 分成x个1和一堆>=w-x的大物品 (x<=20 w>=50) 则拼成w的方案中有且仅有一个大物品 若最终序列中有x个1,有一个大物品为w-k,可以提供C(x,k)种方案 ...
- MySQL之索引(一)
创建高性能索引 索引是存储引擎用于快速找到记录的一种数据结构.这是索引的基本功能.索引对于良好的性能非常关键.尤其是当表中的数据量越来越大时,索引对性能的影响愈发重要.在数据量较小且负载较低时,不恰当 ...
- js 监听后退事件及跳转页面
//直接跳转 window.location.href="b.html"; //返回上一级页面 window.history.back(-1); //返回下一级页面 window. ...
- vrpie在Visio Studio 中无法调试的问题
最近这这几天一直在研究vrpie,之前不能调试,一调试就出问题,打开那个生成的htm文件是没问题的,最初的解决方法是不通过调试来打可那个htm页面,但是这样比较麻烦,因为经常需要和服务器交互,就只能用 ...
- loj2058 「TJOI / HEOI2016」求和
推柿子 第二类斯特林数的容斥表达 fft卡精度就用ntt吧qwq. #include <iostream> #include <cstdio> using namespace ...
- AutoMapper教程
http://www.cnblogs.com/gc2013/p/4487567.html http://www.qeefee.com/article/automapper