Codeforces 219C - Color Stripe - [DP]
题目链接:http://codeforces.com/problemset/problem/219/C
题意:
给你 $n$ 个方块排成水平一排,每个方块都涂上 $k$ 种颜色中的一种。要求对尽量少的方块进行重新涂色,使得任意两个方块的颜色不同。
题解:
$dp[i][x]$ 表示前 $i$ 个方块,第 $i$ 个方块颜色是 $x$,最少重新涂色多少个方块使得满足要求。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=5e5+;
const int maxk=;
int n,k;
char in[maxn],out[maxn];
int dp[maxn][maxk];
int pre[maxn][maxk];
int main()
{
cin>>n>>k;
scanf("%s",in+); for(int i=;i<=n;i++)
{
for(int v=;v<=k;v++)
{
dp[i][v]=INF;
int flag=(in[i]-'A'+!=v);
for(int u=;u<=k;u++)
{
if(u==v) continue;
if(dp[i][v]>dp[i-][u]+flag)
{
dp[i][v]=dp[i-][u]+flag;
pre[i][v]=u;
}
}
}
} int ans=INF, clr;
for(int i=;i<=k;i++) if(ans>dp[n][i]) ans=dp[n][i], clr=i;
printf("%d\n",ans);
for(int i=n;i>=;i--) out[i]='A'+clr-, clr=pre[i][clr];
printf("%s\n",out+);
}
Codeforces 219C - Color Stripe - [DP]的更多相关文章
- CodeForces 219C Color Stripe
Color Stripe Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submi ...
- Codeforces 219C Color Stripe(思维+字符串)
题目链接:http://codeforces.com/problemset/problem/219/C 题目大意: 给出一个字符串,只包含k种字符,问最少修改多少个字符(不增长新的种类)能够得到一个新 ...
- CF--思维练习--CodeForces - 219C Color Stripe (思维)
ACM思维题训练集合 A colored stripe is represented by a horizontal row of n square cells, each cell is paine ...
- PAT 1045 Favorite Color Stripe[dp][难]
1045 Favorite Color Stripe (30)(30 分) Eva is trying to make her own color stripe out of a given one. ...
- PAT 甲级 1045 Favorite Color Stripe(DP)
题目链接 Favorite Color Stripe 题意:给定$A$序列和$B$序列,你需要在$B$序列中找出任意一个最长的子序列,使得这个子序列也是$A$的子序列 (这个子序列的相邻元素可以重复) ...
- 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe
题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...
- PAT 甲级 1045 Favorite Color Stripe (30 分)(思维dp,最长有序子序列)
1045 Favorite Color Stripe (30 分) Eva is trying to make her own color stripe out of a given one. S ...
- 【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)
题意: 输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个 ...
- 1045 Favorite Color Stripe (30分)(简单dp)
Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...
随机推荐
- Spring全局异常处理的三种方式
在J2EE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的.不可预知的异常需要处理.每个过程都单独处理异常,系统的代码耦合度高,工作 ...
- 一起SQL引发OOM的解决思路与过程(转载)
在TOMCAT WEB程序的运行过程中,突然触发了内存溢出错误,检查Tomcat的localhost日志,找到如下信息: java.lang.OutOfMemoryError: Java heap s ...
- MUI class="mui-switch"开关 JQuery 控制开关
<div class="mui-switch mui-active"> <div class="mui-switch-handle">& ...
- oradim新建服务后,登录数据库报ORA-12560错误
> oradim -new -sid mydb 实例已创建. > sqlplus /nolog SQL*Plus: Release 11.2.0.4.0 Production on 星期二 ...
- javascript es6 箭头函数
1.箭头函数示例 let add = (a,b) => a + b //没有语句块时,默认作为返回值 add(1,2); var multi = (a,b) => {ret ...
- Jenkins Post Build网址
Hudson Post build taskhttps://plugins.jenkins.io/postbuild-taskThis plugin allows the user to execut ...
- django 返回 code 405
return HttpResponse('Invalid Method',status=405)
- MXNET:欠拟合、过拟合和模型选择
当模型在训练数据集上更准确时,在测试数据集上的准确率既可能上升又可能下降.这是为什么呢? 训练误差和泛化误差 在解释上面提到的现象之前,我们需要区分训练误差(training error)和泛化误差( ...
- 前端项目微金所1 - bootstrap模板,Compatible(兼容),Viewport(视口),条件注释,第三方依赖,MediaQuery媒体查询
前端项目微金所笔记1 基础的bootstrap模板 <!DOCTYPE html> <html lang="en"> <head> <me ...
- Android wpa_supplicant 四次握手 流程分析
记录wpa_supplicant四次握手的过程. 相关log:https://www.cnblogs.com/helloworldtoyou/p/9633603.html 接收到第一次握手,会设置一个 ...