题目链接: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]的更多相关文章

  1. CodeForces 219C Color Stripe

    Color Stripe Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submi ...

  2. Codeforces 219C Color Stripe(思维+字符串)

    题目链接:http://codeforces.com/problemset/problem/219/C 题目大意: 给出一个字符串,只包含k种字符,问最少修改多少个字符(不增长新的种类)能够得到一个新 ...

  3. CF--思维练习--CodeForces - 219C Color Stripe (思维)

    ACM思维题训练集合 A colored stripe is represented by a horizontal row of n square cells, each cell is paine ...

  4. 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. ...

  5. PAT 甲级 1045 Favorite Color Stripe(DP)

    题目链接 Favorite Color Stripe 题意:给定$A$序列和$B$序列,你需要在$B$序列中找出任意一个最长的子序列,使得这个子序列也是$A$的子序列 (这个子序列的相邻元素可以重复) ...

  6. 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe

    题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...

  7. 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 ...

  8. 【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)

    题意: 输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个 ...

  9. 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 ...

随机推荐

  1. Swift Guard 守护

    前言 guard 语句和 if 语句有点类似,都是根据其关键字之后的表达式的布尔值决定下一步执行什么. guard 语句只会有一个代码块,不像 if 语句可以 if else 多个代码块. guard ...

  2. R语言手册

    在R的官方教程里是这么给R下注解的:一个数据分析和图形显示的程序设计环境(A system for data analysis and visualization which is built bas ...

  3. 12C新特性 -- 共享asm口令文件

    12C中,ASM口令文件,可以提供本地.远程登录asm的验证.当然,要想使用asm口令文件验证,必须为每个asm创建一个口令文件. 如果是使用asm存储,asmca在配置asm磁盘组的会后,会自动为a ...

  4. MemoryFile偷取安卓内存

    参考链接:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2016/0111/3859.html MemoryFile memoryFile ...

  5. 菜鸟学Java(二十二)——重新认识泛型

    泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以用在类.接口和方法的创建中,分别称为泛型类.泛型接口.泛型方法. Java语言引 ...

  6. 业界常用的和不常用cad快捷键

    AutoCAD 是目前世界各国工程设计人员的首选设计软件,简便易学.精确无误是AutoCAD成功的两个重要原因.AutoCAD提供的命令有很多,绘图时最常用的命令只有其中的百分之二十. 在CAD软件操 ...

  7. java 中使用log4j

    一.控制台使用 1.导入log4j包到工程中 2.配置: log4j.rootLogger=DEBUG,console,R log4j.appender.console=org.apache.log4 ...

  8. hdoj:2027

    #include <iostream> #include <string> #include <vector> using namespace std; int m ...

  9. CFA一级知识点总结

    更多来自:   www.vipcoursea.com   Ethics 部分 Objective of codes and standard:永远是为了maintain public trust in ...

  10. 浏览器和服务器 对http请求(post get) url长度限制

    1. GET  URL长度限制 在Http1.1协议中并没有提出针对URL的长度进行限制,RFC协议里面是这样描述的,HTTP协议并不对URI的长度做任何的限制,服务器端 必须能够处理任何它们所提供服 ...