贪心 Codeforces Round #135 (Div. 2) C. Color Stripe
/*
贪心:当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的更多相关文章
- 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...
- 构造 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 ...
- 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know
题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...
- 贪心 Codeforces Round #301 (Div. 2) B. School Marks
题目传送门 /* 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 num1是输出的1的个数,numy是除此之外的数都为y,此时的n ...
- 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks
题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...
- 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges
题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...
- 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
- 找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones
题目传送门 /* 找规律/贪心:ans = n - 01匹配的总数,水 */ #include <cstdio> #include <iostream> #include &l ...
- 字符串处理/贪心 Codeforces Round #307 (Div. 2) B. ZgukistringZ
题目传送门 /* 题意:任意排列第一个字符串,使得有最多的不覆盖a/b字符串出现 字符串处理/贪心:暴力找到最大能不覆盖的a字符串,然后在b字符串中动态得出最优解 恶心死我了,我最初想输出最多的a,再 ...
随机推荐
- Github上600多个iOS开源项目分类及介绍
将Github上600多个iOS开源项目进行分类并且有相应介绍,小伙伴们快来看呀 地址:http://github.ibireme.com/github/list/ios/
- Delphi ADO的Lookup类型字段的问题
关于ADO数据集控件中的Lookup类型字段,在其Lookupkeyfields属性指向的字段中存在NULL值的,就会出现'EOleException with message '发生未知错误',这个 ...
- KMP算法 C#实现 字符串查找简单实现
KMP算法 的C#实现,初级版本 static void Main(string[] args) { #region 随机字符 StringBuilder sb = new StringBuilder ...
- Spring在Java Filter注入Bean为Null的问题解决
在Spring的自动注入中普通的POJO类都可以使用@Autowired进行自动注入,但是除了两类:Filter和Servlet无法使用自动注入属性.(因为这两个归Web容器管理)可以用init(集承 ...
- Ubuntu 16.04安装RabbitVCS替代TortoiseSVN/TortoiseGit
RabbitVCS官网:http://www.rabbitvcs.org/easonjim 1.添加PPA源 sudo add-apt-repository ppa:rabbitvcs/ppa 如果导 ...
- Linux NFS服务器的安装与配置(转载)
一.NFS服务简介 NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布.功能是通过网络让不同的机器.不同的操 ...
- webpack—入门
点击进入webpack官网.,开始教程时,建议先学习ES6语法,也请先观看本篇Windows符号介绍文章,当所有webpack内容学习完后,会有一个专门的介绍 webpack四个核心概念(从官网入门— ...
- Linux改动/etc/profile配置错误command is not found自救方法
我的CSDN博客地址: http://blog.csdn.net/caicongyang 博主之前在改动了/etc/profile配置文件方法后,导致bash命令无法用 运行ls命令结果例如以下: - ...
- openstack-glance-api.service start request repeated too quickly, refusing to start
问题描写叙述 openstack J版 centos7部署 重新启动服务时起不来,日志也不报错.以glance服务为例,例如以下: # systemctl start openstack-glance ...
- docker init 起步
#yum install wget http://fedora.mirror.nexicom.net/epel/6/x86_64/epel-release-6-8.noarch.rpm yum -y ...