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. 夺命雷公狗—angularjs—15—内置封装好的计时器$interval和$timeout

    这里其实和js源生的效果是一样的,但是源生的在angularjs里面不能直接正常执行代码如下所示: <!DOCTYPE html> <html lang="en" ...

  2. css 文字换行控制

    强制不换行 white-space:nowrap 强制不换行,并显示省略号 word-wrap: normal; text-overflow: ellipsis; white-space: nowra ...

  3. Dreamweaver 时间轴如何打开

    因为下午要辅导几个调皮捣蛋的小孩HTML, 什么能让他们感觉又简单又好玩而起又能提高他们的兴趣呢?DW中设置浮动广告吧!       想好了,动手去做.DwearmWeaver里面居然没了时间轴, 度 ...

  4. zw版【转发·台湾nvp系列Delphi例程】HALCON DispCross

    zw版[转发·台湾nvp系列Delphi例程]HALCON DispCross procedure TForm1.Button1Click(Sender: TObject);var r, c : Ol ...

  5. ch2-4:遇到嵌套列表进行缩进打印

    1.增加一个参数来控制缩进打印:level '''这是一个模块,可以打印列表,其中可能包含嵌套列表''' def print_list(the_list,level): ""&qu ...

  6. Valid Anagram

    class Solution { public: bool isAnagram(string s, string t) { if(t=="") return s=="&q ...

  7. struts2,实现Ajax异步通信

    用例需要依赖的jar: struts2-core.jar struts2-convention-plugin.jar,非必须 org.codehaus.jackson.jar,提供json支持 用例代 ...

  8. php中method_exists()和is_callable()如何进行语句判断

    method_exists()和is_callable()方法进行判断.那么两则区别是什么呢? 已知类文件如下: class Student{private $alias=null;private $ ...

  9. [ Laravel 5.3 文档 ] 安全 ―― API认证(Passport)保障安全性。

    1.简介 Laravel通过传统的登录表单已经让用户认证变得很简单,但是API怎么办?API通常使用token进行认证并且在请求之间不维护session状态.Laravel使用LaravelPassp ...

  10. ecshop第一讲之安装

    今天开始对Ectouch做一些研究,但是发现网上资源很少,而且官网对服务都需收费:并且提供的wiki.官方文档等都是空的,瞬间感觉,,,,后来开始海量搜索,最后发现只有论坛还有些东西可以看看,并对海量 ...