区间dp String painter

1 篇文章 0 订阅
String painter
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

给定两个字符串a,b,可以在a的某个区间里全涂成某个字符,求变成b的最少涂法。
这题想不到的话很难,看了题解也不容易懂。
要做两次dp,第一次:
dp[i][j]:把一个空串涂成b[i][j]之间的字符的最小花费。
因为是区间涂色,所以如果涂色区间有交集的话,先涂上的区间可以缩小,因为后涂的区间会把之前的覆盖,相当于没有交集。
所以,长度为1,dp[i][j]=1。
如果b区间两端点相同,即 b[i]==b[j],则dp[i][j]=dp[i+1][j],不是dp[i+1][j-1]+1,因为如果b[i+1] 和b[j-1]中都不和b[i]一样,dp[i+1][j-1]+1也对,如果有一个或两个一样,显然这样就不是最优答案。
第二次:
ans[i]: a字符串前i个与b的前i个涂成一样的最小花费,
当a[i]=b[i]时,ans[i]=ans[i-1],因为涂i-1的时候可以往右延伸一个,把前i个也涂了
否则,ans[i]=min(ans[k]+dp[k+1][i]),因为不相等,所以i位置必定要涂

#include<bits/stdc++.h>
#include<stdio.h>
#include<algorithm>
#include<ctype.h>
using namespace std;
template<class T> inline bool read(T &x){
    x=0;register char c=getchar();register bool f=0;
    while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();}
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
    if(f)x=-x;
    return true;
}
template<class T>inline void print (T x){
    if(x<0)putchar('-'),x=-x;
    if(x>9)print (x/10);
    putchar('0'+x%10);
}
#define Init(a,v) memset(a,v,sizeof(a))
typedef long long ll;
const ll MAXN=1e2+8,inf=0x3f3f3f3f,mod=1e9+7;
char a[MAXN],b[MAXN];
int n,dp[MAXN][MAXN],ans[MAXN];
int main() {
    a[0]=b[0]='a';
    while(~scanf("%s%s",a+1,b+1)){
        n=strlen(a)-1;
        for(int i=1;i<=n;++i)dp[i][i]=1;
        for(int len=2;len<=n;++len){
            for(int i=1,j=len;j<=n;++i,++j){
                dp[i][j]=len;
                if(b[i]==b[j])dp[i][j]=dp[i+1][j];
                else{
                    for(int k=i;k<j;++k)
                        dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);
                }
            }
        }
        ans[1]=(a[1]!=b[1]);
        for(int i=2;i<=n;++i){
            ans[i]=i;
            if(a[i]==b[i])ans[i]=ans[i-1];
            else{
                for(int k=0;k<i;++k)
                    ans[i]=min(ans[i],ans[k]+dp[k+1][i]);
            }
        }
        print(ans[n]);
        putchar('\n');
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值