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. PAT乙级 1029. 旧键盘(20)

    1029. 旧键盘(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 旧键盘上坏了几个键,于是在敲一段文字的 ...

  2. 仅支持webkit浏览器的多行内容超出显示省略号

    .box { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; ...

  3. JSP-05- JSP总结

    1   客户端请求新页面 <%@ page language="java" contentType="text/html; charset=UTF-8" ...

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

    zw版[转发·台湾nvp系列Delphi例程]HALCON HWindowX 01 procedure TForm1.Button1Click(Sender: TObject);var img : H ...

  5. jquery 下拉菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. android 项目学习随笔七(ViewPagerIndicator与ViewPager)

    1.ViewPagerIndicator https://github.com/JakeWharton/ViewPagerIndicator package com.viewpagerindicato ...

  7. selenium webdriver三种等待方法

    webdriver三种等待方法 1.使用WebDriverWait from selenium import webdriverfrom selenium.webdriver.common.by im ...

  8. PHP memcached 扩展的安装

    PHP memcached 扩展的安装 1.下载相关的源码包(软件版本自己选择) memcached-1.4.15 , http://memcached.org/ //Memcached服务端. me ...

  9. DateTimeUtil 工具类,android 和 java 通用

    import java.sql.Date;import java.text.SimpleDateFormat; public class DateTimeUtil { public final cla ...

  10. Ubuntu 下配置ftp服务端

    安装sudo apt-get install pure-ftpdsudo netstat –ano | grep “21”查看21端口是否已开启.完成安装后,就可以用Ubuntu系统的用户名和密码登陆 ...