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)
上一篇 2021年12月4日 下午5:00
下一篇 2021年12月4日 下午6:00


相关推荐

  • Git篇

    Git篇

    2021年10月19日
    41
  • 运维架构

    运维架构运维架构1、运维团队配置运维团队分为:应用运维、系统运维、运维开发、监控运维、DBA团队和安全团队团队分工:应用运维:负责支持线上业务,各自会负责对应的业务线,主要职能是保证线上业务稳定性和同开发共

    2022年7月1日
    25
  • redis数据库端口号_redis对接mysql

    redis数据库端口号_redis对接mysql关系型数据库一:Oracle驱动:oracle.jdbc.driver.OracleDriverURL:jdbc:oracle:thin:@:dbname注:machine_name:数据库所在的机器的名称,如果是本机则是127.0.0.1或者是localhost,如果是远程连接,则是远程的IP地址;port:端口号,默认是1521二:SQLServer驱动:com.microsoft.jdb…

    2026年1月15日
    7
  • godaddy域名服务器_自己有域名怎么建网站

    godaddy域名服务器_自己有域名怎么建网站自己准备做一个个人网站,由于第一次做所以遇到了各种困难,现在把自己的一些经历经验贴出来供大家参考。此贴针对一个完全没有经验的菜鸟,大牛勿喷。个人搭建一个网站首先要考虑的是网址(域名),然后是你的文件放到哪里(空间)。最后将域名解析后,绑定到空间则网站搭建完成就可以访问。网上关于网站搭建的技术贴有很多,但由于岁月年长有些方法或步骤可能不适用,在这里通过一次个人经历,给大家提供一些经验。一般域

    2022年10月8日
    7
  • 查看linux系统版本centos,CentOS下查看系统版本的4种方法

    查看linux系统版本centos,CentOS下查看系统版本的4种方法Linux有很多的发行版,不同的版本会有一些细微区别,所以经常需要查看服务器系统的版本号。下面来看下CentOS下如何查看CentOS版本。方法1:cat/etc/issue执行命令:[www@pythontab.com]$cat/etc/issueCentOSrelease6.8(Final)Kernelonanm我当前系统版本就是:CentOS6.8知识扩展:etc目录…

    2022年6月24日
    31
  • 一、为什么要是用Redis?

    一、为什么要是用Redis?前言随着大数据时代的来临 随之而来的三高问题 高并发 高性能 高可用 也随之而来 数据库的写入压力增加 读写集中在一个数据库上让数据库不堪重负 大部分网站开始使用主从复制技术来达到读写分离 以提高读写性能和读库的可扩展性 MySQL 的 master slave 模式成为这个时候的网站标配了 MySQL 数据库也经常存储一些大文本的字段 导致数据库表非常的大 在做数据库恢复的时候就导致非常的慢 不容易快速恢复数据库 关系数据库很强大 但是它并不能很好的应付所有的应用场景 MySQL 的扩展性差 需要复杂的技

    2026年1月18日
    3

发表回复

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

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