题目链接:hdu_2476_String painter

题意:

有a,b两字符串,现在你有一个刷子,每次可以任选一个区间,使这个区间变成你想要的字符,现在让你将a变成b,问最少刷多少次

题解:

考虑区间dp[i][j],表示从第i到第j最少需要刷的次数。这里要先算从空串到b的dp,然后根据这个来推a串到b串的最小次数。

因为如果a的整个区间需要重新构造,这个区间就相当于空串。状态转移方程具体看代码

 #include<cstdio>
#include<cstring>
#define F(i,a,b) for(int i=a;i<=b;i++) const int N=;
char a[N],b[N];
int dp[N][N],ans[N]; inline void up(int &a,int b){if(a>b)a=b;} int main()
{
while(~scanf("%s%s",a+,b+))
{
int n=strlen(a+);
memset(dp,,sizeof(dp));
F(i,,n)dp[i][i]=;
F(l,,n-)F(i,,n-l)
{
int j=i+l;
dp[i][j]=dp[i+][j]+;
F(k,i+,j)if(b[i]==b[k])up(dp[i][j],dp[i+][k]+dp[k+][j]);
}
F(i,,n)ans[i]=dp[][i];
F(i,,n)if(a[i]==b[i])ans[i]=(i==?:ans[i-]);
else F(j,,i-)up(ans[i],ans[j]+dp[j+][i]);
printf("%d\n",ans[n]);
}
return ;
}

hdu_2476_String painter(区间DP)的更多相关文章

  1. hdu2476 String painter(区间dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2476 Problem Description There are two strings ...

  2. HDU_2476_String painter_(区间dp)

    String painter Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. HDU 2476 String painter(区间dp)

    题意: 给定两个字符串,让求最少的变化次数从第一个串变到第二个串 思路: 区间dp, 直接考虑两个串的话太困难,就只考虑第二个串,求从空白串变到第二个串的最小次数,dp[i][j] 表示i->j ...

  4. HDU2476 String painter——区间DP

    题意:要把a串变成b串,每操作一次,可以使a串的[l,r]区间变为相同的一个字符.问把a变成b最少操作几次. 这题写法明显是区间dp ,关键是处理的方法. dp[l][r]表示b串的l~r区段至少需要 ...

  5. HDU2476 String painter —— 区间DP

    题目链接:https://vjudge.net/problem/HDU-2476 String painter Time Limit: 5000/2000 MS (Java/Others)    Me ...

  6. uva live 4394 String painter 区间dp

    // uva live 4394 String painter // // 这一题是训练指南上dp专题的习题,初看之下认为仅仅是稍微复杂了一点 // 就敲阿敲阿敲,两个半小时后,发现例子过了.然而自己 ...

  7. hdu 2476"String painter"(区间DP)

    传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给定字符串A,B,每次操作可以将字符串A中区间[ i , j ]的字符变为ch, ...

  8. HDU 2476 String painter(区间DP+思维)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意:给你字符串A.B,每次操作可以将一段区间刷成任意字符,问最少需要几次操作可以使得字符串 ...

  9. hdu2476String painter (区间DP)

    Problem Description There are two strings A and B with equal length. Both strings are made up of low ...

随机推荐

  1. 手工场景--controller--场景设计、场景监控、运行场景

    场景设置: 1.设置界面 2.全局设置. A:初始化: B:启动用户: C:

  2. 2014蓝桥杯问题 C: 神奇算式

    没做完,先搞答题了 #include <stdio.h> #include<string.h> #include<stdlib.h> int comp(const ...

  3. emacs 使用教程

    http://www.cnblogs.com/liuchaogege/p/4464211.html

  4. JTree单击事件

    import javax.swing.*; import javax.swing.tree.*; import java.awt.FlowLayout; import java.awt.GridLay ...

  5. http://www.cnblogs.com/ycxyyzw/archive/2012/07/31/2616951.html

    http://www.cnblogs.com/ycxyyzw/archive/2012/07/31/2616951.html

  6. es5 中类的2种基本实现方法

    function test(){ this.a = 1; this.func = function(){ // var a = 3;下面的this 取的是上面的1,这个不影响 return this. ...

  7. 安装Mysql,缺少libaio依赖

    安装mysql时报如下错误: xxxxx/mysql/bin/mysqld: error : cannot open shared object file: No such file or direc ...

  8. python 主机宝

    需求:开发一个主机批量管理系统,要求按saltstack方式执行命令 #!/usr/bin/env python3. # -*- coding:utf8 -*- import os,sys,pickl ...

  9. sql优化--in和exists效率

    系统要求进行SQL优化,对效率比较低的SQL进行优化,使其运行效率更高,其中要求对SQL中的部分in/not in修改为exists/not exists 修改方法如下: in的SQL语句 SELEC ...

  10. Elasticsearch .net 客户端条件拼接查询

    private Func<SearchDescriptor<BaseTest>, ISearchRequest> ConstructWhere(TestCondition wh ...