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

题意:
每组数据给出两个字符串x、y,通过删除或添加或替换三种操作把两个字符串变得相同。
求最少操作次数。 思路:
可以先定义一个dp[n+20][m+20]的二维数组,要明白题目求的是什么,根据需要去写题,而且要时刻记住题目求的是什么,自己定义的东西的具体含义到底是什么。
dp[i][j]代表,从长度为i的a数组到长度为j的b数组最少变换次数。
经过分析,可以得出来一个思想——同化思想,删除和添加可以看作是一种操作,比如a串可以通过添加'c'这个字符变得和b串一样,那也就说明b数组可以通过删除操作,删除'c'变得和a
串一样,所以可以说明通过添加操作和删除操作所需操作次数是确定的,
所以题目可以转换成,通过添加或者替换操作,最少把a、b串变的相同需要多少步。 注意:
a[i]!=b[i],是逻辑表达式,只有两个返回值,满足条件返回值为1,不满足返回为0;
if(1-1),是属于算数表达式,注意区分。 碰到题目一定要去好好思考,好好思考,好好思考。
 #include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int N=; char a[N],b[N];
int dp[N][N]; int main()
{
int n,m;
while(~scanf("%d%s%d%s",&n,a,&m,b))
{
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
dp[i][]=i;
for(int i=;i<=m;i++)
dp[][i]=i;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
dp[i][j]=min(dp[i-][j]+,dp[i][j-]+);
dp[i][j]=min(dp[i][j],dp[i-][j-]+(a[i-]!=b[j-]));
}
}
printf("%d\n",dp[n][m]); }
return ;
}

POJ3356-AGTC-dp+字符串处理的更多相关文章

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

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

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

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

  3. Codeforces 176B (线性DP+字符串)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...

  4. Codeforces Round #367 (Div. 2) A B C 暴力 二分 dp(字符串的反转)

    A. Beru-taxi time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  5. HDU 2089 数位dp/字符串处理 两种方法

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. Codeforces 611d [DP][字符串]

    /* 题意:给一个长度不超过5000的字符串,每个字符都是0到9的数字. 要求将整个字符串划分成严格递增的几个数字,并且不允许前导零. 思路: 1.很开心得发现,当我在前i个区间以后再加一个区间的时候 ...

  7. poj3267--The Cow Lexicon(dp:字符串组合)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8211   Accepted: 3864 D ...

  8. poj3356 AGTC

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

  9. BZOJ3297: [USACO2011 Open]forgot DP+字符串

    Description 发生了这么多,贝茜已经忘记了她cowtube密码.然而,她记得一些有用的信息.首先,她记得她的密码(记为变 量P)长度为L(1 <= L<=1,000)字符串,并可 ...

  10. hdu 1159(DP+字符串最长公共序列)

    http://blog.csdn.net/a_eagle/article/details/7213236 公共序列可以用一个二维数组dp[i][j]保存每个点时的最大数字,本质就是一个双向比较. dp ...

随机推荐

  1. Python进阶:set和dict/对象引用、可变性和垃圾回收/元类编程/迭代器和生成器

    frozenset:不可变集合,无序,不重复 dict上的特性: 1. dict的key或者set的值 都必须是可以hash的(不可变对象 都是可hash的, str, fronzenset, tup ...

  2. 9. Jmeter-前置处理器

    jmeter-前置处理器介绍与使用 JSR223 PreProcessor 用户参数 HTML链接解析器 HTTP URL 重写修饰符 JDBC PreProcessor RegEx User Par ...

  3. Git与GitHub同步

    如何通过Git Bash实现本地与远端仓库——GitHub的同步 1.下载安装Git:下载网址 2.在自己的github上新建一个repository 例如我这里新建了一个叫test的reposito ...

  4. Head First PHP &MySQL学习笔记

      最近一段时间在学习PHP,买了<Head First PHP&MySQL>中文版这本书,之前买过<Head First设计模式>,感觉这系列的书籍总体来说很不错. ...

  5. Python - 元组 , range

    元组和元组嵌套 元组: 俗称不可变的列表.又被成为只读列表, 元组也是python的基本数据类型之一, 用小括号括起来, 里面可以放任何数据类型的数据, 查询可以. 循环也可以. 切片也可以. 但就是 ...

  6. elementUI 限制上传个数limit

    :limit='1' // 个数限制.

  7. mySQL学习入门教程——2.创建表

    二.创建表 一.创建数据表的SQL语句模型(弱类型)CREATE TABLE [IF NOT EXISTS] 表名称(字段名1 列的类型[属性][索引],字段名2 列的类型[属性][索引],-字段名n ...

  8. mybatis中的命名空间(namespace)的作用

    mybatis中为每一个映射文件添加一个namespace,这样不同的映射文件中sql语句的id相同也不会有冲突,只要定义在映射文件中的sql语句在该映射文件中id唯一就可以

  9. Activiti学习笔记3 — 流程定义

    一.创建流程引擎对象 private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); 二.发布一个流程 ...

  10. 【记录】API Gateway作用? 与过滤器的区别?Nginx与Zuul区别?

    网关(gateway)的作用: 网关可以拦截客户端所有请求,对该请求进行权限控制.负载均衡.日志管理.接口调用监控等 过滤器与网关的区别是什么? 过滤器是拦截单个tomcat服务器请求. 网关是拦截整 ...