题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2476

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

思路:区间dp,Orz,这应该算是我第一道接触的区间dp的题目,刚刚看到题目的时候一点思路都没有。没办法,只能看着别人的代码硬扣!

刚接触新的知识的时候,肯定是一个挺痛苦的过程,但是请相信,只要坚持下去就会有收获的!骨头再硬我都会啃下来的。加油 区间dp!!

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
const int maxn=1e5+;
const int INF=0x3f3f3f3f; char str1[],str2[];
int dp[][];
int ans[]; int main()
{
//freopen("in.txt","r",stdin);
while(cin>>str1+>>str2+)
{
int n=strlen(str1+);
for(int i=;i<=n;i++) dp[i][i]=;
for(int d=;d<n;d++)
for(int i=;i+d<=n;i++){
int j=i+d;
dp[i][j]=dp[i+][j]+;
for(int k=i+;k<=j;k++)
if(str2[i]==str2[k])
dp[i][j]=min(dp[i][j],dp[i+][k]+dp[k+][j]);
}
for(int i=; i<=n; i++)ans[i]=dp[][i];
for(int i=; i<=n; i++)
if(str1[i]==str2[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[n]);
}
return ;
}

hdu2476 String painter(区间dp)的更多相关文章

  1. HDU2476 String painter —— 区间DP

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

  2. HDU2476 String painter——区间DP

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

  3. uva live 4394 String painter 区间dp

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

  4. HDU 2476 String painter(区间dp)

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

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

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

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

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

  7. HDU2476 String painter(DP)

    题目 String painter 给出两个字符串s1,s2.对于每次操作可以将 s1 串中的任意一个子段变成另一个字符.问最少需要多少步操作能将s1串变为s2串. 解析 太妙了这个题,mark一下. ...

  8. uva live 4394 String painter 间隔dp

    // uva live 4394 String painter // // 问题是,在培训指导dp运动主题,乍一看,我以为只是一点点复杂 // A A磕磕磕,两个半小时后,.发现超过例子.然而,鉴于他 ...

  9. HDU2476 String painter

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

随机推荐

  1. mybatis一对多查询

    18 <!-- 19 方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集 20 封装联表查询的数据(去除重复的数据) 21 select * from class c, teacher ...

  2. 【python】dict4ini和xmltodict模块用途

    dict4ini模块:可以读写配置文件 xmltodict模块:将xml和json互相转换  https://pypi.python.org/pypi/xmltodict

  3. 【python】多进程学习

    来源:廖雪峰 讲解看来源吧 把例子记一下 1.用fork创建进程 import os print "Process (%s) start..." % os.getpid() pid ...

  4. HDU 4793 Collision (解二元一次方程) -2013 ICPC长沙赛区现场赛

    题目链接 题目大意 :有一个圆硬币半径为r,初始位置为x,y,速度矢量为vx,vy,有一个圆形区域(圆心在原点)半径为R,还有一个圆盘(圆心在原点)半径为Rm (Rm < R),圆盘固定不动,硬 ...

  5. python getopt.getopt 不能精确匹配的问题

    代码:opts,argv = getopt.getopt(sys.argv[1:],('u:'),['ad','join','passwd=','domain=','dip=','test','ip= ...

  6. JS倒计时,不会重复执行

    直接上代码,亲自测试了的,没问题咯 <html> <head> <title>点击获取验证码按钮后按钮变灰,倒计时一段时间后又可重复点击</title> ...

  7. Json数据报错

    在Json数据解析的时候报错,如下图: 这里的id,code,A1等等,都应该使用双引号,即:"id","code","A1",假如id后面 ...

  8. python基础——使用模块

    python基础——使用模块 Python本身就内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用. 我们以内建的sys模块为例,编写一个hello的模块: #!/usr/bin/env ...

  9. 自定义UIDatePikerView

    1.添加文件GoYearMonthDayPickerView.h .m .xib.NSDate+Helper.h .m.iCarousel.h .m 2.在Lable上显示日期 UILabel *ag ...

  10. UISplitViewController - iPad分屏视图控制器

    UISplitViewController - 分屏视图控制器 概述 UISplitViewController 是一个容器vc, 展示一个 master-detail(主-详(从))界面. 主视图改 ...