题目大意:

给你两个字符串A,B,和以下三种操作:

1.删除一个字符

2.插入一个字符

3.把一个字符改变成另一个字符

求使A变成B所需要的最少的操作;

我刚开始的思路是以为求出最长公共子序列,然后对比A,B的长度做加减,不过WA了一发,

后来想,,可以在这三种操作上做文章,

A[i]==B[j]时

  dp[i][j]=dp[i-1][j-1];

A[i]!=B[j]时:分三种情况

1.插入字符:dp[i][j]=dp[i-1][j]+1;

2.删除字符:dp[i][j]=dp[i][j-1]+1;

3.替换字符:dp[i][j]=dp[i-1][j-1]+1;

代码如下:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f;
#define F(x,y) for(x=0;x<=y;x++)
const int maxn=2e3+;
int dp[maxn][maxn];
int main()
{
string a,b;
while(cin>>a>>b){
memset(dp,,sizeof(dp));
int i,j;
int lena=a.length(),lenb=b.length();
F(i,lena) dp[i][]=i;
F(i,lenb) dp[][i]=i;
F(i,lena)
F(j,lenb)
if(a[i-]==b[j-])
dp[i][j]=dp[i-][j-];
else
dp[i][j]=min(dp[i-][j-],min(dp[i-][j],dp[i][j-]))+;
printf("%d\n",dp[lena][lenb]);
}
return ;
}

这题代码稍微一改,就是  不连续的最长公共子序列http://acm.hdu.edu.cn/showproblem.php?pid=1159;

Edit Distance FZU-1434的更多相关文章

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

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  2. [LeetCode] Edit Distance 编辑距离

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

  3. Edit Distance

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

  4. 编辑距离——Edit Distance

    编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...

  5. LintCode Edit Distance

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

  6. stanford NLP学习笔记3:最小编辑距离(Minimum Edit Distance)

    I. 最小编辑距离的定义 最小编辑距离旨在定义两个字符串之间的相似度(word similarity).定义相似度可以用于拼写纠错,计算生物学上的序列比对,机器翻译,信息提取,语音识别等. 编辑距离就 ...

  7. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  8. 动态规划 求解 Minimum Edit Distance

    http://blog.csdn.net/abcjennifer/article/details/7735272 自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit D ...

  9. One Edit Distance

    Given two strings S and T, determine if they are both one edit distance apart. 分析:https://segmentfau ...

  10. 【leetcode】Edit Distance

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

随机推荐

  1. VS的代码管理工具

    参考文章 可以从中知道修改代码段是要打开相关路径下的文档来修改 官方下载地址 百度云盘 如果项目中国有一些代码重复率非常高,可以把这些代码做成snippet代码段,简化成快捷键命令,加速开发

  2. String 经常用法最优算法实现总结 (一)

    <pre name="code" class="java"><span style="font-family: Arial, Hel ...

  3. 好纠结啊,JeeWx商业版本号和开源版本号有什么差别呢?

    好纠结啊,JeeWx商业版本号和开源版本号有什么差别呢? JeeWx开源版本号是一套基础微信开发平台.有基础的微信菜单.素材管理.微信对接等基础功能,适合于开发人员学习研究. JeeWx商业版本号是一 ...

  4. [Unit Testing] Set the timeout of a Test in Mocha

    Mocha uses a default timeout of 2000 ms. However, if for some reason that does not work for your use ...

  5. js 判断 wifi and 流量

    var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection || { ...

  6. Shell脚本递归打印指定文件夹中全部文件夹文件

    #!/bin/bash #递归打印当前文件夹下的全部文件夹文件. PRINTF() { ls $1 | while read line #一次读取每一行放到line变量中 do [ -d $1/$li ...

  7. 0x5C 数位统计DP

    怎么说,数位DP还是我的噩梦啊,细节太恐怖了. 但是这章感觉又和之前的学的数位DP有差异?(应该是用DP预处理降低时间复杂度,好劲啊,不过以前都是记忆化搜索的应该不会差多少) poj3208 f[i] ...

  8. jQuery右键菜单contextMenu实例

    URL: http://www.cnblogs.com/whitewolf/archive/2011/09/28/2194795.html http://www.blogjava.net/superc ...

  9. systemd实践: 依据情况自动重启服务

    systemd服务异常自动重启很好用,但有的时候希望某些服务只在特定情况下进行重启,其他时候不要自动重启(比如OOM,需要人工介入). 本文抛砖引玉,旨在能够让读者对systemd的重启机制有一定了解 ...

  10. ThinkPHP搜索框需要注意的事项

    1.当搜索成功后需要用到分页的时候,form表单需要用get传参 2.编码方式 当编码方式不正确的时候,使用分页类改变分页,会使搜索框里面的内容乱码 改变编码方式的方法 第一种:header(&quo ...