leetcode 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

首先想到了LCS,但发现忽略了ab,bc这样的测试用例。

正确方法:

    假如我们要将字符串str1变成str2
      sstr1(i)是str1的子串,范围[0到i),sstr1(0)是空串
        sstr2(j)是str2的子串,同上
          d(i,j)表示将sstr1(i)变成sstr2(j)的编辑距离
            首先d(0,t),0<=t<=str1.size()和d(k,0)是很显然的。
              当我们要计算d(i,j)时,即计算sstr1(i)到sstr2(j)之间的编辑距离,
                此时,设sstr1(i)形式是somestr1c;sstr2(i)形如somestr2d的话,
                  将somestr1变成somestr2的编辑距离已知是d(i-1,j-1)
                    将somestr1c变成somestr2的编辑距离已知是d(i,j-1)
                      将somestr1变成somestr2d的编辑距离已知是d(i-1,j)
                        那么利用这三个变量,就可以递推出d(i,j)了:
                          如果c==d,显然编辑距离和d(i-1,j-1)是一样的
                          如果c!=d,情况稍微复杂一点,

                          如果将c替换成d,编辑距离是somestr1变成somestr2的编辑距离 + 1,也就是d(i-1,j-1) + 1
                          如果在c后面添加一个字d,编辑距离就应该是somestr1c变成somestr2的编辑距离 + 1,也就是d(i,j-1) + 1
                          如果将c删除了,那就是要将somestr1编辑成somestr2d,距离就是d(i-1,j) + 1

                          那最后只需要看着三种谁最小,就采用对应的编辑方案了。



                          class Solution {
                          public:
                          /*最优问题,首先选取动态规划和贪心算法。该问题使用动态规划,动态规划列出在做决定的所有选择,找最好的选择*/
                              int minDistance(string word1, string word2) {
                                  int n1=word1.length(),n2=word2.length();
                                  int maxLen=max(n1,n2);
                                  vector<vector<int>> LCS(n1+1,vector<int>(n2+1,0));
                                  int i,j;
                                  for(int j=0;j<=n2;j++){
                                      LCS[0][j]=j;
                                  }
                                  for(int i=0;i<=n1;i++){
                                      LCS[i][0]=i;
                                  }
                                  for(int i=1;i<=n1;i++){
                                      for(j=1;j<=n2;j++){
                                          if(word1[i-1]==word2[j-1]){
                                              LCS[i][j]=LCS[i-1][j-1];
                                          }
                                          else{
                                             int temp=min(LCS[i-1][j],LCS[i][j-1]);
                                              LCS[i][j]=min(temp,LCS[i-1][j-1])+1;
                                          }
                                      }
                                  }
                                  return LCS[n1][n2];
                              }
                          };class Solution {
                          public:
                          /*最优问题,首先选取动态规划和贪心算法。该问题使用动态规划,动态规划列出在做决定的所有选择,找最好的选择*/
                              int minDistance(string word1, string word2) {
                                  int n1=word1.length(),n2=word2.length();
                                  int maxLen=max(n1,n2);
                                  vector<vector<int>> LCS(n1+1,vector<int>(n2+1,0));
                                  int i,j;
                                  for(int j=0;j<=n2;j++){
                                      LCS[0][j]=j;
                                  }
                                  for(int i=0;i<=n1;i++){
                                      LCS[i][0]=i;
                                  }
                                  for(int i=1;i<=n1;i++){
                                      for(j=1;j<=n2;j++){
                                          if(word1[i-1]==word2[j-1]){
                                              LCS[i][j]=LCS[i-1][j-1];
                                          }
                                          else{
                                             int temp=min(LCS[i-1][j],LCS[i][j-1]);
                                              LCS[i][j]=min(temp,LCS[i-1][j-1])+1;
                                          }
                                      }
                                  }
                                  return LCS[n1][n2];
                              }
                          };


                          评论
                          添加红包

                          请填写红包祝福语或标题

                          红包个数最小为10个

                          红包金额最低5元

                          当前余额3.43前往充值 >
                          需支付:10.00
                          成就一亿技术人!
                          领取后你会自动成为博主和红包主的粉丝 规则
                          hope_wisdom
                          发出的红包
                          实付
                          使用余额支付
                          点击重新获取
                          扫码支付
                          钱包余额 0

                          抵扣说明:

                          1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
                          2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

                          余额充值