HDU 2476 String painter (区间DP)
题意:给出两个串a和b,一次只能将一个区间刷一次,问最少几次能让a=b
思路:首先考虑最坏的情况,就是先将一个空白字符串刷成b需要的次数,直接区间DP[i][j]表示i到j的最小次数。
再考虑把a变成b的次数 ans[i]:a从(0,i)变成b(0,i)所需的最小次数
初始化ans[i]=dp[0][i]
如果a[i]==b[i],则ans[i]=ans[i-1];
由小区间更新到大区间
//#pragma comment(linker, "/STACK:167772160")//手动扩栈~~~~hdu 用c++交
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
// #include<malloc.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
#define LL long long
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
// const double pi = acos(-1);
const LL MOD = ;
const int N = ; // inline int r(){
// int x=0,f=1;char ch=getchar();
// while(ch>'9'||ch<'0'){if(ch=='-') f=-1;ch=getchar();}
// while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
// return x*f;
// }
char a[N],b[N];
int dp[N][N];
int ans[N]; int main(){
while(~scanf("%s%s",a,b)){
int n=strlen(a);
clc(dp,);
for(int i=;i<n;i++){
for(int j=i;j<n;j++){
dp[i][j]=j-i+;
}
} for(int len=;len<n;len++){
for(int i=;i<n&&i+len<n;i++){
int j=i+len;
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]);
}
}
} if(a[]==b[]) {
ans[]=;
}
else
ans[]=;
for(int i=;i<n;i++){
ans[i]=dp[][i];
if(a[i]==b[i]){
ans[i]=ans[i-];
}
else{
for(int k=;k<i;k++){
ans[i]=min(ans[i],ans[k]+dp[k+][i]);
}
}
}
printf("%d\n",ans[n-]);
}
return ;
}
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) Tota ...
- 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)
题目大意: 给你两个串,有一个操作! 操作时可以把某个区间(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专题的习题,初看之下认为仅仅是稍微复杂了一点 // 就敲阿敲阿敲,两个半小时后,发现例子过了.然而自己 ...
随机推荐
- ASP.NET遍历textbox文本框
Asp.Net如何遍历所有TextBox控件并清空 asp.net 不能像window那样直接遍历this.Controls就可以了,因为: this.Controls只是包含了Page根一级的con ...
- MySQL分区表(转)
查看分区情况 SELECT * FROM information_schema.PARTITIONS WHERE table_name='table_name': PARTITION_NAME:分区的 ...
- uva 11039
水题 排序 判符号 #include <cstdio> #include <cstring> #include <algorithm> using namespa ...
- Linq基本用法
- Kafka操作
http://blog.csdn.net/xiao_jun_0820/article/details/46831203 http://blog.csdn.net/xcockroach/article/ ...
- [itint5]完全二叉树节点个数的统计
http://www.itint5.com/oj/#4 这题是利用完全二叉树的性质计算节点数目.那么是通过比较左右子树的最左结点的高度来看那边是满的,然后递归计算. //使用getLeftChildN ...
- HeadFirst设计模式之组合模式
一. 1.The Composite Pattern allows us to build structures of objects in the form of trees that contai ...
- 删除appcompat_v7会出很多错误
创建工程的时候会出现appcompat_v7这个文件夹 手贱删除后,发现出错了 说明test项目是依赖于appcompat_v7包的,所以这个appcompat_v7包是不能被删除的. appcomp ...
- WPF之Behavior
本文主要是以实现拖动元素作为例子. 创建Behavior: 通常这个类会继承自Behavior<T>,其中T就是此Behavior服务的对象,在此处使用的是UIElement,也就是虽有的 ...
- 过虑器 ThreadLocal 权限 监听器 观察者模式
数据的压缩 GzipOutputStream - > > ByteArrayOutputStream. 以下是在某个servlet中对指定的数据进行压缩 package cn.itcast ...