hdoj 2476 String painter 【区间dp】

String painter

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2624    Accepted Submission(s): 1188


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串的任意一段涂上相同的字符,问你最少需要涂多少次才可以把a串变为b串。


思路:先计算出空串涂成b串的代价,用dp[i][j]存储区间[i, j]涂成和b串一样的最小代价。

因为字符都不相同,那么代价的优化点就在于b串中相同的字符。

dp[i][j] = min(dp[i+1][j]+1, dp[i+1][k]+dp[k+1][j]); (i+1<=k<=j && b[k] == b[i])

b[i]可以在涂区间[i+1, k]时顺带涂上。  处理时需要注意边界。


整理我们已经有的信息dp[i][j],把空串变成b[i] - b[j]的字符串所需的最小代价。

设置ans[i]为a[0] - a[i]的字符串变成b[0] - b[i]所需的最小代价。

则状态转移:

a[i] == b[i]   ans[i] = ans[i-1];

a[i] != b[i]   ans[i] = min(ans[i], ans[j] + dp[j+1][i]); 把区间[j+1, i]当做空串来处理。



AC代码:



#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (100+10)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
using namespace std;
char s1[110], s2[110];
int dp[110][110], ans[110];
int main()
{
    while(scanf("%s%s", s1, s2) != EOF)
    {
        int len = strlen(s1); CLR(dp, INF);
        for(int i = len-1; i >= 0; i--)
        {
            for(int j = i; j < len; j++)
            {
                if(i == j)
                {
                    dp[i][j] = 1;
                    continue;
                }
                dp[i][j] = dp[i+1][j]+1;
                for(int k = i+1; k <= j; k++)
                {
                    if(s2[i] == s2[k])
                    {
                        if(k == j)
                            dp[i][j] = min(dp[i][j], dp[i+1][j]);
                        else
                            dp[i][j] = min(dp[i][j], dp[i+1][k]+dp[k+1][j]);
                    }
                }
            }
        }
        for(int i = 0; i < len; i++)
            ans[i] = dp[0][i];
        for(int i = 0; i < len; i++)
        {
            if(s1[i] == s2[i])
            {
                if(i == 0)
                    ans[i] = 0;
                else
                    ans[i] = ans[i-1];
            }
            else
            {
                for(int j = 0; j < i; j++)
                    ans[i] = min(ans[i], ans[j] + dp[j+1][i]);
            }
        }
        Pi(ans[len-1]);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值