题目大意:

给你两个字符串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. COGS——C610. 数对的个数

    http://cogs.pro/cogs/problem/problem.php?pid=610 Description出题是一件痛苦的事情!题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的A+B P ...

  2. xtrabackup增量备份mysql +MHA

    http://blog.csdn.net/yanggd1987/article/category/2214421 https://www.centos.bz/2013/09/innobackupex- ...

  3. Object对象具体解释(一)之toString

    Object作为Java中超然的存在.当中定义了一切对象都共同拥有的方法. 特点: 1. java.lang包在使用的时候无需显示导入.编译时由编译器自己主动导入. 2. Object类是类层次结构的 ...

  4. HDU 4535

    裸 的错排.... #include <iostream> #include <cstdio> #include <cstring> #include <al ...

  5. AC自己主动机模板(数组实现版)

    BY 九野 做了一道题,用我的那种写法华丽丽的超时了.,无奈学一学数组实现的 #include<stdio.h> #include<string.h> #include< ...

  6. 生产上数据库大量的latch free 导致的CPU资源耗尽的问题的解决

    中午的时候,我们生产上的某个数据库,cpu一直居高不下 通过例如以下的sql语句,我们查看当时数据库的等待,争用的情况: select s.SID, s.SERIAL#, 'kill -9 ' || ...

  7. luogu1072 Hankson的趣味题

    题目大意 给出数a0, a1, b0, b1,求满足gcd(a0, x)=a1, lcm(b0, x)=b1的x的个数 解法一 枚举b1的因数,看看是否满足上述条件. 怎样枚举因数 试除法.对于1~s ...

  8. spring的bean管理(注解和配置文件混合使用)

    1.建三个类,在一个类中引用其他两个类 import javax.annotation.Resource; import org.springframework.beans.factory.annot ...

  9. C++ 共用体 枚举类型 所有

    一.共用体类型 1.共用体的概念. 有时候需要将几种不同类型的变量存放到同一段内存单元中.例如有三个变量,他们的字节数不同,但是都从同一个地址开始存放.也就是用了覆盖技术,几个变量互相覆盖.这种使几个 ...

  10. Hdu-5992 2016ACM/ICPC亚洲区青岛站 K.Finding Hotels KDtree

    题面 题意:二维平面上有很多点,每个点有个权值,现在给你一个点(很多组),权值v,让你找到权值小于等于v的点中离这个点最近的,相同的输出id小的 题解:很裸的KDtree,但是查询的时候有2个小限制, ...