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 ...
随机推荐
- kafka 简单安装以及java小demo
文章目录 第1步,下载解压 kafka: 第2步,运行 kafka: 第3步,创建topic 第4步,生产者发送消息 第5步,消费者接收消息 使用 java 客户端 kafka 0.8.0版本demo ...
- Java封装JDBC数据库增、删、改、查操作成JAR文件,以供Web工程调用,适用于多种数据库
废话不多说,直接上源代码,最后有使用方法,当然,也可以作为普通公用类使用,只是封装成JAR更方便使用. package db.util; import java.io.BufferedReader; ...
- Oracle高水位线(HWM)及性能优化
说到HWM,我们首先要简要的谈谈ORACLE的逻辑存储管理.我们知道,ORACLE在逻辑存储上分4个粒度:表空间,段,区和块. (1)块:是粒度最小的存储单位,现在标准的块大小是8K,ORACL ...
- PAT甲级——A1152 GoogleRecruitment【20】
In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...
- 卷积实现 python
import sys h, w = input().strip().split() h = int(h) w = int(w) img = [] for i in range(h): line = s ...
- XML DTD详解
XML DTD详解 一个有效的XML文档必然是结构正规的,结构正规的XML文档不一定是有效的,即有效的是格式正规的一个子集. 本文详细介绍DTD,包括其对元素的定义,属性的定义,以及实体的定义. 元素 ...
- c# 关于DataTable
1.DataRow数组 转DataTable using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=123;da ...
- Oracle上课学习笔记<1>
简单的select查询语句 1.select查询语句基本语法 使用两个关键字: select 指定要查询的字段和内容 from 从哪张表中查询 语法:select 字段名 from 表名; 三种不同的 ...
- 安装APK到android设备那些事儿
APK是AndroidPackage的缩写,即Android系统的应用软件安装包(apk).APK是类似Symbian Sis或Sisx的文件格式.通过将APK文件直接传到Android模拟器或And ...
- java多线程面试题_线程并发面试题
1.什么是线程?线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位.程序员可以通过它进行多处理器编程,你可以使用多线程对运算密集型任务提速.比如,如果一个线程完成一个 ...