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 ...
随机推荐
- Docker installation in sles SP2
Please refer to official site for installation details : https://docs.docker.com/install/linux/doc ...
- [bzoj1552\bzoj2506][Cqoi2014]robotic sort 排序机械臂_非旋转Treap
robotic sort 排序机械臂 bzoj-1552 bzoj-2506 Cqoi-2014 题目大意:给定一个序列,让你从1到n,每次将[1,p[i]]这段区间反转,p[i]表示整个物品权值第i ...
- 洛谷 P2412 查单词
P2412 查单词 题目背景 滚粗了的HansBug在收拾旧英语书,然而他发现了什么奇妙的东西. 题目描述 udp2.T3如果遇到相同的字符串,输出后面的 蒟蒻HansBug在一本英语书里面找到了一个 ...
- C语言可变參实现參数累加返回
C语言可变參的作用真的是很大,自从发表了可变參怎样实现printf.fprintf,sprintf的文章以来.便有不少博友私信问我实现的机制,我也解释了相关的知识点.今天,我们借着这个机会,再来举一个 ...
- JAVA基础实例(一)
1写一个方法,用一个for循环打印九九乘法表 /** *一个for循环打印九九乘法表 */ public void nineNineMultiTable() { for (int i = 1,j = ...
- poj1753,Flip Game,ArrayDeque<Node>
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30449 Accepted: 13232 Descr ...
- hdoj-- Walking Ant
Walking Ant Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total S ...
- hihoCoder-1829 2018亚洲区预选赛北京赛站网络赛 B.Tomb Raider 暴力 字符串
题面 题意:给你n个串,每个串都可以选择它的一个长度为n的环形子串(比如abcdf的就有abcdf,bcdfa,cdfab,dfabc,fabcd),求这个n个串的这些子串的最长公共子序列(每个串按顺 ...
- Python基础:lambda 匿名函数
格式 lambda argument1, argument2,... argumentN : expression square = lambda x: x**2 print(square(2)) 与 ...
- Nginx 配置埋点js日志采集
页面埋点&nginx日志采集 页面(web容器:httpd/nginx负载均衡 + apache server)<===> 日志采集服务器(nginx服务器) 通过某个页面跳转到我 ...