51nod1183(Edit Distance)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1183
题意:中文题啦~
思路:dp
用dp[i][j]表示从第一个字符开始,长度分别为i, j的两个字符串的编辑距离;
那么我们很容易能想到初始化为:dp[0][j]=j, dp[i][0]=i;
对于dp[i][j](其是a[0...i-1], b[0...j-1]的编辑距离),我们可以由三种情况得到:
1. 假设我们已经知道了 dp[i-1][j] 即 a[0...i-2], b[0...j-1]的编辑距离,那么我们只要往a[0...i-2]中再添加一个字符就能得到a[0...i-1], 所以dp[i][j]=dp[i-1][j]+1;
2. 假设我们已经知道了 dp[i][j-1] 即 a[0...i-1], b[0...j-2]的编辑距离,那么我们只要往b[0...j-2]中再添加一个字符就能得到b[0...j-1], 所以dp[i][j]=dp[i][j-1]+1;
3.1. 假设我们已经知道了 dp[i-1][j-1] 即 a[0...i-2], b[0...j-2]的编辑距离,如果a[i-1]=b[j-1], 那么dp[i][j]=dp[i-1][j-1], 如果a[i-1]!=b[j-1],那么我们需要改变a[i-1]
使其等于b[j-1],所以dp[i][j]=dp[i-1][j-1]+1;
所以我们得到状态转移方程式 :dp[i][j]=min(dp[i-1][j-1]+(a[i-1]==b[j-1]?0:1), min(dp[i-1][j]+1, dp[i][j-1]+1));
代码:
#include <bits/stdc++.h>
#define MAXN 1010
using namespace std; int dp[MAXN][MAXN]; //dp[i][j]表示从第一个字符开始,长度分别为i, j的两个字符串的编辑距离
char a[MAXN], b[MAXN]; int main(void){
scanf("%s%s", a, b);
int lena=strlen(a), lenb=strlen(b);
for(int i=; i<=lena; i++){ //初始化
dp[i][]=i;
}
for(int i=; i<=lenb; i++){ //初始化
dp[][i]=i;
}
for(int i=; i<=lena; i++){
for(int j=; j<=lenb; j++){
dp[i][j]=min(dp[i-][j-]+(a[i-]==b[j-]?:), min(dp[i-][j]+, dp[i][j-]+));
}
}
printf("%d\n", dp[lena][lenb]);
return ;
}
51nod1183(Edit Distance)的更多相关文章
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 编辑距离——Edit Distance
编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...
- LintCode Edit Distance
LintCode Edit Distance Given two words word1 and word2, find the minimum number of steps required to ...
- stanford NLP学习笔记3:最小编辑距离(Minimum Edit Distance)
I. 最小编辑距离的定义 最小编辑距离旨在定义两个字符串之间的相似度(word similarity).定义相似度可以用于拼写纠错,计算生物学上的序列比对,机器翻译,信息提取,语音识别等. 编辑距离就 ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- 动态规划 求解 Minimum Edit Distance
http://blog.csdn.net/abcjennifer/article/details/7735272 自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit D ...
- One Edit Distance
Given two strings S and T, determine if they are both one edit distance apart. 分析:https://segmentfau ...
随机推荐
- 从底层谈,WebGIS 原理、设计、实现
留待备用! http://www.cnblogs.com/naaoveGIS/category/600559.html 介绍与WebGIS相关的各种原理知识,以及基于原理知识上的程序设计和实现. (一 ...
- pgsql 建数据库注意事项
在用navacat建好表之后,需要主键自增的时候,把字段建好之后,可以使用下面的sql来建立主键自增. ALTER TABLE "public"."chart_sql&q ...
- Linux下Redis的安装和部署
一.Redis介绍 Redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcache类似,但很大程度补偿了Memcache的不足,它支持存储的value类型相对更多 ...
- Ubuntu 14.04 安装SSH
1.一般我们安装好ubuntu系统后,首先就是更换国内的ubuntu源,使得更新及安装软件速度更快 sudo cp /etc/apt/sources.list /etc/apt/sources.lis ...
- Php compiler for .NET framework
https://phalanger.codeplex.com http://tomasp.net/blog/ducktyping-in-phalaner.aspx/ https://visualstu ...
- thinkphp3.22 多项目配置
1.index.php if(version_compare(PHP_VERSION,'5.3.0','<')) die('require PHP > 5.3.0 !'); // 开启调试 ...
- 【.net core 跨平台】第一步 在Ubuntu16.04 配置.net core环境
本次使用VMware10.0.4工具安装Ubuntu16.04系统并配置.net core环境 Ubuntu 16.04 desktop下载地址:http://releases.ubuntu.co ...
- eclipse配置tomcat
1eclipse默认是用workspace的comcat,要把它配置成我们自己的外部tomcat,并且发布路径修改为webapps 2设置tomcat的启动和关闭时间 3如果要发布到tomcat根目录 ...
- [Machine Learning & Algorithm]CAML机器学习系列1:深入浅出ML之Regression家族
声明:本博客整理自博友@zhouyong计算广告与机器学习-技术共享平台,尊重原创,欢迎感兴趣的博友查看原文. 符号定义 这里定义<深入浅出ML>系列中涉及到的公式符号,如无特殊说明,符号 ...
- [Machine Learning] 梯度下降法的三种形式BGD、SGD以及MBGD
在应用机器学习算法时,我们通常采用梯度下降法来对采用的算法进行训练.其实,常用的梯度下降法还具体包含有三种不同的形式,它们也各自有着不同的优缺点. 下面我们以线性回归算法来对三种梯度下降法进行比较. ...