CodeForces 219C Color Stripe
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 ≤ n ≤ 5·105; 2 ≤ k ≤ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer — the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Sample Input
6 3
ABBACC
2
ABCACA
3 2
BBB
1
BAB
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int n,m;
char s[];
while(scanf("%d%d",&n,&m)!=EOF)
{
scanf("%s",s);
int i,ans=;
m+=;
if(n==)
{
cout<<ans<<endl<<s<<endl;
continue;
}
if(m==)
{
int l=,r=;
for(i=;i<n;i++)
if(i%==)
{
if(s[i]=='B') l++;
else r++;
}
else
{
if(s[i]=='A') l++;
else r++;
}
if(l<r)
{
cout<<l<<endl;
for(i=;i<n;i++)
if(i%==) cout<<'A';
else cout<<'B';
}
else
{
cout<<r<<endl;
for(i=;i<n;i++)
if(i%==) cout<<'B';
else cout<<'A';
}
cout<<endl;
continue;
}
s[n]='#';
for(i=;i<n;i++)
if(s[i]==s[i-])
{
ans++;
for(int j=;j<=m;j++)
if(j==s[i]||j==s[i+]) continue;
else
{
s[i]=j;
break;
}
}
s[n]='\0';
cout<<ans<<endl<<s<<endl; }
return ;
}
CodeForces 219C Color Stripe的更多相关文章
- CF--思维练习--CodeForces - 219C Color Stripe (思维)
ACM思维题训练集合 A colored stripe is represented by a horizontal row of n square cells, each cell is paine ...
- Codeforces 219C - Color Stripe - [DP]
题目链接:http://codeforces.com/problemset/problem/219/C 题意: 给你 $n$ 个方块排成水平一排,每个方块都涂上 $k$ 种颜色中的一种.要求对尽量少的 ...
- Codeforces 219C Color Stripe(思维+字符串)
题目链接:http://codeforces.com/problemset/problem/219/C 题目大意: 给出一个字符串,只包含k种字符,问最少修改多少个字符(不增长新的种类)能够得到一个新 ...
- 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe
题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
- Codeforces 18C C. Stripe
Codeforces 18C C. Stripe 链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/E 题 ...
- 1045. Favorite Color Stripe (30) -LCS允许元素重复
题目如下: Eva is trying to make her own color stripe out of a given one. She would like to keep only her ...
- PAT 甲级 1045 Favorite Color Stripe
https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 Eva is trying to make ...
- 1045 Favorite Color Stripe 动态规划
1045 Favorite Color Stripe 1045. Favorite Color Stripe (30)Eva is trying to make her own color strip ...
随机推荐
- yii2的GridView和ActiveDataProvider具体使用
1.控制器中(以User模块的列表为例): 第一步: use backend\models\User;use yii\data\ActiveDataProvider; 第二步: public func ...
- MessageDigest
转: 我们知道,编程中数据的传输,保存,为了考虑安全性的问题,需要将数据进行加密.我们拿数据库做例子.如果一个用户注册系统的数据库,没有对用户的信息进 行保存,如,我去页面注册,输入"Vic ...
- Rsync详解
Rsync详解 1.什么是RsyncRsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本 ...
- U盘启动引导安装linux
一.U盘引导,安装前的准备 1.U盘一枚,至少2G 2.下载并安装虚拟光驱,这里我用的是UltralSO. 二.制作引导盘 1.打开UltraISO软件,选择文件->打开,打开需要烧录的镜像文件 ...
- android 项目学习随笔二十一(IM、语音识别、机器人、统计、扫描二维码、条形码)
语音识别:科大讯飞语音云 http://www.xfyun.cn/ 语音机器人模拟 public class TalkBean { public String text; public boolean ...
- 测试过程中LR的关联报错
在测试过程中,录制的脚本会做一些关联.在测试的过程中,常常出现关联失败的情况. 如果最后的结果有检查点,检查点失败而事务失败. 每次出现这样的情况,我都不知道如何办.为了不出现错误,我都在关联函数里面 ...
- 【转载】Perl异常处理方法总结
程序脚本在运行过程中,总会碰到这样那样的问题,我们会预知一些问题并为其准备好处理代码,而有一些不能预知.好的程序要能尽可能多的处理可能出现的异常问题,本文就总结了一些方法来解决这些异常,当然perl在 ...
- Rsync原理介绍及配置应用
1.前言 基于LAN或WAN的网络应用之间进行数据传输或者同步非常普遍,比如远程数据镜像.备份.复制.同步,数据下载.上传.共享等等.对此,最简单.直接的做法是对数据进行完全复制.然而,数据在网络上来 ...
- Hive文件格式
hive文件存储格式包括以下几类: 1.TEXTFILE 2.SEQUENCEFILE 3.RCFILE 4.ORCFILE(0.11以后出现) 其中TEXTFILE为默认格式,建表时不指定默认为这个 ...
- php的内存分配还是很智能的
<?php echo memory_get_usage().PHP_EOL;$a = 1;$b = $a;echo memory_get_usage().PHP_EOL; <?php ec ...