[一道区间dp][String painter]
http://acm.hdu.edu.cn/showproblem.php?pid=2476
String painter
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6863 Accepted Submission(s): 3330
The first line contains string A.
The second line contains string B.
The length of both strings will not be greater than 100.
abcdefedcba
abababababab
cdcdcdcdcdcd
题意:有两个字符串,A串和B串,每次可以对A串一个区间进行涂改,使该区间所有字母变成任意一种字母,求使A串变成B串需要的最少操作次数
题解:首先考虑一个简化的问题,把一个空串涂改成B串需要的操作数,显然可以通过最基本的区间dp进行解决,转移方程为if(B[i]==B[k])dp[i][j]=min(dp[i][j],dp[i][k-1]+dp[k+1][j]);else dp[i][j]=min(dp[i][j],min(dp[i][k]+dp[k+1][j],dp[i][k-1]+dp[k][j]));然后考虑A串不是空串,那么如果A[i]==B[i],则有ans[i]=ans[i-1],如果A[i]!=B[i],那么ans[i]=min(ans[j]+dp[j][i])。
普通的循环迭代版本
#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<"["<<#x<<"]"<<" is "<<x<<endl;
char ch[],ch2[];
int dp[][],ans[];
const int inf=1e8;
int main(){
while(scanf("%s",ch+)!=EOF){
scanf("%s",ch2+);
int len=strlen(ch+);
for(int i=;i<=len;i++){
for(int j=;j<=len;j++){
if(i>j)dp[i][j]=;
else if(i==j)dp[i][j]=;
else dp[i][j]=inf;
}
}
for(int i=;i<=len;i++){
for(int j=;j+i-<=len;j++){
for(int k=j+;k<=j+i-;k++){
if(ch2[j]==ch2[k])dp[j][j+i-]=min(dp[j][j+i-],dp[j][k-]+dp[k+][j+i-]);
else dp[j][j+i-]=min(dp[j][j+i-],min(dp[j][k]+dp[k+][j+i-],dp[j][k-]+dp[k][j+i-]));
}
}
}
for(int i=;i<=len+;i++){
ans[i]=inf;
}
ans[]=;
for(int i=;i<=len;i++){
if(ch[i]==ch2[i]){
ans[i+]=min(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[len+]);
}
return ;
}
记忆化搜索版本(注意由于sol(1,len)只能保证dp[1][len]被更新,而不能保证所有的dp[i][j]被遍历到,所以需要使用n^2次sol(i,j)保证所有dp[i][j]都被更新了而不再是初始值)
#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<"["<<#x<<"]"<<" is "<<x<<endl;
char ch[],ch2[];
int dp[][],ans[];
const int inf=1e8;
int sol(int l,int r){
if(dp[l][r]!=0x3f3f3f3f)return dp[l][r];
if(l>r)return dp[l][r]=;
if(l==r)return dp[l][r]=;
for(int k=l+;k<=r;k++){
if(ch2[k]==ch2[l]){
dp[l][r]=min(dp[l][r],sol(l+,k)+sol(k+,r));
}
else{
dp[l][r]=min(dp[l][r],sol(l+,r)+);
}
}
return dp[l][r];
}
int main(){
while(scanf("%s",ch+)!=EOF){
scanf("%s",ch2+);
int len=strlen(ch+);
memset(dp,0x3f3f3f3f,sizeof(dp));
for(int i=;i<=len;i++){
for(int j=i;j<=len;j++){
sol(i,j);
}
}
// sol(1,len);
for(int i=;i<=len+;i++){
ans[i]=0x3f3f3f3f;
}
ans[]=;
for(int i=;i<=len;i++){
if(ch[i]==ch2[i]){
ans[i+]=min(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[len+]);
}
return ;
}
[一道区间dp][String painter]的更多相关文章
- HDU 2476 区间DP String painter
题解 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm ...
- 又一道区间DP的题 -- P3146 [USACO16OPEN]248
https://www.luogu.org/problemnew/show/P3146 一道区间dp的题,以区间长度为阶段; 但由于要处理相邻的问题,就变得有点麻烦; 最开始想了一个我知道有漏洞的方程 ...
- 再一道区间DP -- P4170 [CQOI2007]涂色
https://www.luogu.org/problemnew/show/P4170 一道简单的区间DP,注意读入 #include <bits/stdc++.h> #define up ...
- 还一道区间DP -- MZOJ 1346: 不老的传说
http://10.37.2.111/problem.php?id=1346 与上一道染色基本一样,就加了个限制条件(一次最多刷maxd) #include <bits/stdc++.h> ...
- 一道区间DP的水题 -- luogu P2858 [USACO06FEB]奶牛零食Treats for the Cows
https://www.luogu.org/problemnew/show/P2858 方程很好想,关键我多枚举了一次(不过也没多大关系) #include <bits/stdc++.h> ...
- 区间dp的典例
区间dp, 属于dp的一种,顾名思义,便是对区间处理的dp,其中石子归并,括号匹配,整数划分最为典型. (1)石子归并 dp三要素:阶段,状态,决策. 首先我们从第i堆石子到第j堆石子合并所花费的最小 ...
- HDU4632:Palindrome subsequence(区间DP)
Problem Description In mathematics, a subsequence is a sequence that can be derived from another seq ...
- POJ2955:Brackets(区间DP)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- POJ1179Polygon(区间dp)
啊~~ 被dp摁在地上摩擦的人 今天做了一道区间dp的题(POJ1179Polygon) 题目: Polygon Time Limit: 1000MS Memory Limit: 10000K T ...
随机推荐
- 转:SLAM算法解析:抓住视觉SLAM难点,了解技术发展大趋势
SLAM(Simultaneous Localization and Mapping)是业界公认视觉领域空间定位技术的前沿方向,中文译名为“同步定位与地图构建”,它主要用于解决机器人在未知环境运动时的 ...
- vue element-ui typescript tree报错 === Property 'getCheckedNodes' does not exist on type 'Element | Element[] | Vue | Vue[]'.
import { Tree } from 'element-ui' const ref = <Tree>this.$refs.tree ref.getCheckedNodes()
- python读写文件的操作
编程语言中,我们经常会和文件和文件夹打交道,这篇文章主要讲的是Python中,读写文件的常用操作: 一.打开文件 openFile = open('../Files/exampleFile.txt', ...
- Activate注解
Activate注解 被该注解修饰的接口,扩展类可能会被加载 ProtocolFilterWrapper.buildInvokerChain @Documented @Retention(Retent ...
- Scratch 3.0,新在哪里?
大家期待已久的Scratch 3.0,已经上线一段时间了. 学生们可轻松通过连接WeDo2.0和EV3机器人 进行scratch编程学习啦! 或许有些朋友还不太了解Scratch,没关系,小乐今天就为 ...
- VS2013+WDK8.1 驱动开发环境配置
Windows Driver Kit 是一种完全集成的驱动程序开发工具包,它包含 WinDDK 用于测试 Windows 驱动器的可靠性和稳定性,本次实验使用的是 WDK8.1 驱动开发工具包,该工具 ...
- 通俗化理解Spring3 IoC的原理和主要组件
♣什么是IoC? ♣通俗化理解IoC原理 ♣IoC好处 ♣工厂模式 ♣IoC的主要组件 ♣IoC的应用实例 ♣附:实例代码 1.什么是IoC(控制反转)? Spring3框架的核心是实现控制反转( ...
- Java BinarySearch
Java BinarySearch /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternation ...
- SQL优化中的重要概念:锁定
原文:SQL优化中的重要概念:锁定 上篇文章讲的是事务,这篇就引出另一个重要概念,就是锁定. 当一个用户要读取另一个用户正在修改的数据,或者一个用户正在修改另一个用户正在读取的数据,或者一个用户要修改 ...
- XML-RPC-1概述
XML-RPC是一个远程过程调用(远端程序呼叫)(remote procedure call,RPC)的分布式计算协议,通过XML将调用函数封装,并使用HTTP协议作为传送机制. 中文名 XML- ...