String painter

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

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
 
2017.3.18复习,感觉这道题的dp设计很巧妙,还是没有吃透。。。

 
题意:给定两个等长字符串A、B,一次操作可以将A的任意一段连续的字符变为同一任意字符,问最少多少次操作可以将A变为B。
 
专门挑的区间dp的题,但是然并卵。。。没思路啊。。。看题解。。。
 
思路:dp[i][j]:将空串变为B[i-->j]所需要的最少次数。先求出所有dp[i][j],即求出将空串变为B[i-->j]所需最少次数。那么根据dp数组求出最终答案就很容易:
1.ans[k]=dp[1][k]
 
2.if(A[k]==B[k])   ans[k]=ans[k-1];     (ans[0]==0)
 else  ans[k]=min(ans[k],ans[t]+ans[t+1][k]);  (1=<t<k)
 
求dp[i][j]:
枚举起点i、终点j、遍历[i+1,j]区间k

for(int j=1;j<=len;j++) //end
{
  for(int i=j;i>0;i--) //begin
  {
    dp[i][j]=dp[i+1][j]+1;
    for(int k=i+1;k<=j;k++)
      if(B[i]==B[k])
        dp[i][j]=min(dp[i][j],dp[i][k-1]+dp[k+1][j]);
   }
}

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; char A[],B[];
int dp[][];
int ans[]; int main()
{
while(scanf("%s%s",A+,B+)!=EOF)
{
memset(dp,,sizeof(dp));
memset(ans,,sizeof(ans));
int len=strlen(B+);
for(int j=;j<=len;j++) //end
{
for(int i=j;i>;i--) //begin
{
dp[i][j]=dp[i+][j]+;
for(int k=i+;k<=j;k++) //一定是从左往右找重的
if(B[i]==B[k])
dp[i][j]=min(dp[i][j],dp[i][k-]+dp[k+][j]);
}
}
for(int i=;i<=len;i++)
{
ans[i]=dp[][i];
if(A[i]==B[i])
ans[i]=ans[i-];
else
{
for(int j=;j<i;j++)
ans[i]=min(ans[i],ans[j]+dp[j+][i]);
}
}
printf("%d\n",ans[len]);
}
return ;
}

HDU_2476_String painter_(区间dp)的更多相关文章

  1. hdu_2476_String painter(区间DP)

    题目链接:hdu_2476_String painter 题意: 有a,b两字符串,现在你有一个刷子,每次可以任选一个区间,使这个区间变成你想要的字符,现在让你将a变成b,问最少刷多少次 题解: 考虑 ...

  2. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  3. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  4. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  5. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  6. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

  7. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  8. BZOJ 1260&UVa 4394 区间DP

    题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...

  9. 区间dp总结篇

    前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...

随机推荐

  1. iOS音频播放 (二):AudioSession 转

    原文出处 :http://msching.github.io/blog/2014/07/08/audio-in-ios-2/ 前言 本篇为<iOS音频播放>系列的第二篇. 在实施前一篇中所 ...

  2. powershell 的版本号所引起的载入 FSharp 编译器问题

    powershell 的版本号所引起的载入 FSharp 编译器问题 在 64 位的系统下,大部分系统文件都有 64 位和 32 位的版本号:通常在C:\WINDOWS\system32 下的是 64 ...

  3. org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not

    遇到这个问题之前,我去百度和谷歌去搜索了一下.发现各种说法.可是针对我的项目而言,也就是公司的项目而言,这个问题的根源并不是是网上所说的那样. 最后是通过自己的想法做測试得到了解决. 1.首先说说我的 ...

  4. js坑爹笔试题目汇总(持续更新中)

    把你的面试官问倒,你就是一个合格的面试者了,以下总结一些易错的js笔试题目,会持续更新中.欢迎关注 1,考察this var length = 10 function fn(){ alert(this ...

  5. IIS网站无法启动,提示 另一个程序正在使用此文件

    s还用netstat -ano命令,观察哪一个进程正在使用80端口,任务管理中勾选PID,看看是哪个程序,关掉即可

  6. iOS开发——基础篇——assign,copy,retain之间的区别以及weak和strong的区别

    @property (nonatomic, assign) NSString *title; 什么是assign,copy,retain之间的区别? assign: 简单赋值,不更改索引计数(Refe ...

  7. 混合使用Delphi和C ++(附下载)

    您想将C ++添加到Delphi应用程序中吗?或者将Delphi代码添加到C ++应用程序中?这是如何做. 您可能不知道的一件事是如何在RAD Studio中集成C ++和Delphi语言.您可以将单 ...

  8. 三种解密 HTTPS 流量的方法介绍

    转载自:https://imququ.com/post/how-to-decrypt-https.html作者: Jerry Qu Web 安全是一项系统工程,任何细微疏忽都可能导致整个安全壁垒土崩瓦 ...

  9. MySQL 基本信息的查询(初始化配置信息 my.ini)

    0. my.ini MySQL 的初始化配置信息 mysql 启动时会读取该配置文件,如果按照默认方式安装 mysql 的话,该配置文件在: C:\ProgramData\MySQL\MySQL Se ...

  10. golang中管道热替换

    golang中管道替换问题 https://blog.csdn.net/cyk2396/article/details/78875347 1.运行以下代码: var chan1 chan int va ...