pojAGTC(LCS,DP)

pojAGTC(LCS,DP)

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

题目链接:

啊哈哈,点我点我

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

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

题目:

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;
}


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118550.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • EVE模拟器的使用-带图超详细(学网络用)「建议收藏」

    EVE模拟器的使用-带图超详细(学网络用)「建议收藏」文章目录EVE模拟器的安装EVE模拟器的使用登陆添加一个实验退出一个实验实验分类创建任务(添加路由交换机)选择型号和种类说明修改已创建种类参数界面放大和缩小方式一方式二种类(设备)之间连线和删除连线连线删线添加注释信息(描述)设备开机开启单个设备开启所有设备设备功能配置软件抓包EVE模拟器的安装EVE模拟器的下载及安装见:EVE模拟器的使用登陆根据上面安装步骤进入到这个界面以后:用户名:admin密码:unl(小写L)登陆方式:Nativeconsole登陆成功以后就是这个界面:添

    2022年4月27日
    485
  • updog云盘_dog意思

    updog云盘_dog意思pip3installupdog#安装注意必须基于 python3.x 进行安装updog-d目录-p端口–password密码#开启云盘服务在关闭命令

    2022年8月1日
    4
  • mysql databasemetadata_DatabaseMetaData和ResultSetMetaData接口获取元数据的方法

    mysql databasemetadata_DatabaseMetaData和ResultSetMetaData接口获取元数据的方法DatabaseMeta 和 ResultSetMet 就是两个常用的获取数据库元数据相关信息的接口 本文讲解 DatabaseMeta 和 ResultSetMet 接口获取元数据的方法 packagewww wlkankan com importjava sql Connection importjava sql DatabaseMeta importja

    2025年8月31日
    3
  • Linux操作系统常用命令_centos命令大全及用法

    Linux操作系统常用命令_centos命令大全及用法Linux操作系统常用指令大全,包括开关机操作、帮助指令、文件目录类指令、时间日期类指令、搜索查找类指令、压缩解压类指令,附举例说明。

    2022年9月26日
    5
  • pycharm激活码2021年【2021.7最新】

    (pycharm激活码2021年)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月21日
    51
  • 数据库隔离级别实现原理[通俗易懂]

    数据库隔离级别实现原理[通俗易懂]昨晚和以为前辈聊天,聊到Mysql的引擎innodb默认的事务隔离级别是REPEATABLEREAD(可重复读);在Oracle中默认的事务隔离级别是提交读(readcommitted)。那么,问题来了,在Mysql中的隔离级别是怎么实现的呢?关于数据库数据隔离级别在之前的博客中总结过:https://blog.csdn.net/qq_34417408/article/details/79…

    2022年5月9日
    63

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号