题目链接: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. manage account

    #!/bin/bash # #Delete_user - Automates the steps to remove an account # ############################ ...

  2. php面向对象:封装

    OOP三大特性:封装.继承.多态. 封装的目的:为了让类更安全封装的做法:1.类里面的成员变量做为private2.使用成员方法来间接访问成员变量3.在该方法里面加限制条件 注意:php类里面不允许出 ...

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

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

  4. 142. Linked List Cycle II

    题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...

  5. HTML标记之Form表单

    一.表单的作用 从访问的Web站点的用户那里获得信息.访问者可以使用诸如文本域.列表框.复选框以及单选按钮之类的表单元素输入信息,然后单击某个按钮提交这些信息.是客户端与服务器端的交流途径. 二.说明 ...

  6. September 23rd 2016 Week 39th Friday

    Even a small star shines in the darkness. 星星再小,也会发光. In the darkness, even a small star can shine. N ...

  7. MongoDB配置文件YAML-based选项全解

    配置文件部分 MongoDB引入一个YAML-based格式的配置文件.2.4版本以前的仍然兼容. 我的mongodb配置文件: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  8. chaper3_exerise_Uva10340_子字串

    #include<iostream> #include<string> #include<cstring> using namespace std; int Fin ...

  9. java.util.ConcurrentModificationException

    遍历 List 的时候将 item remove 掉会抛出此异常

  10. hdu1212(大数取模)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1212 题意:给出两个数a, b,求a%b: 思路:(c+d)%e=c%e+d%e,(c*d)%e=(c ...