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


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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 在线部署kubeedge 1.6详细教程(Ubuntu)

    在线部署kubeedge 1.6详细教程(Ubuntu)在线部署kubeedge1.6详细教程(Ubuntu与树莓派)基本环境:操作系统:centos7;Ubuntu;docker:20.10.7角色系统IPmastercentos7(amd64)172.16.0.114edgeubuntu(arm64)172.16.0.113kubeedge部署须知master以成功部署kubernetes,并且master结点处于ready状态.edge未执行kubeadmjoin命令kubeedge部署配置

  • CCBValue

    CCBValue

  • 存储过程之流程控制语句

    存储过程之流程控制语句

  • docker 入门_docker高级教程

    docker 入门_docker高级教程一、docker概述1.什么是dockerDocker是一个开源的应用容器引擎,基于Go语言并遵从Apache2.0协议开源。Docker可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口(类似iPhone的app),更重要的是容器性能开销极低。Docker从17.03版本之后分为CE(CommunityEdition:社区版)和E..

    2022年10月15日
  • ES6数组的各种方法「建议收藏」

    ES6数组的各种方法「建议收藏」1.ES6数组的各种方法2.forEach()函数①数组名.forEach(function(数组中一个元素的值){对这个值进行处理….})②数组名.forEach(test)test为方法名,不用加(),把函数引用传进去③利用函数引用这种方式的话,会自己把每个值传进去,不需要用()传进去3.map()方法①map()方…

  • 苹果手机数据转移到新手机_旧手机数据转移到新手机,一键免费传输

    苹果手机数据转移到新手机_旧手机数据转移到新手机,一键免费传输这款软件所有人都能用到建议收藏备用当我们换新手机时是不是很多数据需要转移很繁琐费劲电话号短信备注等等都想留着解决办法来了!!!今天推送的这款神器是腾讯旗下唯一一款零差评的app这款软件真正解决了我们平时更换手机遇到的所有痛点,没有GG无需会员软件名字:换机助手(适用于安卓iOS)01软件介绍现在随着互联网的发展,智能手机几乎人手必备,而且大家更换手机的频率越来越高,更换手机时候,…

发表回复

您的电子邮箱地址不会被公开。

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