题目链接: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. 在Asp.Net中操作PDF – iTextSharp - 操作图片

    iTextSharp支持所有主流的图片格式,比如:jpg, tif, gif, bmp, png和wmf.在iTextSharp中使用Image.GetInstance()方法创建图片有很多种方式,或 ...

  2. Duplicate Manager Pro for Mac(重复文件查找工具)破解版安装

    1.软件简介    Duplicate Manager Pro 是 macOS 系统上一款重复文件查找工具,可以帮你在 Mac 电脑上查找出磁盘上面的重复文件,然后让你对这些重复文件进行判断并删除,使 ...

  3. 实战c++中的vector系列--vector&lt;unique_ptr&lt;&gt;&gt;初始化(全部权转移)

    C++11为我们提供了智能指针,给我们带来了非常多便利的地方. 那么假设把unique_ptr作为vector容器的元素呢? 形式如出一辙:vector<unique_ptr<int> ...

  4. 基础007_FIFO原理

    作者:桂. 时间:2018-05-13  05:51:13 链接:http://www.cnblogs.com/xingshansi/p/9030879.html 前言 主要梳理FIFO的特性,fif ...

  5. RandomAccessFile类理解

    一.简述 这个是JDK上的截图,我们可以看到它的父类是Object,没有继承字节流.字符流家族中任何一个类.并且它实现了DataInput.DataOutput这两个接口,也就意味着这个类既可以读也可 ...

  6. [svc]find+xargs/exec重命名文件后缀&文件操作工具小结

    30天内的文件打包 find ./test_log -type f -mtime -30|xargs tar -cvf test_log.tar.gz awk运算-解决企业统计pv/ip问题 find ...

  7. Fluent动网格【5】:部件变形

    在动网格中关于部件运动除了指定刚体运动外,有时还需要指定某些边界的变形,这种情况经常会遇到,尤其是与运动部件存在相连接边界的情况下,如下图中边界1运动导致与之相连的边界2和边界3发生变形. Fluen ...

  8. java中job运行时间

    long startTime=System.currentTimeMillis();//记录开始时间 method();//此处为你调用的方法 long endTime=System.currentT ...

  9. JVM——Java HotSpot VM Options

    JVM常用参数 参数名称 含义 默认值  描述 -Xms 初始堆大小 物理内存的1/64(<1GB) 默认(MinHeapFreeRatio参数可以调整)空余堆内存小于40%时,JVM就会增大堆 ...

  10. C艹目录

    c++ 学习路线  c++学习路线 c++ 学习目录 c++ 常用数据类型,命名规则, 不常有数据类型 C++复合类型(数组) C艹复合类型(字符串) C++复合类型(结构体) C++ 结构体和枚举 ...