题目传送门

 /*
贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量;当m > 2时,当与前一个相等时, 改变一个字母
同时不和下一个相等就是最优的解法
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXN = 5e5 + ;
const int INF = 0x3f3f3f3f;
char s[MAXN]; int main(void) { //Codeforces Round #135 (Div. 2) C. Color Stripe
//freopen ("C.in", "r", stdin); int n, m;
while (scanf ("%d%d", &n, &m) == ) {
scanf ("%s", s + ); int len = strlen (s + ); if (m == ) {
int c1 = , c2 = ;
for (int i=; i<=len; ++i) {
if (i & ) {
if (s[i] == 'A') c2++;
else c1++;
}
else {
if (s[i] == 'A') c1++;
else c2++;
}
} printf ("%d\n", min (c1, c2));
for (int i=; i<=len; ++i) {
if (i & ) printf ("%c", (c1 < c2) ? 'A' : 'B');
else printf ("%c", (c1 < c2) ? 'B' : 'A');
}
puts ("");
}
else {
int ans = ; s[len+] = s[] = '@';
for (int i=; i<=len; ++i) {
if (s[i] == s[i-]) {
ans++;
for (int j=; j<=m; ++j) {
char ch = 'A' + j - ;
if (s[i] == ch || s[i+] == ch) continue;
else {
s[i] = ch; break;
}
}
}
}
printf ("%d\n", ans); s[len+] = '\0';
printf ("%s\n", s + );
}
} return ;
}

贪心 Codeforces Round #135 (Div. 2) C. Color Stripe的更多相关文章

  1. 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

    题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...

  2. 构造 Codeforces Round #135 (Div. 2) B. Special Offer! Super Price 999 Bourles!

    题目传送门 /* 构造:从大到小构造,每一次都把最后不是9的变为9,p - p MOD 10^k - 1,直到小于最小值. 另外,最多len-1次循环 */ #include <cstdio&g ...

  3. 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know

    题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...

  4. 贪心 Codeforces Round #301 (Div. 2) B. School Marks

    题目传送门 /* 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 num1是输出的1的个数,numy是除此之外的数都为y,此时的n ...

  5. 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

    题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...

  6. 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges

    题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...

  7. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  8. 找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones

    题目传送门 /* 找规律/贪心:ans = n - 01匹配的总数,水 */ #include <cstdio> #include <iostream> #include &l ...

  9. 字符串处理/贪心 Codeforces Round #307 (Div. 2) B. ZgukistringZ

    题目传送门 /* 题意:任意排列第一个字符串,使得有最多的不覆盖a/b字符串出现 字符串处理/贪心:暴力找到最大能不覆盖的a字符串,然后在b字符串中动态得出最优解 恶心死我了,我最初想输出最多的a,再 ...

随机推荐

  1. 1sting 大数 递推

    You will be given a string which only contains ‘1’; You can merge two adjacent ‘1’ to be ‘2’, or lea ...

  2. 选择器(E:hover/E:active/E:focus的使用)

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...

  3. Spring Boot多数据源连接8小时后断开的问题解决(MySQL)

    这个问题涉及的方面很多,需要一步步去排查,可能环境有问题,数据库有问题,但是网上最多的应该是如下的方式去解决. 以单个数据源为主,多个数据源基本方法一致. 1.MySQL 5版本之前可以通过在URL后 ...

  4. delphi的一些语法知识 以及参数传递问题,按引用方式传递参数,按值方式传递参数

    //delphi中exit,abort,break,continue 的区别 exit: 退出函数体abort: 遇到异常,安静处理,就是不显示不提示break: 退出当前循环体,包括for ,whi ...

  5. 【CV论文阅读】Image Captioning 总结

    初次接触Captioning的问题,第一印象就是Andrej Karpathy好聪明.主要从他的两篇文章开始入门,<Deep Fragment Embeddings for Bidirectio ...

  6. 关于ping以及TTL的分析

    首先介绍一下ping这个工具 ping [目标] 的意思就是向目标发送几个数据包,之后假设目标接受到一个数据包.那么目标就会向发送ping的主机返回一个数据包 比方上图.我ping了百度的server ...

  7. 推荐IOS开发3个工具:Homebrew、TestFight、Crashlytics-b

    1. Homebrew 什么是Homebrew? Homebrew is the easiest and most flexible way to install the UNIX tools App ...

  8. 使用adb在电脑和手机间传文件

    首先须要root手机. 然后,"Win + R",打开cmd窗体.以下以copy d:\1.txt到/system/文件夹为例说明. adb push source(localpa ...

  9. web 开发之js---js 实现文本高亮

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head ...

  10. Servlet+JSP 原理

    Servlet是用Java编写的Server端程序,与协议和平台无关,可移植行较强. Servlet在编辑时须要导入特定的Servlet API 的包,类似于普通Java程序的写法. Servlet採 ...