E. Color Stripe
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
input

Copy
6 3
ABBACC
output

Copy
2
ABCACA
input

Copy
3 2
BBB
output

Copy
1
BAB 题意:给你一行n个元素,k种颜色,要求相邻的元素颜色不能相同,输出最少改变颜色的数量和n个元素最后的颜色(若有多种可能可任意输出一种)
注意:k==2时必定是奇偶位不相同,要选一个最少改变方案,所以要知道奇位放A要改变的数多还是偶位放A要改变的数多,在训练时卡在这种情况了,
其实一开始想到了ABBA这种情况,但当时的思路是只改变相同颜色的元素,没想到不同颜色元素也能改变,所以当时就假设如果有偶数个相同颜色的元素,其两边元素颜色必不同,结果就一直wrong answer on the test 15...
 #include<bits/stdc++.h>
using namespace std;
const int amn=5e5+;
char mp[amn];
int a[amn],c[];
int main(){
int n,k;
ios::sync_with_stdio();
cin>>n>>k;
for(int i=;i<=n;i++){
cin>>mp[i];
a[i]=mp[i]-'A'+;
}
int ans=,c1=,c2=;
if(k==){ ///k==2时必定是奇偶位不相同,要选一个最少改变方案,所以要知道奇位放A要改变的数多还是偶位放A要改变的数多
for(int i=;i<=n;i++){ /// 我们先假设奇位为A,偶位为B,因为有可能合法情况是偶位为A,奇数位为B,所以最后要比较哪个不合法的数量少,这样更改少的那个才能得到最少改变方案,故下面统计不合法数,
if(i%==){ ///若偶位为A,则A的不合法数加1,否则B的不合法数加1
if(mp[i]=='A')c1++;
else c2++;
}
else{ ///若偶位为A,则B的不合法数加1,否则A的不合法数加1
if(mp[i]=='A')c2++;
else c1++;
}
}
ans=min(c1,c2); ///选一个最小不合法数,得到最少改变方案
for(int i=;i<=n;i++){
if(ans==c1){ ///若偶数位为A是不合法的,要将奇位变为A,偶位变为B
if(i%)
mp[i]='A';
else
mp[i]='B';
}
else{
if(i%) ///若奇数为位A是不合法的,要将偶数位变为A,奇数位变为B
mp[i]='B';
else
mp[i]='A';
}
}
}
else{
a[n+]=;
memset(c,,sizeof c);
bool f=,d=;
int fr=,ta=,frc,mic,tac,it,cc;
for(int i=;i<=n;i++){
if(a[i]==a[i-]){
d=;
if(fr>ta){
fr=i-;
if(i->=){
frc=a[fr-];//cout<<frc<<'!'<<endl;
c[a[i]]=c[frc]=;
}
else{
c[a[i]]=;
f=;
}
mic=a[i];
ta=i+;
tac=a[ta];
c[tac]=;
}
else{ ta=i+;
tac=a[ta];
c[tac]=;
}
//printf("i:%d fr:%d ta:%d\n",i,fr,ta);
if(!f&&i==n){
ans+=(ta-fr)/;
it=fr+;
cc=frc;
while(it<ta){
a[it]=cc;
mp[it]=a[it]-+'A';
it+=;
}
break;
} }
else if(d){
//printf("i:%d fr:%d ta:%d tac:%d\n",i,fr,ta,tac);
d=;
ans+=(ta-fr)/;
if(f){
cc=tac;
if(ta>n)
for(int i=;i<=k;i++){
if(!c[i]){
cc=i;
break;
}
}
it=ta-;
while(it>){
a[it]=cc;
mp[it]=a[it]-+'A';
it-=;
}
f=;
}
else{
cc=frc;
//printf("---\ni:%d frc: %d tac: %d\n",i,frc,tac);
//for(int i=1;i<=26;i++)cout<<c[i]<<' ';
//cout<<"\n---\n";
if(frc==tac){
for(int i=;i<=k;i++){
if(!c[i]){
cc=i;
break;
}
}
}
//cout<<i<<'?'<<cc<<endl;
memset(c,,sizeof c);
it=fr+;
while(it<ta){
a[it]=cc;
mp[it]=a[it]-+'A';
it+=;
}
}
fr=ta;
ta--;
}
}
if(f){
ans+=(ta-fr)/;
for(int i=;i<=k;i++){
if(!c[i]){
cc=i;
break;
}
}
memset(c,,sizeof c);
it=fr+;
while(it<ta){
a[it]=cc;
mp[it]=a[it]-+'A';
it+=;
}
fr=ta;
ta--;
}
}
cout<<ans<<endl;
cout<<mp+<<endl;
}
 

Codeforce219C-Color Stripe的更多相关文章

  1. CodeForces 219C Color Stripe

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

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

  3. PAT 甲级 1045 Favorite Color Stripe

    https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 Eva is trying to make ...

  4. 1045 Favorite Color Stripe 动态规划

    1045 Favorite Color Stripe 1045. Favorite Color Stripe (30)Eva is trying to make her own color strip ...

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

  6. PAT甲级1045. Favorite Color Stripe

    PAT甲级1045. Favorite Color Stripe 题意: 伊娃正在试图让自己的颜色条纹从一个给定的.她希望通过剪掉那些不必要的部分,将其余的部分缝合在一起,形成她最喜欢的颜色条纹,以保 ...

  7. pat1045. Favorite Color Stripe (30)

    1045. Favorite Color Stripe (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

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

  9. 1045 Favorite Color Stripe (30)(30 分)

    Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...

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

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

随机推荐

  1. Flutter Widgets 之 ListWheelScrollView

    注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 基础用法 在展示大量数据的时候我们第一会想到使用ListV ...

  2. TCP可靠传输的工作原理

    TCP可靠传输的工作原理 一.停止等待协议 1.1.简介 在发送完一个分组后,必须暂时保留已发送的分组的副本. 分组和确认分组都必须进行编号. 超时计时器的重传时间应当比数据在分组传输的平均往返时间更 ...

  3. SpringCloud入门(六): Hystrix监控

    Hystrix.stream 监控 <!--. 配置pom文件,引入actuator包--> <dependency> <groupId>org.springfra ...

  4. KEMET新型电容器推动了电动汽车技术的发展

    前言:KEMET成立于1919年,总部位于佛罗里达州劳德代尔堡,是全球领先的高端电子组件供应商,KEMET为客户提供业内最广泛的电容器技术选择,以及不断扩大的机电设备,电磁兼容性解决方案和超级电容器. ...

  5. java基础进阶篇(四)_HashMap------【java源码栈】

    目录 一.前言 二.特点和常见问题 二.接口定义 三.初始化构造函数 四.HashMap内部结构 五.HashMap的存储分析 六.HashMap的读取分析 七.常用方法 八.HashMap 的jav ...

  6. ASP.NET CORE 管道模型及中间件使用解读

    说到ASP.NET CORE 管道模型不得不先来看看之前的ASP.NET 的管道模型,两者差异很大,.NET CORE 3.1 后完全重新设计了框架的底层,.net core 3.1 的管道模型更加灵 ...

  7. UVA - 10462 Is There A Second Way Left?

    题意: 给你一张无向图,让你判断三种情况:1.不是连通图(无法形成生成树)2.只能生成唯一的生成树 3.能生成的生成树不唯一(有次小生成树),这种情况要求出次小生成树的边权值和. 思路: 比较常见的次 ...

  8. vue-cli “从入门到放弃”

    主要作用:目录结构.本地调试.代码部署.热加载.单元测试 在如今前端技术飞速发展的时代,angular.js.vue.js 和 react.js 作为前端框架已经呈现出了三国鼎立的局面.作为国人若你不 ...

  9. Linux学习5-安装mysql

    前言 今天我们来学习一下如何在Linux下安装mysql 一:准备安装包 可以从http://www.mysql.com/downloads/官方网站下载到最新版本,本次安装的版本是mysql-5.7 ...

  10. 多道技术 进程 线程 协程 GIL锁 同步异步 高并发的解决方案 生产者消费者模型

    本文基本内容 多道技术 进程 线程 协程 并发 多线程 多进程 线程池 进程池 GIL锁 互斥锁 网络IO 同步 异步等 实现高并发的几种方式 协程:单线程实现并发 一 多道技术 产生背景 所有程序串 ...