题目链接:

啊哈哈,点我点我

题意:给两个字符串,找出经过多少个操作能够使得两个串相等。。

思路:找出两个串的最长公共子序列,然后用最大的串的长度减去最长公共子序列的长度得到的就是须要的操作数。。

题目:

AGTC
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10015   Accepted: 3849

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

Source

代码为:

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1000+10;
int dp[maxn][maxn];
char str1[maxn],str2[maxn]; int LCS(int len1,int len2)
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=len1;i++)
for(int j=1;j<=len2;j++)
{
if(str1[i-1]==str2[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
return dp[len1][len2];
} int main()
{
int n,m;
while(~scanf("%d%s",&n,str1))
{
scanf("%d%s",&m,str2);
int len1=strlen(str1);
int len2=strlen(str2);
int ans=LCS(len1,len2);
int max_ans=max(n,m);
printf("%d\n",max_ans-ans);
}
return 0;
}

pojAGTC(LCS,DP)的更多相关文章

  1. 51Nod 1092 回文字符串(LCS + dp)

    51Nod 1092 数据结构暑假作业上出现的一题,学习了一下相关算法之后,找到了oj测试能AC. 1.回文串是一种中心对称的结构,这道题可以转变为求最长回文子序列长度的题目.(子序列:可以不连续) ...

  2. 51 Nod 1006 最长公共子序列(LCS & DP)

    原题链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1006 题目分析: 首先先知道LCS问题,这有两种: Long ...

  3. DP基础(线性DP)总结

    DP基础(线性DP)总结 前言:虽然确实有点基础......但凡事得脚踏实地地做,基础不牢,地动山摇,,,嗯! LIS(最长上升子序列) dp方程:dp[i]=max{dp[j]+1,a[j]< ...

  4. ACdreamOJ 1154 Lowbit Sum (数字dp)

    ACdreamOJ 1154 Lowbit Sum (数位dp) ACM 题目地址:pid=1154" target="_blank" style="color ...

  5. 「SDOI2016」储能表(数位dp)

    「SDOI2016」储能表(数位dp) 神仙数位 \(dp\) 系列 可能我做题做得少 \(QAQ\) \(f[i][0/1][0/1][0/1]\) 表示第 \(i\) 位 \(n\) 是否到达上界 ...

  6. 【HDU1693】Eat the Trees(插头dp)

    [HDU1693]Eat the Trees(插头dp) 题面 HDU Vjudge 大概就是网格图上有些点不能走,现在要找到若干条不相交的哈密顿回路使得所有格子都恰好被走过一遍. 题解 这题的弱化版 ...

  7. 【BZOJ1814】Ural 1519 Formula 1 (插头dp)

    [BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...

  8. 【BZOJ4712】洪水(动态dp)

    [BZOJ4712]洪水(动态dp) 题面 BZOJ 然而是权限题QwQ,所以粘过来算了. Description 小A走到一个山脚下,准备给自己造一个小屋.这时候,小A的朋友(op,又叫管理员)打开 ...

  9. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

随机推荐

  1. 网站linux.linuxidc.com有很多好资料

    免费下载地址在 http://linux.linuxidc.com/ 用户名与密码都是www.linuxidc.com 有一些介绍:www.linuxidc.com/download

  2. php随笔1-php图片处理

    php图片处理的知识内容 upload_image.php: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...

  3. ASP.NET页面跳转

    总结一下跳转方式: <a>标签 <a href=”home.aspx”></a> HyperLink控件 Asp.net 服务器端控件 属性NavigateUrl指 ...

  4. Oracle EBS-SQL (BOM-3):检查期间新增Bom数量.sql

    --本周系统BOM汇总记录 SELECT         ITM.SEGMENT1  物料编码, ITM.DESCRIPTION   物料描述, bom2.CREATION_DATE   创建日期, ...

  5. C语言入门(8)——形参与实参

    对于带参数的函数,我们需要在函数定义中指明参数的个数和每个参数的类型,定义参数就像定义变量一样,需要为每个参数指明类型,并起一个符合标识符命名规则的名字.例如: #include <stdio. ...

  6. Swift语法总结(精简版)

    第一部分: 1. Swift简介 2010年的夏天,苹果公司的开发人员Chris Lattne接到了一个特别的任务,为OS X 和iOS平台开发下一代的编程语言,也就是Swift. 苹果公司于2014 ...

  7. dataset 用法(3)

    ReadXml 提供了只将数据或同时将数据和架构从 XML 文档读入 DataSet 的方式(若要同时读数据和架构,请使用包括 mode 参数的 ReadXML 重载之一,并将其值设置为 ReadSc ...

  8. Oracle存储过程返回一张表数据

    在oracle数据库中你要在程序里得到一张表的数据就必须先创建游标和SQL Service不一样. --创建游标create or replace package pkg_Dataas type re ...

  9. JavaSE思维导图(一)

  10. c# 小数的处理

    数值类型处理小数 1.Math.Round(x) 四舍五入      Math.Round(0.4) 0     Math.Round(-1.7) -2 2.Math.floor(x) 小于等于 x, ...