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. CentOS卸载旧版本内核

    CentOS卸载旧版本内核 查看正在使用的内核 uname -a 查看系统中的全部内核 rpm -qa | grep kernel 卸载多余内核 yum remove kernel-x.xx.x

  2. openCryptoki安装

    什么是OpenCryptoki OpenCryptoki提供Linux下的PKCS#11库和工具,支持包括TPM和IBM加密硬件以及软件令牌. 目前(2019/05/06)最新release版为3.1 ...

  3. Windows 下 LaTeX 手动安装宏包(package)以及生成帮助文档的整套流程

    本文简单介绍如何手动安装一个 LaTeX 宏包. 一般来说,下载的 TeX 发行版已经自带了很多宏包,可以满足绝大部分需求,但是偶尔我 们也可能碰到需要使用的宏包碰巧没有安装的情况,这时我们就需要自己 ...

  4. 亚马逊,谷歌,Facebook,IBM和微软:为了AI,是的,我们在一起了

    美国时间9月28日,也就是几个小时前,亚马逊,谷歌,Facebook,IBM和微软宣布成立了一家非盈利组织:人工智能合作组织(Partnership on AI),目标是为人工智能的研究制定和提供范例 ...

  5. 量化投资学习笔记30——《Python机器学习应用》课程笔记04

    有监督学习 常用分类算法 KNN:K近邻分类器.通过计算待分类数据点,与已知数据中所有点的距离,取距离最小的前K个点,根据"少数服从多数"的原则,将这个数据点划分为出现次数最多的那 ...

  6. C#中使用 正则表达式 替换img中src路径但保留图片名

    text = Regex.Replace(text, @"(?i)(?<=<img\b[^>]*?src=\s*(['""]?))([^'"& ...

  7. lnmp 一键安装包(nginx) 运行laravel项目显示该网页无法正常运行

    vi /usr/local/nginx/conf/fastcgi.conf 注释掉 PHP_ADMIN_VALUE #fastcgi_param PHP_ADMIN_VALUE "open_ ...

  8. 基于netty的群聊

    基于netty的群聊 学了一段时间的netty知识,现在通过这个基于console的程序来对netty的相关接口做个简单的应用. 准备 依赖 <dependency> <groupI ...

  9. 浏览器内核之 HTML 解释器和 DOM 模型

    微信公众号:爱写bugger的阿拉斯加如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 此文章是我最近在看的[WebKit 技术内幕]一书的一些理解和做的笔记.而[WebKit 技术内幕]是基 ...

  10. ABP开发框架前后端开发系列---(16)ABP框架升级最新版本的经验总结

    有一小段时间没有持续升级ABP框架了,最近就因应客户的需要,把ABP框架进行全面的更新,由于我们应用的ABP框架,基础部分还是会使用官方的内容,因此升级的时候需要把官方基础ABP的DLL进行全面的更新 ...