HDU 2476 String painter(区间DP)
String painter
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2792 Accepted Submission(s): 1272
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题目
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
using namespace std;
int dp[105][105];
int ans[105];
char s1[105];
char s2[105];
int main()
{
while(scanf("%s%s",s1+1,s2+1)!=EOF)
{
memset(dp,0,sizeof(dp));
int len=strlen(s1+1);
for(int j=1;j<=len;j++)
{
for(int i=j;i>=1;i--)
{
dp[i][j]=dp[i+1][j]+1;
for(int k=i+1;k<=j;k++)
{
if(s2[k]==s2[i])
dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]);
}
}
}
for(int i=1;i<=len;i++)
ans[i]=dp[1][i];
for(int i=1;i<=len;i++)
{
if(s1[i]==s2[i])
ans[i]=ans[i-1];
else
{
for(int j=i-1;j>=1;j--)
ans[i]=min(ans[i],ans[j]+dp[j+1][i]);
}
}
printf("%d\n",ans[len]);
}
return 0;
}
HDU 2476 String painter(区间DP)的更多相关文章
- HDU 2476 String painter(区间dp)
题意: 给定两个字符串,让求最少的变化次数从第一个串变到第二个串 思路: 区间dp, 直接考虑两个串的话太困难,就只考虑第二个串,求从空白串变到第二个串的最小次数,dp[i][j] 表示i->j ...
- HDU 2476 String painter(区间DP+思维)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意:给你字符串A.B,每次操作可以将一段区间刷成任意字符,问最少需要几次操作可以使得字符串 ...
- hdu 2476"String painter"(区间DP)
传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给定字符串A,B,每次操作可以将字符串A中区间[ i , j ]的字符变为ch, ...
- hdu 2476 (string painter) ( 字符串刷子 区间DP)
String painter Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 2476 String painter (区间DP)
题意:给出两个串a和b,一次只能将一个区间刷一次,问最少几次能让a=b 思路:首先考虑最坏的情况,就是先将一个空白字符串刷成b需要的次数,直接区间DP[i][j]表示i到j的最小次数. 再考虑把a变成 ...
- HDU 2476 String painter(记忆化搜索, DP)
题目大意: 给你两个串,有一个操作! 操作时可以把某个区间(L,R) 之间的所有字符变成同一个字符.现在给你两个串A,B要求最少的步骤把A串变成B串. 题目分析: 区间DP, 假如我们直接想把A变成B ...
- hdu2476 String painter(区间dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2476 Problem Description There are two strings ...
- HDU2476 String painter —— 区间DP
题目链接:https://vjudge.net/problem/HDU-2476 String painter Time Limit: 5000/2000 MS (Java/Others) Me ...
- uva live 4394 String painter 区间dp
// uva live 4394 String painter // // 这一题是训练指南上dp专题的习题,初看之下认为仅仅是稍微复杂了一点 // 就敲阿敲阿敲,两个半小时后,发现例子过了.然而自己 ...
随机推荐
- aspose导出excel文件
using Aspose.Cells; using System; using System.Collections.Generic; using System.Data; using System. ...
- Dubbo -- 系统学习 笔记 -- 示例 -- 泛化引用
Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 泛化引用 泛接口调用方式主要用于客户端没有API接口及模型类元的情况,参数及返回值 ...
- 【NLP】HanLP环境
1.参考:https://github.com/hankcs/pyhanlp 2.问题: C:\Users\ADMINI~1\AppData\Local\Temp\pip-install-u617cf ...
- 数字图像处理笔记与体会(一)——matlab编程基础
最近开始学习数字图像处理,使用matlab实现,下面我就来记录笔记和体会,一方面是给大家提供参考,另一方面是防止我忘记了. 复习一下: 1.数字图像是用一个数字矩阵来表示的,数字阵列中的每个数字,表示 ...
- Ubuntu输入法切换问题
不知道改了个什么东西,Ubuntu 15.04 中Ctrl+Space不能切换输入法了,因此不能输入英文,shell就更是没法工作,在设置里面找了好久,“文本输入”/“语言支持”/“键盘”里面都没找到 ...
- java虚拟机性能监控调优及原则
摘抄 http://uule.iteye.com/blog/2114697 一.JVM内存模型及垃圾收集算法 1.根据Java虚拟机规范,JVM将内存划分为: New(年轻代) Tenured(年老 ...
- centos7/nginx/tornado错误异常收集
临时方法 – 设置系统参数 使用命令setenforce 附: setenforce 设置SELinux 成为enforcing模式 setenforce 设置SELinux 成为permissive ...
- 安装windows7/8/10到U盘或移动硬盘
https://jingyan.baidu.com/article/e52e36156f6ad240c60c518c.html jpg改rar
- 使用Phoenix通过sql语句更新操作hbase数据
hbase 提供很方便的shell脚本,可以对数据表进行 CURD 操作,但是毕竟是有一定的学习成本的,基本上对于开发来讲,sql 语句都是看家本领,那么,有没有一种方法可以把 sql 语句转换成 h ...
- Android 自动化测试 robotium
转:http://xiaomaozi.blog.51cto.com/925779/908886 Android 的开发可以说已经遍地都是,不说精致的app,只要看些书,看点教学视频,学习二至三个月,都 ...