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 ...
随机推荐
- poj 2406 Power Strings(kmp应用)
题目链接:http://poj.org/problem?id=2406 题意:给出一个字符串s,求重复子串出现的最大次数. 分析:kmp的next[]数组的应用. 要求重复子串出现的最大次数,其实就是 ...
- sqlbulkcopy 批量更新 数据库
转载: http://blog.csdn.net/wangzh300/article/details/7382506 private static void DataTableToSQLServer( ...
- 【刷题】BZOJ 2734 [HNOI2012]集合选数
Description <集合论与图论>这门课程有一道作业题,要求同学们求出{1, 2, 3, 4, 5}的所有满足以 下条件的子集:若 x 在该子集中,则 2x 和 3x 不能在该子集中 ...
- tmux终端复用/tmate终端共享
目录 tmux tmate tmux 一种终端复用工具,是终端中的窗口管理器.常见的一种用途是在关闭父shell时保持程序运行(常用于ssh访问断开连接后保持运行).linux上可通过包管理器进行安装 ...
- HDU 6153 扩展kmp
A Secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total ...
- UML类图学习笔记
http://note.youdao.com/noteshare?id=d5af220db7081dda73511fcb7b4da390
- WebService和Http的POST和GET请求区别和示例
web service(SOAP) Webservice的一个最基本的目的就是提供在各个不同平台的不同应用系统的协同工作能力.Web service 就是一个应用程序,它向外界暴露出一个能够通过Web ...
- git高清技能图
----
- Java 在匿名内部函数中为外部函数变量赋值的解决方案
Java匿名内部函数本人不怎么主动使用,但是经常会调用一些API,其中会调用一些接口,而这些接口是需要使用匿名内部类来实现的,于是就遇到了一些问题. 就比如okHttp3 的接口调用 OkHttpCl ...
- bzoj千题计划233:bzoj 1304: [CQOI2009]叶子的染色
http://www.lydsy.com/JudgeOnline/problem.php?id=1304 结论1:根节点一定染色 如果根节点没有染色,选择其子节点的一个颜色,那么所有这个颜色的子节点都 ...