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 ...
随机推荐
- python内置函数-排列组合函数
product 笛卡尔积 (有放回抽样排列) permutations 排列 (不放回抽样排列) combinations 组合,没有重复 (不放回抽样组合) combinations_with_re ...
- 7款公认比较出色的Python IDE,你值得拥有!
Python作为一款比较“简洁”的编程语言,它拥有很多性价比高的性能,造就了它现在比较火热的局面,很多人都来学习Python.Python 的学习过程少不了 IDE 或者代码编辑器,或者集成的开发编辑 ...
- 使用python3下载网易云音乐歌单歌曲,附源代码
""" 用selenium+PhantomJS配合,不需要进行逆向工程 python 3下的selenium不能默认安装,需要指定版本2.48.0 "" ...
- 51nod 1202 不同子序列个数(计数DP)
1202 子序列个数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 子序列的定义:对于一个序列a=a[1],a[2],......a[n].则非空序列a'=a[p1],a ...
- 牛课第二次多校I
链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 题目描述 White Cloud has a square of n*n from (1,1) ...
- kafka 的offset的重置
最近在spark读取kafka消息时,每次读取都会从kafka最新的offset读取.但是如果数据丢失,如果在使用Kafka来分发消息,在数据处理的过程中可能会出现处理程序出异常或者是其它的错误,会造 ...
- HDU 3032 Nim or not Nim?(Multi_SG,打表找规律)
Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- dcpromo(server2012不支持)
dcpromo 编辑 dcpromo命令是一个“开关”命令.如果Windows 2000 Server计算机是成员服务器,则 运行dcpromo命令会安装活动目录,将其升级为域控制器:如果Window ...
- C语言编程题001
有一颗树,一年两个生长周期,春天它长高一倍,夏天长高1m,问N个周期后树有多高?假设从春天开始树高为1m,第0个周期树高为1m. 要求:1.可以同时输入多个生长周期 如:3//表示下面有几个生长周期 ...
- CTF 两道web整数溢出题目(猫咪银行和ltshop)
①猫咪银行: (2018中科大hackgame) 一开始给十个CTB,而flag需要20个CTB,我们需要理财赚够20个. 理财是只能买入TDSU才可以获得收益.我们先上来直接把CTB全部换成TDSU ...