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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 孤立的SQL用户

    孤立的SQL用户

    2021年11月26日
    45
  • linux centos7配置网络教程,linux centos7配置网络「建议收藏」

    linux centos7配置网络教程,linux centos7配置网络「建议收藏」1.动态获取ip(前提是你的路由器已经开启了DHCP)修改网卡配置文件vi/etc/sysconfig/network-scripts/ifcfg-ens32(最后一个为网卡名称)动态获取IP地址需要修改两处地方即可(1)bootproto=dhcp(2)onboot=yes修改后重启一下网络服务即可systemctlrestartnetwork2、配置静态IP地址设置静态IP地…

    2022年5月8日
    46
  • php 虚拟ip 刷流量,浅析网站刷流量的利与弊「建议收藏」

    php 虚拟ip 刷流量,浅析网站刷流量的利与弊「建议收藏」估计不少站长都有过刷网站流量的经历,一般发生在网站刚刚上线的时候。网站刚上线时,流量来自哪里?搜索引擎肯定甚少,而推广也应该刚刚开始,推广带来的IP想必也并不乐观。每天登陆站长统计,都是看到寥寥的几十个IP,站长有可能会产生更多的想法。而刷网站IP,尤为常见。刷网站流量有几个常用的方法:1、通过js刷网站流量。这个方法是最初级的刷网页流量的方法,它只能够刷网页PV,而不能刷网站IP,因此几乎没有人…

    2022年9月29日
    3
  • 录取为2021年同济大学秋季博士研究生(电子与信息工程学院计算机科学与技术)

    录取为2021年同济大学秋季博士研究生(电子与信息工程学院计算机科学与技术)本来不想回忆往事,读博路上有点坎坷!!遇人不淑,时运不济,还好也遇到一些特别好的老师!同学帮忙。非常感谢这些老师的帮忙。首先是2020年10月份,我硕士生博导朱老师,告诉我他没博士招生名额,但有资格,这个吉大计算机比较特殊,每年博导重新排名,一年一考核,按照分来分博士名额,我老师最后一名,院长副院长有要两个名额的!这里面不方便多说,最后就是我老师今年21招不了博士,到我这卡壳一年,也特别遗憾没能读朱老师的博士。接下来沉沦了两天,10月10号左右吧,开始疯狂联系吉大别的博导,还好联系了几位,其中一z姓老

    2022年7月25日
    32
  • java轻量级web框架_什么是框架

    java轻量级web框架_什么是框架JEMSF框架前言今天我们准备向广大开发人员推荐一种新的框架,暂时取名JEMSF,如果您已经对Struts、Tapestry以及Spring和Hibernat有一些了解,那么应该可以更好的理解下面的文章,JEMSF是我在工作生涯中最大的一个创造,经历了很多考验和应用的试验,最后形成JEMSF。序一种新的事物的诞生需要经历很多的考验,我自认为JEMSF是一个很好的WEB应用框架,很久以前(2…

    2025年9月30日
    2
  • hive基础总结(面试常用)

    hive基础总结(面试常用)

    2021年6月29日
    77

发表回复

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

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