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 nwhere 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

 题意: 把一个字符串经过最少操作步数转为另一个字符串 ——------操作可以是删除插入修改一个字符

dp[i][j]表示A[0-i] B[0-j]相等的最少步数

我们先来对B进行操作

删除的B[j]   :   d[i][j]=d[i][j-1]+1;

在B[j]后面插入一个: d[i][j]=d[i-1][j]+1;

删除一个数 if(B[j]==A[i]) d[i][j]=d[i-1][j-1];

else  d[i][j]=d[i-1][j-1]+1;

求以上三种方法的最大d[i][j];

同理对A[i]操作也方程也是不变的

#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int dp[][];
string a,b;
int n,m;
int Dp(int i,int j){
if(dp[i][j]==-){
int t1=Dp(i-,j)+;
int t2=Dp(i,j-)+;
int t3=Dp(i-,j-)+(a[i-]==b[j-]?:);
dp[i][j]=min(min(t1,t2),t3);
}
return dp[i][j];
}
int main(){
while(cin>>n>>a>>m>>b){
memset(dp,-,sizeof(dp));
int t=max(n,m);
for(int i=;i<=t;i++){
dp[][i]=i;
dp[i][]=i;
}
cout<<Dp(n,m)<<endl;
}
return ;
}

poj3356 AGTC的更多相关文章

  1. POJ3356 – AGTC(区间DP&&编辑距离)

    题目大意 给定字符串X和Y,可以对字符串进行一下三种操作: 1.删除一个字符 2.插入一个字符 3.替换一个字符 每个操作代价是1,问运用以上三种操作把X变为Y所需的最小步数是多少? 题解 定义dp[ ...

  2. POJ 3356 AGTC(最小编辑距离)

    POJ 3356 AGTC(最小编辑距离) http://poj.org/problem?id=3356 题意: 给出两个字符串x 与 y,当中x的长度为n,y的长度为m,而且m>=n.然后y能 ...

  3. POJ 3356.AGTC

    问题简述: 输入两个序列x和y,分别执行下列三个步骤,将序列x转化为y (1)插入:(2)删除:(3)替换: 要求输出最小操作数. 原题链接:http://poj.org/problem?id=335 ...

  4. POJ 3356 AGTC(最长公共子)

    AGTC Description Let x and y be two strings over some finite alphabet A. We would like to transform  ...

  5. POJ 3356 AGTC(DP-最小编辑距离)

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...

  6. poj3356 dp

    //Accepted 4100 KB 0 ms //类似poj1080 //dp[i][j]表示s1用前i个,s2用前j个的最少匹配步数 //dp[i][j]=min(dp[i][j-1]+1,dp[ ...

  7. poj 3356 AGTC(线性dp)

    题目链接:http://poj.org/problem?id=3356 思路分析:题目为经典的编辑距离问题,其实质为动态规划问题: 编辑距离问题定义:给定一个字符串source,可以对其进行复制,替换 ...

  8. POJ 3356 AGTC(DP求字符串编辑距离)

    给出两个长度小于1000的字符串,有三种操作,插入一个字符,删除一个字符,替换一个字符. 问A变成B所需的最少操作数(即编辑距离) 考虑DP,可以用反证法证明依次从头到尾对A,B进行匹配是不会影响答案 ...

  9. Match:DNA repair(POJ 3691)

    基因修复 题目大意:给定一些坏串,再给你一个字符串,要你修复这个字符串(AGTC随便换),使之不含任何坏串,求修复所需要的最小步数. 这一题也是和之前的那个1625的思想是一样的,通过特殊的trie树 ...

随机推荐

  1. bespoke_百度百科

    bespoke_百度百科 bespoke

  2. 将string当字节流使

    string是C++标准定义的字符串类,它不但支持文本,而且支持二进制字节流.给一个string变量赋值有多种方法: 1) 拷贝构造函数 2) 等号赋值函数 3) append成员函数 4) push ...

  3. Java 输出通过 InetAddress 获得的 IP 地址数组

    使用 InetAddress 获取 IP 地址会得到一个 byte 数组 如果你直接输出这个数组,你会发现 IP 地址中的某些位变成了负数 比如 61.135.169.105 会输出成 61.-121 ...

  4. javascript得知 - js的Date对象函数

    js的Date功能很强大.通常使用. 首先获得当前的日期: var CurDate = new Date(); var Year = CurDate.getFullYear(); //年份(四位 例: ...

  5. 类之string类、Math类、DateTime类

    String类 string a = "abcdef123456"; 注:字符串的长度是从0开始计数的如:0,1,2,3,4,5,6,7,8,9........ a.Length; ...

  6. switch语句:适用于一个条件有多个分支的情况---分支语句

    例1: 客服选择功能,然后按按键 Console.WriteLine("查花费请按1,查余额请按2,查流量请按3,办理业务请按4,宽带请按5,人工服务请按6,集团业务请按7"); ...

  7. 使用Atlas实现MySQL读写分离+MySQL-(Master-Slave)配置

    参考博文: MySQL-(Master-Slave)配置  本人按照博友北在北方的配置已成功  我使用的是 mysql5.6.27版本. 使用Atlas实现MySQL读写分离 数据切分——Atlas读 ...

  8. HDU 3899 简单树形DP

    题意:一棵树,给出每个点的权值和每条边的长度, 点j到点i的代价为点j的权值乘以连接i和j的边的长度.求点x使得所有点到点x的代价最小,输出 虽然还是不太懂树形DP是什么意思,先把代码贴出来把. 这道 ...

  9. ASP.NET MVC 5 学习教程:添加控制器

    原文 ASP.NET MVC 5 学习教程:添加控制器 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 通过 ...

  10. UNIX网络编程——网络数据包检测

    网络数据包检测 数据包捕获(sniffer):是指在网络上进行数据收集的行为,需要通过网卡来完成. 三种访问方式: BSD Packet Filter(BPF) SVR4 Datalink Provi ...