Codeforces 219C Color Stripe(思维+字符串)
题目链接:http://codeforces.com/problemset/problem/219/C
题目大意:
给出一个字符串,只包含k种字符,问最少修改多少个字符(不增长新的种类)能够得到一个新的字符串,这个字符串满足相邻的字符没有相同的。
解题思路:
k=2时:
字符串只能是ABABAB.....或者BABABA.....两种都枚举一下即可。
k>2时:
若有奇数个相同,如AAAAA则可变为ABABA的形式即可。
若有偶数个相同,如AAAAAA则变为ABABAC的形式即可,C保证与后一个不同。
代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
const int N=5e5+; int dp[N][]; int main(){
FAST_IO;
int n,k;
string str,s1,s2;
cin>>n>>k;
cin>>str;
s1=str,s2=str;
int ans=,sum=;
if(k==){
for(int i=;i<n;i++){
if(i%&&s1[i]!='A')
sum++,s1[i]='A';
else if(i%==&&s1[i]!='B')
sum++,s1[i]='B';
}
ans=sum;
sum=;
for(int i=;i<n;i++){
if(i%&&s2[i]!='B')
sum++,s2[i]='B';
else if(i%==&&s2[i]!='A')
sum++,s2[i]='A';
}
ans=min(ans,sum);
cout<<ans<<endl;
if(ans==sum)
cout<<s2<<endl;
else
cout<<s1<<endl;
}
else{
for(int i=;i<n;i++){
if(str[i]==str[i-]){
ans++;
for(int j=;j<k;j++){
if(i!=n-){
if('A'+j!=str[i-]&&'A'+j!=str[i+]){
str[i]='A'+j;
break;
}
}
else{
if('A'+j!=str[i-]){
str[i]='A'+j;
break;
}
}
}
}
}
cout<<ans<<endl;
cout<<str<<endl;
}
return ;
}
Codeforces 219C Color Stripe(思维+字符串)的更多相关文章
- CodeForces 219C Color Stripe
Color Stripe Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submi ...
- 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 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
- 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe
题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...
- PAT 甲级 1045 Favorite Color Stripe (30 分)(思维dp,最长有序子序列)
1045 Favorite Color Stripe (30 分) Eva is trying to make her own color stripe out of a given one. S ...
- 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 ...
随机推荐
- MyBatis分步查询的延迟加载
延迟加载的概念只存在于分步查询时: 延迟加载的本质是为第一步查询返回的Java Bean创建了一个代理对象: 延迟加载的全局设置有两个: lazyLoadingEnabled,作用为设置select语 ...
- spring中的传播性 个人认为就是对方法的设置 其作用能传播到里面包含的方法上
spring中的传播性 个人认为就是对方法的设置 其作用能传播到里面包含的方法上
- Magical Girl Haze 南京网络赛2018
题意: 就是使不大于k条路的权值变为零后求最短路 解析: d[i][j]表示使j条路变为权值后从起点到点i的最短路径 这题不能用spfa做 tle #include <iostream> ...
- AtCoder Grand Contest 010
AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...
- error while loading shared libraries: libmysqlcppconn.so.7: cannot open shared object file: No such file or directory
1. 即使libmysqlcppconn.so.7和与之相关存在,也报这个错误. 解决方法:临时添加LD_LIBRARY_PATH, 假使 libmysqlcppconn.so在/usr/local/ ...
- python xml.etree.ElementTree模块
使用的XML文件如下:file.xml <?xml version="1.0"?> <data name="ming"> <cou ...
- 「Django」浏览+1的操作
适应于网页.文章等浏览次数统计 1.Models设置:添加viewed方法 class NewsTitle(models.Model): title = models.CharField(max_le ...
- 烦人的IE7、8,半透明滤镜(filter:alpha)失效、png半透明失效的解决办法
在项目中的问题,之前用的是用IETest测试IE7,8发现背景透明设置无效,后来找文章解决!看了一些资料,做下总结. 几种IE半透明CSS样式 IE8里可以这样写 -ms-filter:”progid ...
- 网络编程之tcp窗口滑动以及拥塞控制
TCP协议作为一个可靠的面向流的传输协议,其可靠性和流量控制由滑动窗口协议保证,而拥塞控制则由控制窗口结合一系列的控制算法实现.一.滑动窗口协议 关于这部分自己不晓得怎么叙述才好,因为理解的部 ...
- 转自知乎大神----JS 闭包是什么
大名鼎鼎的闭包!这一题终于来了,面试必问. 请用自己的话简述 什么是「闭包」. 「闭包」的作用是什么. --------------------------------------- 首先来简述什么是 ...