博客
关于我
【区间dp】HDU 2476 String painter
阅读量:633 次
发布时间:2019-03-14

本文共 1625 字,大约阅读时间需要 5 分钟。

/*data: 2016/11/08writer: cn_swords题意:给你A,B两字符串,你一次操作可以将A一段全变为一个字符,问你最小次数把A变成B。题解:dp[l][r]代表区间(l,r)里,最小次数把这个区间的A变成B。当检查l位置A与B的时候,如果相同,dp[l][r] = dp[l+1][r];如果不同,初始化dp[l][r] = dp[l+1][r]+1,然后需要在区间(l+1,r)查找B[k]下是否有A[l]字符,dp[l][r] = dp[l+1][k]+dp[k+1][j]。但是这样是不正确的,因为(l,k)的区间改变会改变A串。正解: dp[l][r]代表区间(l,r)里,最小次数把这个区间的空串变成B。dp[l][r] = dp[l+1][r]+1; if(a[k] == a[l]) dp[l][r] = dp[l-1][k-1] + dp[k][r];( l+1 <= k <= r);处理完后,枚举区间(1,n),sum[i]代表处理前i个字符的最小处理次数。*/#include 
#include
#include
using namespace std;const int INF = 0x3f3f3f3f;const int N = 105;char a[N],b[N];int sum[N];int dp[N][N];int main(){ while(~scanf("%s%s",a,b)) { memset(dp,0,sizeof(dp)); int n = strlen(b); for(int len = 0; len < n; len++) { for(int l = 0; l+len < n; l++) { if(len == 0) { dp[l][l] = 1; continue; } int r = l+len; dp[l][r] = dp[l+1][r]+(b[l] == b[l+1]?0:1); for(int k = l+1; k <= r; k++) { if(b[k] == b[l]) dp[l][r] = min(dp[l][r],dp[l+1][k-1]+dp[k][r]); } } } //printf("%d\n",dp[0][n-1]); //int ans = INF; for(int i = 0; i < n; i++) { if(a[i] == b[i]) sum[i] = sum[i-1]; else sum[i] = dp[0][i]; for(int j = 0; j < i; j++) sum[i] = min(sum[i],sum[j]+dp[j+1][i]); } printf("%d\n",sum[n-1]); } return 0;}

转载地址:http://rfaoz.baihongyu.com/

你可能感兴趣的文章
Nmap渗透测试指南之指纹识别与探测、伺机而动
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>