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

Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
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的更多相关文章

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

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

  2. Codeforces 219C - Color Stripe - [DP]

    题目链接:http://codeforces.com/problemset/problem/219/C 题意: 给你 $n$ 个方块排成水平一排,每个方块都涂上 $k$ 种颜色中的一种.要求对尽量少的 ...

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

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

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

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

  5. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

  6. Codeforces 18C C. Stripe

    Codeforces 18C  C. Stripe 链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/E 题 ...

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

  8. PAT 甲级 1045 Favorite Color Stripe

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

  9. 1045 Favorite Color Stripe 动态规划

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

随机推荐

  1. 记linux下使用create_ap 创建热点失败及解决(涉及rfkill)

    先介绍一下 create_ap. 这是一个在linux中创建热点用的脚本, 托管在github中, https://github.com/oblique/create_ap/ 正文开始: 习惯了win ...

  2. 创建本地yum软件源,为本地Package安装Cloudera Manager、Cloudera Hadoop及Impala做准备

    一.包管理工具及CentOS的yum 1.包管理工具如何发现可以用的包 包管理工具依赖一系列软件源,工具下载源的信息存储在配置文件中,其位置随某包管理工具不同而变化 使用yum的RedHat/Cent ...

  3. 我的AutoCAD二次开发之路 (一)

    原帖地址 http://379910987.blog.163.com/blog/static/33523797201011184552167/ 今天在改代码的时候,遇到了AddVertexAt方法的用 ...

  4. umask setuid setgid

    cat /etc/bashrc if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ];#用户UI ...

  5. 【python cookbook】【字符串与文本】1.针对任意多的分隔符拆分字符串

    问题:将分隔符(以及分隔符之间的空格)不一致的字符串拆分为不同的字段: 解决方案:使用更为灵活的re.split()方法,该方法可以为分隔符指定多个模式. 说明:字符串对象的split()只能处理简单 ...

  6. 【python cookbook】【数据结构与算法】8.与字典有关的计算问题

    问题:在字典上对数据执行各式各样的计算(比如求最小值.最大值.排序). 解决方案:利用zip()将字典的键-值对“反转”为值-键对序列. 例如:如下字典存放的股票名称和对应的价格: >>& ...

  7. laravel5.0升级到laravel5.1

    1.修改composer.json.将其中的"laravel/framework": "5.0.*"修改为"laravel/framework&quo ...

  8. JDK结构介绍

    dt.jar和tools.jar位于:{Java_Home}/lib/下, 而rt.jar位于:{Java_Home}/jre/lib/下, 其中: (1) rt.jar是JAVA基础类库,也就是你在 ...

  9. 中颖4位MCU的减法汇编指令

    1, SUB M 执行动作: M - A -> A, 如果M-A的过程中没有产生借位,则CY= 1,如果产生了借位,则CY= 0. 其中,A为累加器. 2, SBI M, I 执行动作:M - ...

  10. Overview of Flashback Technology

    Oracle Flashback Query : SELECT AS OFOracle Flashback Version Query :DBMS_FLASHBACK PackageOracle Fl ...