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. MySQL调优之数据类型选择原则

    本文涉及:高可用数据库设计时数据类型的选择原则 在进行数据库设计时,如果能够选择最恰当的数据类型就可以为后期的数据库调优打好最坚实的基础 选择数据类型的原则 更小的通常更好 例如存储订单状态字段很多时 ...

  2. pycharm版本选择并安装

    pycharm版本选择并安装 一.  初学python最好选择专业版的,因为经测试社区版的很多插件都没有安装,如果自己安装的话会非常麻烦,而且很多根本就安装不上,比如说css,javascript,d ...

  3. [ZOJ1961]Let it Bead

    Description "Let it Bead" company is located upstairs at 700 Cannery Row in Monterey, CA. ...

  4. [GZOI2016] 亚索的量子实验【分块】

    第二题 亚索的粒子实验 [问题描述] 亚索是一名伟大的科学家,他最近在做一个粒子的实验,粒子初始有一定的能量,实验过程中倘若第i个粒子被注入k能量,那该粒子就会增加k能量,同时由于辐射作用,第2i,3 ...

  5. 模拟/字符串处理 UVALive 6833 Miscalculatio

    题目传送门 /* 模拟/字符串处理:主要是对*的处理,先把乘的预处理后再用加法,比如说是:1+2*3+4 = 1+..6+4 = 11 */ #include <cstdio> #incl ...

  6. Android偏好设置(4)设置默认值

    Setting Default Values The preferences you create probably define some important behaviors for your ...

  7. 转-用Eclipse 开发Dynamic Web Project应用程序

    简介:本文仅简单介绍基于Eclipse开发Dynamic Web Project应用下的JSP,Servlet及TOMCAT数据源的配置和开发. 软件环境: Eclipse Java EE IDE f ...

  8. Java-超市购物小票案例-详细介绍

    1.1  超市购物购物小票需求分析 用户循环进行三个操作: 1.输入购买数量,代表为所购买货物的数量赋值,从而计算每项商品金额 2.打印小票,将已有数据打印 3.退出系统(因为该程序为循环操作,无法终 ...

  9. 帮助新手理解equals和hashCode

    入行快要两年,偶尔想起来equals和hash还是会有些晕,索性今天就更深入的弄明白一些,不足之处也请各位大神指出批评,共同进步. 刚开始学java的时候只是记忆性的来背,如果一个类在程序中可能进行比 ...

  10. 完美单例宏定义(兼容ARC和MRC),项目中可以直接使用

     单例模式: 1.永远只分配一块内存来创建对象 2.提供一个类方法, 返回内部唯一的一个对象(一个实例) 3.最好保证init方法也只初始化一次 写一个宏定义文件,传入宏定义函数名,自动生成符合类名的 ...