POJ-3356-AGTC

本文介绍了一种通过动态规划算法解决字符串转换问题的方法,旨在找到将一个字符串转换为另一个字符串所需的最少操作次数(包括插入、删除和替换)。文章详细解释了算法思路及实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                         Time Limit: 1000MS     Memory Limit: 65536K
                         Total Submissions: 12444       Accepted: 4663

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

题目大意:给出两个字符串x 与 y,其中x的长度为n,y的长度为m,并且m>=n,然后y可以经过删除一个字母,添加一个字母,转换一个字母,三种操作得到x,问最少可以经过多少次操作

解题思路:显然此题有点类似于dp中的最长公共子串的问题 就像求最长公共子串那样设dp[i][j]-含义为取前i个字母和x取前j个字母的最少操作次数。

首先是初始化数组dp,初始化dp后一定要注意dp[0][i] = i和dp[i][0]=i 因为如果另一个字符串为空 要获取到另一个字符串的话就必须要进行i次添加操作。

之后就像求最长公共子串相似的操作 不过前者是最大,这里恰恰相反,取最小的。两重for循环。此时我们要分析一共有几种情况,题意已经给出了解答,就只有转换,删除,添加三种操作,所有我们要判断哪个操作次数更加少赋值给当前的dp[i][j]。当然分析这三种情况前,应当先判断当前两字符是否相同。我们下面分这两种情况来讨论

1.当前两字符相同 不进行任何操作 当前dp[i][j]=dp[i-1][j-1]
2.当前两字符相同 进行删除操作 当前dp[i][j]=dp[i-1][j]+1 因为,目的串比源串小,所以删除源串一个字符
3.当前两字符相同 进行添加操作 当前dp[i][j]=dp[i][j-1]+1 在目的串添加一个字符,即源串不变,但是目的串减1,和源串去匹配

1.当前两字符不相同 进行转换操作 当前dp[i][j]=dp[i-1][j-1]+1
2.当前两字符不相同 进行删除操作 当前dp[i][j]=dp[i-1][j]+1 因为,目的串比源串小,所以删除源串一个字符
3.当前两字符不相同 进行添加操作 当前dp[i][j]=dp[i][j-1]+1 在目的串添加一个字符,即源串不变,但是目的串减1,和源串去匹配

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
int n,m;
int dp[1005][1005];
char a[1005],b[1005];
int main()
{
    while(~scanf("%d%s%d%s",&n,a,&m,b))
    {
        memset(dp,0,sizeof(dp));//初始化dp
        for(int i=0;i<=n;i++)
            dp[i][0]=i;//特别注意
        for(int i=0;i<=m;i++)
            dp[0][i]=i;//特别注意
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i-1]==b[j-1])//当前比较两字符相同
                    dp[i][j]=min(min(dp[i-1][j-1],dp[i-1][j]+1),dp[i][j-1]+1);//不进行操作 删除 添加操作取最小值
                else
                    dp[i][j]=min(min(dp[i-1][j-1]+1,dp[i-1][j]+1),dp[i][j-1]+1);//转换 删除 添加操作中取最小值
            }
        }
        printf("%d\n",dp[n][m]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值