POJ3356-AGTC-dp+字符串处理
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 题意:
每组数据给出两个字符串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+字符串处理的更多相关文章
- POJ3356 – AGTC(区间DP&&编辑距离)
题目大意 给定字符串X和Y,可以对字符串进行一下三种操作: 1.删除一个字符 2.插入一个字符 3.替换一个字符 每个操作代价是1,问运用以上三种操作把X变为Y所需的最小步数是多少? 题解 定义dp[ ...
- POJ 3356 AGTC(DP求字符串编辑距离)
给出两个长度小于1000的字符串,有三种操作,插入一个字符,删除一个字符,替换一个字符. 问A变成B所需的最少操作数(即编辑距离) 考虑DP,可以用反证法证明依次从头到尾对A,B进行匹配是不会影响答案 ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
- 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 ...
- HDU 2089 数位dp/字符串处理 两种方法
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Codeforces 611d [DP][字符串]
/* 题意:给一个长度不超过5000的字符串,每个字符都是0到9的数字. 要求将整个字符串划分成严格递增的几个数字,并且不允许前导零. 思路: 1.很开心得发现,当我在前i个区间以后再加一个区间的时候 ...
- poj3267--The Cow Lexicon(dp:字符串组合)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8211 Accepted: 3864 D ...
- poj3356 AGTC
Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...
- BZOJ3297: [USACO2011 Open]forgot DP+字符串
Description 发生了这么多,贝茜已经忘记了她cowtube密码.然而,她记得一些有用的信息.首先,她记得她的密码(记为变 量P)长度为L(1 <= L<=1,000)字符串,并可 ...
- hdu 1159(DP+字符串最长公共序列)
http://blog.csdn.net/a_eagle/article/details/7213236 公共序列可以用一个二维数组dp[i][j]保存每个点时的最大数字,本质就是一个双向比较. dp ...
随机推荐
- [转载]真正的inotify+rsync实时同步 彻底告别同步慢
原文链接http://www.ttlsa.com/web/let-infotify-rsync-fast/ 背景我们公司在用inotify+rsync做实时同步,来解决分布式集群文件一致性的问题.但当 ...
- 论一个PHP项目上线的注意点
一.后端问题 服务器配置要跟上流量 预估QPS时要给足未知流量的空间 后端数据库设计要根据项目大小来相对应,小型流量单表就可以,但是中大型要分库分表 在处理执行修改的操作时一定要多一层判断(判断是否已 ...
- 转 jmeter 实现loadrunner init end 功能
一.JMeter 介绍 Apache JMeter是100%纯JAVA桌面应用程序,被设计为用于测试客户端/服务端结构的软件(例如web应用程序).它可以用来测试静态和动态资源的性能,例如:静态文件, ...
- JPA 继承关系实现的三种方式
single table 一张表保存所有类型 join 扩展属性保存在子表中 TABLE_PER_CLASS 每个类型一张表
- Anjular的ng-repeat
Anjular的ng-repeat不会循环一个二维集合中的一维集合.举个例子:集合 list= {1,2,{0,1,2},23,222},small={0,1,2},使用ng-repeat" ...
- mysql 5.7.20 取得动态sql执行结果
drop procedure test; delimiter ;; CREATE procedure test() -- 取动态sql的值 begin ); ); set v_sqlcounts = ...
- >>> print "hello" SyntaxError: Missing parentheses in call to 'print'
错误原因说你的函数print缺省圆括号,可以知道你用的python是3.x版本3.x版本的python,print中的参数要用圆括号括起来,改成:print("hello")
- kali linux 2019.4 最新版设置中文 - 开启win10风格界面
最新版 kali linux 2019.4 默认是不带中文字体的,执行以下命令,便可以显示: kali源:vi /etc/apt/source.list #中科大 deb http://mirrors ...
- springMVC的数据封装
编写实体类: package cn.mepu.domain; /** * @User 艾康 * @create 2019-11-12 13:56 */ public class User { priv ...
- git 版本控制库的用法及其介绍
版本控制 说到版本控制,脑海里总会浮现大学毕业是写毕业论文的场景,你电脑上的毕业论文一定出现过这番景象! 1 2 3 4 5 6 7 8 9 10 11 毕业论文_初稿.doc 毕业论文_修改1.do ...