CodeForces 219C 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
6 3
ABBACC
2
ABCACA
3 2
BBB
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的更多相关文章
- CF--思维练习--CodeForces - 219C Color Stripe (思维)
ACM思维题训练集合 A colored stripe is represented by a horizontal row of n square cells, each cell is paine ...
- Codeforces 219C - Color Stripe - [DP]
题目链接:http://codeforces.com/problemset/problem/219/C 题意: 给你 $n$ 个方块排成水平一排,每个方块都涂上 $k$ 种颜色中的一种.要求对尽量少的 ...
- Codeforces 219C Color Stripe(思维+字符串)
题目链接:http://codeforces.com/problemset/problem/219/C 题目大意: 给出一个字符串,只包含k种字符,问最少修改多少个字符(不增长新的种类)能够得到一个新 ...
- 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe
题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
- Codeforces 18C C. Stripe
Codeforces 18C C. Stripe 链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/E 题 ...
- 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 ...
- PAT 甲级 1045 Favorite Color Stripe
https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 Eva is trying to make ...
- 1045 Favorite Color Stripe 动态规划
1045 Favorite Color Stripe 1045. Favorite Color Stripe (30)Eva is trying to make her own color strip ...
随机推荐
- 【ubuntu】install openbox+tint2+bmenu on ubuntu12.04.4
原文地址: http://ndever.net/articles/linux/install-openbox-ubuntu-1304-1310 openbox是我用过的轻量窗口中最好用的了. Step ...
- 【pyQuery分析论坛】精英乒乓论坛
In [25]: t= h('table') In [26]: In [26]: t('.mainbox').text() Out[26]: u'\u72b6\u6001 \u4e3b\u9898 \ ...
- yii2的GridView和ActiveDataProvider具体使用
1.控制器中(以User模块的列表为例): 第一步: use backend\models\User;use yii\data\ActiveDataProvider; 第二步: public func ...
- UIPage
分页控件是一种用来取代导航栏的可见指示器,方便手势直接翻页,最典型的应用便是iPhone的主屏幕,当图标过多会自动增加页面,在屏幕底部你会看到原点,用来只是当前页面,并且会随着翻页自动更新. 一.创建 ...
- iOS发布条款检查表
序号 分类 条款编号 条款 案例 1 功能 2.1 崩溃的程序将会被拒绝 2 2.2 有错误的程序将会被拒绝 点击版本升级无反应/点击版本升级,在线版本和当前版本都是2.0.3 3 2.3 跟开发者宣 ...
- Mysql密码恢复
由于种种原因,Mysql root用户的密码可能被恶意篡改,这个时候就需要对Mysql进行密码恢复了.大致步骤如下: 1.修改MySQL的登录设置: # vi /etc/my.cnf 在[mysqld ...
- The C++ Standard Library --- A Tutorial Reference 读书笔记
5.2 Smart Pointer(智能指针) shared_ptr的aliasing构造函数,接受一个shared pointer和一个raw pointer.它允许你掌握一个事实:某对象拥有另一个 ...
- mysql btree与hash索引的适用场景和限制
btree索引: 如果没有特别指明类型,多半说的就是btree索引,它使用btree数据结构来存储数据,大多数mysql引擎都支持这种索引,archive引擎是一个例外,5.1之前这个引擎不支持任何索 ...
- Squares 分类: POJ 2015-08-04 11:46 3人阅读 评论(0) 收藏
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 17462 Accepted: 6634 Description ...
- U3D UGUI学习2 - Canvas
Canvas Render Mode - UGUI不像NGUI,它没有UI摄像机.但有时候需要做相机动画就要调出来. 在Canvas里设置第二个选项即可调出. Pixel Perferct - 这个似 ...