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 ...
随机推荐
- 能够在子线程绘画的View SurfaceView
转载请注明出处:王亟亟的大牛之路 近期两天都没有写文章,一方面是自己在看书.一方面不知道写什么,本来昨天想写Glide或者RxAndroid的东西结果公司的"狗屎"网怎么都刷不好G ...
- <T extends Serializable>这是什么意思呢?看明白这个,你的问题就自然而然的明白了!
1.转自:https://blog.csdn.net/liwenqiang758/article/details/8131185 自己动手丰衣足食!!! 泛型是Java SE 1.5的新特性,泛型的本 ...
- linux系统在线搭建禅道
1.先安装wget:yum -y install wget 2.下载安装禅道:[root@zhaowen ~]# wget http://dl.cnezsoft.com/zentao/9.0.1/Ze ...
- AJAX复习笔记
AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术.通过在后台与服务器进行少量数据交换,AJAX 可况下更新以使网页实现异步更新. 工作原理: AJAX是基于现有的Internet ...
- IDEA中Lombok插件的安装与使用
背景 我们在开发过程中,通常都会定义大量的JavaBean,然后通过IDE去生成其属性的构造器.getter.setter.equals.hashcode.toString方法,当要对某个属性进行 ...
- Qt5.9 WebChannel
Qt WebChannel enables peer-to-peer communication between a server (QML/C++ application) and a client ...
- Scrapy中的UA池,代理池,以及selenium的应用
UA池 代理池 selenium在Scrapy中的应用 UA池 - 下载中间件: - 下载中间件(Downloader Middlewares) 位于scrapy引擎和下载器之间的一层组件. - 作用 ...
- It Started With A Kiss
- Css 分类 属性 选择器
Css 层叠样式表 美化页面的小工具 分类: 内联 (行内)在标签内部以属性的形式呈现,属性名style 内嵌 head标签内以标签形式呈现,标签名style 外部 head标签内 加link标签 引 ...
- 23个Python爬虫开源项目代码:爬取微信、淘宝、豆瓣、知乎、微博等
来源:全球人工智能 作者:SFLYQ 今天为大家整理了23个Python爬虫项目.整理的原因是,爬虫入门简单快速,也非常适合新入门的小伙伴培养信心.所有链接指向GitHub,祝大家玩的愉快 1.Wec ...