集训第五周动态规划 C题 编辑距离
Description
Let x and y be two strings over some finite alphabet A. We would like to transform x into y allowing only operations given below:
- Deletion: a letter in x is missing in y at a corresponding position.
- Insertion: a letter in y is missing in x at a corresponding position.
- Change: letters at corresponding positions are distinct
Certainly, we would like to minimize the number of all possible operations.
Illustration
A G T A A G T * A G G C
| | | | | | |
A G T * C * T G A C G CDeletion: * in the bottom line
Insertion: * in the top line
Change: when the letters at the top and bottom are distinct
This tells us that to transform x = AGTCTGACGC into y = AGTAAGTAGGC we would be required to perform 5 operations (2 changes, 2 deletions and 1 insertion). If we want to minimize the number operations, we should do it like
A G T A A G T A G G C
| | | | | | |
A G T C T G * A C G C
and 4 moves would be required (3 changes and 1 deletion).
In this problem we would always consider strings x and y to be fixed, such that the number of letters in x is m and the number of letters in y is n where n ≥m.
Assign 1 as the cost of an operation performed. Otherwise, assign 0 if there is no operation performed.
Write a program that would minimize the number of possible operations to transform any string x into a string y.
Input
The input consists of the strings x and y prefixed by their respective lengths, which are within 1000.
Output
An integer representing the minimum number of possible operations to transform any string x into a string y.
Sample Input
10 AGTCTGACGC
11 AGTAAGTAGGC
Sample Output
4 经典的LIS变种,编辑距离
很显然这道题使用一般的方法是做不出来的,因为这道题要求输出的操作数最少,每一步的方法都应该最优。
所以DP
状态表示:dp[i][j]表示两个字符串
最优子结构:dp[i][j]表示从a[i]到b[j]完全匹配的最小操作数
状态转移方程:1.dp[i][j]=dp[i-1][j-1] (a[i]=b[j]) //相等无需变化,因此操作数也不增加
2.dp[i][j]=min{dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+1} (a[i]!=b[j]) //不相等还要考虑替换,插入操作
3.dp[i][0]=i,dp[0][i]=i //这是初始化步骤,这符合规律,因为这种情况下只能执行删除操作,而这也是动态规划往后扩展的基石
#include"iostream"
#include"cstdio"
using namespace std; const int maxn=; int m,n,len,ans;
char a[maxn],b[maxn];
int dp[][]; void Work()
{
len=max(m,n);
for(int i=;i<=len;i++)
{
dp[i][]=i;
dp[][i]=i;
}
for(int i=;i<=m;i++)
{
for(int j=;j<=n;j++)
{
dp[i][j]=min(dp[i-][j],dp[i][j-])+;
if(a[i]==b[j])
dp[i][j]=dp[i-][j-];
else
dp[i][j]=min(dp[i][j],dp[i-][j-]+);
}
}
ans=dp[m][n];
} void Print()
{
cout<<ans<<endl;
} int main()
{
while(~scanf("%d %s",&m,a+))
{
scanf("%d %s",&n,b+);
Work();
Print();
}
return ;
}
O(OO)O
集训第五周动态规划 C题 编辑距离的更多相关文章
- 集训第五周动态规划 G题 回文串
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
- 集训第五周动态规划 D题 LCS
Description In a few months the European Currency Union will become a reality. However, to join the ...
- 集训第五周 动态规划 B题LIS
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Des ...
- 集训第五周动态规划 I题 记忆化搜索
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- 集训第五周动态规划 H题 回文串统计
Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...
- 集训第五周动态规划 F题 最大子矩阵和
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...
- 集训第五周 动态规划 K题 背包
K - 背包 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- 集训第五周动态规划 J题 括号匹配
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- 集训第五周动态规划 E题 LIS
Description The world financial crisis is quite a subject. Some people are more relaxed while others ...
随机推荐
- 【正睿多校联盟Day4 T4 简单的数论题】
题目名有毒 由于并没有系统地开始学习数论,所以数论题基本靠暴力. 然鹅本题的题解相当简单: emmm....我当你没说 一个简单易懂的方法是这样的: 1. 欧拉定理的推论 若正整数a,n互质,则对于任 ...
- 《Windows核心编程系列》九谈谈同步设备IO与异步设备IO之同步设备IO
同步设备IO 所谓同步IO是指线程在发起IO请求后会被挂起,IO完成后继续执行. 异步IO是指:线程发起IO请求后并不会挂起而是继续执行.IO完毕后会得到设备的通知.而IO完成端口就是实现这种通知的很 ...
- QT5之2D绘图-绘制路径
在绘制一个复杂的图形的时候,如果你需要重复绘制一个这样的图形,就可以使用到QPainterPath类,然后使用QPainter::drawPath()来进行绘制. QPainterPath类为绘制操作 ...
- ACM_数数?诶?这么简单?
数数?诶?这么简单? Time Limit: 2000/1000ms (Java/Others) Problem Description: 当看到GDUFE-GAME宣传海报上提到"场内人员 ...
- Linux环境下MySQL5.7安装记录
参考文档 <Installing MySQL on Unix/Linux Using Generic Binaries> https://dev.mysql.com/doc/refman/ ...
- Tenegrad评价函数 分类: 图像处理 Opencv 2014-11-12 20:46 488人阅读 评论(0) 收藏
Tenegrad函数式一种常用的图像清晰度评价函数,是一种基于梯度的函数. 在图像处理中,一般认为对焦好的图像具有更尖锐的边缘,故具有更大的梯度函数值. Tenegrad函数使用Sobel算子提取水平 ...
- ASP.NET Core MVC使用MessagePack配合前端fetch交换数据
1.安装Nuget包 - WebApiContrib.Core.Formatter.MessagePack https://www.nuget.org/packages/WebApiContrib.C ...
- AJPFX对equals()方法和==异同的比较
equals()方法是Object类的方法,所有的类都集成了此方法,还有部分类重写了这个方法,我们看一下Object类中关于该方法的的源码: public boolean equals(Object ...
- 自学 iOS - 三十天三十个 Swift 项目 第二天
继续做仿造着别人的第二个 1.首先下载 一些字体 网上搜索 "造字工房" 2.把下载的相应字体文件放到工程之中,就Ok了 不多说 效果如下 可以下面这个方法 检索项目里面所有的字体 ...
- shutil模块 + shelve模块 二合一版
其他的看我前面的博客 import shutil # 将文件内容拷贝到另一个文件with open('old.xml','r') as read_f,open('new.xml', 'w') as w ...