HDU2476(60/600)

本文介绍了一种通过动态规划算法解决字符串A转换为字符串B所需的最少操作次数的问题。利用一个强大的字符串画家工具,该工具能将字符串中的一段字符替换为任意单一字符。文章详细描述了解决方案的思路及实现过程。

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

Problem Description
There are two strings A and B with equal length. Both strings are made up of lower case letters. Now you have a powerful string painter. With the help of the painter, you can change a segment of characters of a string to any other character you want. That is, after using the painter, the segment is made up of only one kind of character. Now your task is to change A to B using string painter. What’s the minimum number of operations?

Input
Input contains multiple cases. Each case consists of two lines:
The first line contains string A.
The second line contains string B.
The length of both strings will not be greater than 100.

Output
A single line contains one integer representing the answer.

Sample Input
zzzzzfzzzzz
abcdefedcba
abababababab
cdcdcdcdcdcd

Sample Output
6
7

这个题实在是太6了…
因为直接转移太难转移了…
所以题解是先算出来从一个空的直接转移到b串的dp
那么就直接+1就行了可以不用多bb
遇到两端点相同的区间就枚举一下所有子区间求最小的..
这样就相当于隐性的少了一个+1操作…
这tmd实在是太6了….

然后再去算从a怎么到b….
不一样的当空白算….一样的等于前一个…..
哇这个题太骚了

#include<bits/stdc++.h>
using namespace std;
int dp[101][101],dan[101];
string q,w;
int main()
{
    while(cin>>q>>w)
    {
        int n=q.size();
        memset(dp,0,sizeof(dp));
        for(int a=0;a<n;a++)dp[a][a]=1;
        for(int a=0;a<n;a++)
        {
            for(int b=a-1;b>=0;b--)
            {
                dp[b][a]=dp[b+1][a]+1;
                for(int c=b+1;c<=a;c++)
                {
                    if(w[b]==w[c])dp[b][a]=min(dp[b+1][c]+dp[c+1][a],dp[b][a]);
        //          dp[b][a]=min(dp[b][c]+dp[c+1][a],dp[b][a]);
                }
            }
        }
        for(int a=0;a<n;a++)
        {
            if(q[a]==w[a])
            {
                if(a==0)dan[a]=0;
                else
                {
                    dan[a]=dan[a-1];
                }
                continue;
            }
            dan[a]=dp[0][a];
            for(int b=0;b<a;b++)
            {
                dan[a]=min(dan[a],dan[b]+dp[b+1][a]);
            }
        }
        cout<<dan[n-1]<<endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值