题目链接:https://vjudge.net/problem/HDU-2476

String painter

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

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
 
Source
 
Recommend
lcy

题意:

给出A字符串和B字符串。每次操作可以把A串某个区间的字符变成同一种字符(自己选),问最少需要操作多少次,就能把A串变成B串?

题解:

1.先求出把一个空白串刷成B串所需要的最少操作次数,并且不仅仅是整个区间的最少操作次数需要记录,而且每个子区间的最少操作次数也需要记录。记录在dp[l][r]数组中。(怎么用最少的操作次数把空白串刷成目标串?LightOJ - 1422 Halloween Costumes

2.A串与空白串所不同的地方在于:A串在某些地方可能与B串相同,在这些地方,A串就不要再去刷了,而空白串则必须要刷。所以A串的最少操作次数就可以这样求:

从第一个位置开始递推,假设当前递推到第i个位置。

1) 如果在第i个位置上,A串与B串相同,那么在i处就不需要处理,直接 f[i] = f[i-1] 。

2) 如果在第i个位置上,A串与B串不同,那么表明第i个字符必须刷,要刷的话,就要考虑刷多少,即需要考虑往前刷多少个?枚举取最优值。

3.一开始想用记忆化搜索去写把空串刷成B串的。但由于要利用dp[][]数组,而记忆化搜索又不能把所有信息都准确记录到dp数组上(如l>r时或者下标越界时),所以就写成递推的形式。

4.疑问:为什么可以从第一个位置开始递推,而不是也如上面那样要每个子区间都要求出来?原理是什么?跟这个相似吗?SCUT125 华为杯 D.笔芯回文

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; char a[MAXN], b[MAXN];
int dp[MAXN][MAXN], f[MAXN]; int main()
{
while(scanf("%s%s",a+, b+)!=EOF)
{
int n = strlen(a+);
memset(dp, , sizeof(dp)); for(int i = ; i<=n; i++)
dp[i][i] = ;
for(int len = ; len<=n; len++) //先求出把空串刷成目标串所需要的最少次数
{
for(int l = ; l<=n-len+; l++)
{
int r = l+len-;
dp[l][r] = +dp[l+][r];
for(int k = l+; k<=r; k++)
if(b[l]==b[k])
dp[l][r] = min(dp[l][r], dp[l][k-]+dp[k+][r]);
}
} f[] = ;
for(int i = ; i<=n; i++) //再求出已有串刷成目标串的最少次数。
{
f[i] = i; //初始化一下
if(a[i]==b[i]) f[i] = f[i-]; //如果已有串与目标串在i处相等,则此处可以不用处理,这就是空串与已有串不同的地方
else for(int k = ; k<=i; k++) //否则,就要对i处进行刷色。刷多少呢?可知终点为i,枚举起点k,取最优值。
f[i] = min(f[i], f[k-]+dp[k][i]);
}
printf("%d\n", f[n]);
}
}

HDU2476 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. 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. 一个java定时器框架

    ScheduleIterator接口 import java.util.Date; public interface ScheduleIterator {    public Date next(); ...

  2. Can't connect to X11 window server using 'localhost:0.0' 的解决

    Can't connect to X11 window server using 'localhost:0.0' 的解决 http://lufei-99999.blog.163.com/blog/st ...

  3. centos 下完全卸载 mysql5.6

    查看已经安装的服务 rpm –qa|grep -i mysql -i 作用是不区分大小写 yum remove mysql mysql-server mysql-libs compat-mysql51 ...

  4. java内部类理解使用

    这是我学习Java内部类的笔记 1.为什么使用内部类?使用内部类最吸引人的原因是:每个内部类都能独立地继承一个(接口的)实现,所以无论外围类是否已经继承了某个(接口的)实现,对于内部类都没有影响1.1 ...

  5. 使用RTL-SDR,从打开一个车门到批量打开车门

    在最近几年,入侵汽车在当代社会的黑客圈中成为热点,很多文章表明汽车产业还有很多东西等待完善,在本篇文章中,我会让你熟悉我一直研究的一些概念,以及如何在网状网络中使用一些便宜的部件渗透远程开门系统. 软 ...

  6. 有方向的运动js

    <!doctype html> <html lang="en"> <head>     <meta charset="UTF-8 ...

  7. Deleting array elements in JavaScript - delete vs splice

    javascript 数组中删除元素用 array.splice(start, deleteCount);这个方法. ----------------------------------------- ...

  8. chrome插件网站

    chrome插件网站 http://chromecj.com/

  9. XStream 数组(List)输出结构

    <!-- 期望的DOM树 --> <Articles> <item> <Title>微信SDK初步结构</Title> <Descri ...

  10. IP协议解读(二)

    IP协议是TCP协议栈中的核心协议,也是网络编程的基础之中的一个. 我们接着在IP协议解读(一)继续学习 网络层作用 IP分片: IP数据报的长度超过帧的MTU时,将会被分片传输. 分片可能发生在发送 ...