CF--思维练习--CodeForces - 219C Color Stripe (思维)
ACM思维题训练集合
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.
Examples
Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB
题目很简单,类比平面图的四色定理,线性上有3色定理,即如果k>=3那么无论遇到什么情况AABB的时候,我们都可以将相同的变成第三种颜色,所以这里要特判K=2的时候,我一开始想了很久,就是不知道怎么处理,但是k=2的串只有ABABA……和BABABA……这两种情况,判断当前穿变成这两种那种花费少,就是答案了。
#include <bits/stdc++.h>
using namespace std;
int n, m, cnt = 0;
char check(char a, char b)
{
for (char i = 'A'; i <= 'A' + m - 1; i++)
{
if (i == a || i == b)
continue;
else
return i;
}
}
int main()
{
cin >> n >> m;
string s;
cin >> s;
if (m == 2)
{
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; i++)
{
if (s[i] != ('A' + (i % 2)))
cnt1++;
else
cnt2++;
}
if (cnt2 > cnt1)
{
cout << cnt1 << endl;
for (int i = 0; i < n; i++)
if (i % 2 == 0)
putchar('A');
else
putchar('B');
}
else
{
cout << cnt2 << endl;
for (int i = 1; i <= n; i++)
if (i % 2 == 0)
putchar('A');
else
putchar('B');
}
puts("");
return 0;
}
for (int i = 1; i < s.length();)
{
if (s[i] == s[i - 1])
{
s[i] = check(s[i - 1], s[i + 1]);
i = i + 2;
cnt++;
}
else
i++;
}
cout << cnt << endl
<< s << endl;
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- FPGA程序加载方式
- 国密SSL协议之性能测试
- 发现一个好看的手机壁纸网站,撸代码的手已经饥渴难耐了
- 还在为怎么学习Android苦恼?看完学会这些大牛资料,2年高级3年资深不是问题!
- 面试心得:一个BAT大厂面试者整理的Android面试题目,五轮面试后终于拿到Offer!
- 【Lighthouse教程】scrapy爬虫初探
- python 手把手教你基于搜索引擎实现文章查重
- Intellij IDEA 如何自动生成 serialVersionUID
- Spring事务是如何应用到你的业务场景中的?
- BFE.dev前端刷题#9. 解密消息 (Facebook面试题)
- JUnit 5 测试问题 must be static unless the test class is
- Intellij IDEA 如何自动生成 serialVersionUID
- 图解面试题:SQL存储过程有什么用?
- LeetCode-6.Z 字形变换 - 消费补偿算法
- python + redis 实现 分布式队列任务