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. openTSDB ConnectionManager: Unexpected exception from downstream java.io.IOException: Broken pipe

    openTSDB有这种错误: ConnectionManager: Unexpected exception from downstream for [id: 0xf85323a8, /10.65.3 ...

  2. Boss OpenCart 商城自适应主题模板 ABC-0012-01

    Boss OpenCart 商城自适应主题模板 ABC-0012-01 模板特性兼容浏览器IE7, IE8, IE9, IE10, Firefox, Safari, Chrome OpenCart版本 ...

  3. scikit-learn:3.3. Model evaluation: quantifying the quality of predictions

    參考:http://scikit-learn.org/stable/modules/model_evaluation.html#scoring-parameter 三种方法评估模型的预測质量: Est ...

  4. debug 和release 的区别

    http://blog.csdn.net/h_wlyfw/article/details/26688677

  5. 为JMenu中的JPopupMenu定制透明背景

    最近研究了很久这个问题,从LookAndFeel到继承JPopupMenu或者JMenu都搞不定. 其实替换背景的话,只要在JMenuUI中设置Opaque(false) 再将背景设置透明就可以看到P ...

  6. POJ 1060:Modular multiplication of polynomials

    Modular multiplication of polynomials Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4 ...

  7. 洛谷P1531 I Hate It

    题目背景 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 题目描述 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的 ...

  8. SQLAlchemy 反向生成 model 模型

    前言 Django 反向生成的 model 模型的命令 :  python manager.py inspectdb SQLAlchemy / Flask-SQLAlchemy则是: pip3 ins ...

  9. CSS里#和.以及大小写

    # 选定ID .  选定class   大小写严格区分,因此选定class和设定class等要一致  

  10. bzoj 2662: [BeiJing wc2012]冻结【分层图+spfa】

    死活想不到分层图emmm 基本想法是建立分层图,就是建k+1层原图,然后相邻两层之间把原图的边在上一层的起点与下一层的终点连起来,边权为val/2,表示免了这条边的边权,然后答案就是第0层的s到k层的 ...