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


相关推荐

  • Sublime Text 3 全程详细图文教程(转载)

    Sublime Text 3 全程详细图文教程(转载)今天被群里大佬安利了一款文本编辑软件,找了一下相关教程。一、 前言使用SublimeText也有几个年头了,版本也从2升级到3了,但犹如寒天饮冰水,冷暖尽自知。最初也是不知道从何下手

    2022年7月4日
    21
  • 单向链表之删除节点(C语言实现)「建议收藏」

    单向链表之删除节点(C语言实现)「建议收藏」链表的创建查看删除节点就是将某一节点从链中摘除。将待删节点与其前一节点解除联系(中间或尾部)或本阶段删除(头节点),并释放相应空间(free)。删除的第一步是找到要删除的节点,同链表查找,如果找不到或链表为空,提示未找到,找到后根据情况删除此节点。删除节点两种情况:第一个节点,后面节点。步骤:1、链表为空:不用删除2、链表不为空:先循环找要删除的节点1)找到了1>找

    2025年8月5日
    2
  • 【计算机网络】网络层 : DHCP 协议 ( DHCP 协议概念 | DHCP 协议特点 | DHCP 协议流程 )

    【计算机网络】网络层 : DHCP 协议 ( DHCP 协议概念 | DHCP 协议特点 | DHCP 协议流程 )一、DHCP协议概念、二、DHCP协议特点、三、DHCP协议流程、

    2022年5月10日
    47
  • 2019版idea激活码(破解版激活)[通俗易懂]

    2019版idea激活码(破解版激活),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月17日
    696
  • typedef struct Lnode{ Elemtype data; struct Lnode *next; } Lnode,*Linklist;「建议收藏」

    typedef struct Lnode{ Elemtype data; struct Lnode *next; } Lnode,*Linklist;「建议收藏」*next是指向下一个值的地址而*Linklist是当前这个值的地址,只是这个值里有数据data,和下一个值的地址就是说链表中,因为数据是随机储存的,所以地址不是连续的,要想读取下一个数,就要知道下一个数据的地址不知道有没有看懂,表达能力不是很好*Linklist相当于一个数组头指针,只是这个数组的元素是结构体*next则是构成链表的一个基本元素,指向该结点下一个结点的地址从某种意义上讲,*Linklist是指定了这段空间在内存中的位置(可以申请连续的结点空间),而*next则对结点进行了一

    2022年5月19日
    41
  • UOS命令_uos手动安装教程

    UOS命令_uos手动安装教程其他Linux发行版中用习惯了ll命令,在UOS中居然木有提供,其实ll命令就是ls-alF命令的一个别名,加到.bashrc里面就行了$vim~/.bashrc#在文件末尾追加下面内容aliasll=’ls-alF’$source~/.bashrc$ll

    2022年9月2日
    3

发表回复

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

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