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


相关推荐

  • mysql 把表名改成大写_mysql将表名改成大写的实例

    mysql 把表名改成大写_mysql将表名改成大写的实例MYSQL将表名称修改成大写的存储过程本文为大家分享了MYSQL将表名称修改成大写的存储过程,具体内容如下1.条件:1.1Mysql设置对大小写敏感2.执行下述存储过程:#calluppercase(‘库名’)DROPPROCEDUREIFEXISTSuppercase;CREATEPROCEDUREuppercase(INdbnameVARCHAR(200))BEGIND…

    2022年5月27日
    27
  • PetShop4架构设计分析(三,四)

    PetShop4架构设计分析(三,四) petshop4.0详解之三(PetShop数据访问层之消息处理)三、PetShop数据访问层之消息处理在进行系统设计时,除了对安全、事务等问题给与足够的重视外,性能也是一个不可避免的问题所在,尤其是一个B/S结构的软件系统,必须充分地考虑访问量、数据流量、服务器负荷的问题。解决性能的瓶颈,除了对硬件系统进行升级外,软件设计的合理性尤为重要。在前面我曾提到,分层式结构设计可能会在一定

    2022年10月16日
    0
  • Vue学习之v-if和v-for指令「建议收藏」

    Vue学习之v-if和v-for指令「建议收藏」Vue学习之v-if和v-for指令

    2022年4月23日
    100
  • notepad++正则表达式替换字符串

    notepad++正则表达式替换字符串则表达式是一个查询的字符串,它包含一般的字符和一些特殊的字符,特殊字符可以扩展查找字符串的能力,正则表达式在查找和替换字符串的作用不可忽视,它能很好提高工作效率。EditPlus的查找,替换,文件中查找支持以下的正则表达式:表达式说明/t制表符./n新行..匹配任意字符.|匹配表达式左边和右边的字符.例如,”ab|bc”匹配”ab”或者”bc”….

    2022年5月17日
    41
  • MyEclipse10.0 配置 Tomcat1.7

    MyEclipse10.0 配置 Tomcat1.7

    2021年8月29日
    63
  • 2021最强Python学习教程,从零基础入门到精通

    2021最强Python学习教程,从零基础入门到精通你准备好了吗???areyouready???前言01.python介绍02.项目开发完整流程(详解版)03.项目开发流程(精简版)第一篇计算机核心基础01计算机组成原理第二篇编程语言01编程语言介绍第三篇python入门01python介绍及IDE集成开发环境02python是解释型的强类型动态语言03python语法之变量、常量04python语法之注释05python垃圾回收机制GC06Python语法入门之基本数据类型07Python语法

    2022年9月19日
    1

发表回复

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

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