Edit Distance FZU-1434
题目大意:
给你两个字符串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的更多相关文章
- [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 ...
- 【leetcode】Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
随机推荐
- JAVA:将反射技术应用于工厂模式(Factory)和单例模式(Singleton)的简单代码
反射技术大量用于Java设计模式和框架技术,最常见的设计模式就是工厂模式(Factory)和单例模式(Singleton). 参考URL: http://blog.csdn.net/xiaohai79 ...
- centos 解压压缩包到指定目录
解压.tar.gz文件: tar -zxvf web.tar.gz tar不支付解压文件到指定的目录! 解压.war .zip文件到指定目录: unzip web.war -d webapps/ROO ...
- fputs与fgets
1. fputs 函数名: fputs 功 能: 送一个字符到一个流中 用 法: int fputs(char *string, FILE *stream); 说明: fputs是一 ...
- Php socket数据编码
bytes.php 字节编码类 /** * byte数组与字符串转化类 * @author * created on 2011-7-15 */ class bytes { /** * 转换一个str ...
- tensorflow利用预训练模型进行目标检测(四):检测中的精度问题以及evaluation
一.tensorflow提供的evaluation Inference and evaluation on the Open Images dataset:https://github.com/ten ...
- Android常用的一些make命令【转】
本文转载自:http://blog.csdn.net/liuxd3000/article/details/39181377 1.make -jX X表示数字,这个命令将编译Android系统并生成镜 ...
- Battle City
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7208 Accepted: 2427 Descr ...
- findContours 轮廓查找
物体的轮廓勾勒出了物体的整体形状,物体形状的边界像素一起组合成了轮廓. 灰度图像边界的明显特征是边界两侧灰度级的突变,根据这个特征,使用Sobel.拉普拉斯或Canny之类的边缘检测算子可以有效的检测 ...
- thinkphp 内存查询表 防止多次查库
//从内存查询 表 以防止多次查库 private static function selectTable($tableName,array $where,$getFirst=false){ $res ...
- js设计模式-门面模式
适用场景:门面模式在DOM脚本编程这种需要对各种不一致的浏览器接口的环境中很常用. 例子:阻止模式事件 var DED = widow.DED || {}; DED.util = { stopProp ...