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 ...
随机推荐
- CSS样式基础总结
首行缩进:text-indent:2em 行高:line-height:1.5em 1.5倍行距 也可以设置像素文字修饰:text-decoration:underline下划线 line-throu ...
- Java MD5机密算法的使用
MD5 是常用的加密算法,是不可逆的.既只能加密,但不能解密. package cn.com.ctsi.csdp.base.util; import java.security.MessageDige ...
- ionic ios iframe 白屏
之前碰到一个问题: 在ios下边使用iframe出现白屏问题 android下边正常 原因是ios对app打开外部网页有限制需要取消限制 解决方法 1.确认添加whitelist 插件 2.在conf ...
- swift实现水仙花数
//: Playground - noun: a place where people can play import Cocoa for var a in 1...4{ var b = 4 - a ...
- ubuntu安装/卸载mysql
1.安装mysql root@openstack001:/tmp# apt-get install mysql-server Reading package lists... Done Buildin ...
- juery实现贪吃蛇的游戏
今天用juery做了一个贪吃蛇的游戏,代码比较简陋,不过作为这些天学习juery的成果,非常有成就感.另外关于代码内容如有雷同不胜荣幸. 更改了下 让头和身子的颜色不一样 这样好区分些,虽然还是不怎么 ...
- [译]Object.getPrototypeOf
原文 概要 返回指定object的prototype. 语法 Object.getPrototypeOf(object) 参数 object 要返回原型的对象. 描述 当object参数不是一个对象的 ...
- rpm查询命令摘要
任务 命令 显示软件包的相关信息 rpm -q -i NAME 列出软件包中含有的所有文件 rpm -q -i NAME 列出软件包中含有的配置文件 rpm -q -c NAME 列出软件包中含有的文 ...
- 谈谈混合 App Web 资源的打包与增量更新
综述 移动 App 的运行环境具有带宽不稳定,流量收费,启动速度比较重要等特点,所以混合 App 如何加载 Web 资源并不是一个新问题.本文目的是总结出一种资源打包下载的思路和方案,并且提供一种打包 ...
- Notepad++源码编译及其分析
Notepad++是一个小巧精悍的编辑器,其使用方法我就不多说了,由于notepad++是使用c++封装的windows句柄以及api来实现的,因此对于其源码的研究有助于学习如何封装自己简单的库(当然 ...