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 C

Deletion: * 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题 编辑距离的更多相关文章

  1. 集训第五周动态规划 G题 回文串

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  2. 集训第五周动态规划 D题 LCS

    Description In a few months the European Currency Union will become a reality. However, to join the ...

  3. 集训第五周 动态规划 B题LIS

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Des ...

  4. 集训第五周动态规划 I题 记忆化搜索

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  5. 集训第五周动态规划 H题 回文串统计

    Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...

  6. 集训第五周动态规划 F题 最大子矩阵和

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...

  7. 集训第五周 动态规划 K题 背包

    K - 背包 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  8. 集训第五周动态规划 J题 括号匹配

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  9. 集训第五周动态规划 E题 LIS

    Description The world financial crisis is quite a subject. Some people are more relaxed while others ...

随机推荐

  1. 水题 Codeforces Round #285 (Div. 2) C. Misha and Forest

    题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...

  2. Hadoop YARN学习之组件功能简述(3)

    Hadoop YARN学习之组件功能简述(3) 1. YARN的三大组件功能简述: ResourceManager(RM)是集群的资源的仲裁者, 它有两部分:一个可插拔的调度器和一个Applicati ...

  3. ViewPager讲解以及ViewFlipper

    1.加入ViewPager最好导入<android.support.v4.view.ViewPager>兼容低版本 2.将布局转换为View的方法 3.适配器类型 PagerAdapter ...

  4. json两层解析

    public class Demo { public static void main(String[] args) { try { // 创建连接 服务器的连接地址 URL url = new UR ...

  5. 详解java中staitc关键字

    一.static定义 static是静态修饰符意思,什么叫静态修饰符呢?大家都知道,在程序中任何变量或者代码都是在编译时由系统自动分配内存来存储的,而所谓静态就是指在编译后所分配的内存会一直存在,直到 ...

  6. Farseer.net轻量级开源框架 中级篇:BasePage、BaseController、BaseHandler、BaseMasterPage、BaseControls基类使用

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: UrlRewriter 地址重写 下一篇:Farseer.net轻量级开源框架 中 ...

  7. wps 图片代码 复制 粘贴

    <table><tr><td><img src="C:\Users\Administrator\Desktop\QQ截图20160921180946 ...

  8. mycat+ mysql集群 分库分表

    mycat介绍Mycat数据库分库分表中间件国内最活跃的.性能最好的开源数据库中间件!Mycat关键特性关键特性支持SQL92标准支持MySQL.Oracle.DB2.SQL Server.Postg ...

  9. java线程学习2

    sleep  变为阻塞态  但不释放锁  休眠指定毫秒时间 yield  变为就绪态  可能立即被执行  也可能不立即被执行 join   插队  暂停当前执行的线程  让调用join的线程先执行 线 ...

  10. vue工程化引入组件模板

    vue脚手架搭建好项目后,组件间的引用通过components import bannerComponent from './banner' export default { data(){ retu ...