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 ...
随机推荐
- 深入理解Java反射+动态代理
答: 反射机制的定义: 是在运行状态中,对于任意的一个类,都能够知道这个类的所有属性和方法,对任意一个对象都能够通过反射机制调用一个类的任意方法,这种动态获取类信息及动态调用类对象方法的功能称为j ...
- python自动化之鼠标移动
################################用GUI自动化控制键盘和鼠标############################### ''' http://pyautogui.r ...
- ubuntu系统创建新用户并赋予sudo权限
1.创建新用户 创建新用户有两种方式:adduser和useradd adduser会为用户创建組./home目录下同名文件夹,密码,而useradd不会 因此推荐使用adduser创建用户,例: s ...
- Day23-Model操作,Form操作和序列化操作
参考源出处:http://blog.csdn.net/fgf00/article/details/54629502 1. 搭建环境请参考:http://www.cnblogs.com/momo8238 ...
- 【刷题】BZOJ 2125 最短路
Description 给一个N个点M条边的连通无向图,满足每条边最多属于一个环,有Q组询问,每次询问两点之间的最短路径. Input 输入的第一行包含三个整数,分别表示N和M和Q 下接M行,每行三个 ...
- 【BZOJ1055】[HAOI2008]玩具取名(动态规划)
[BZOJ1055][HAOI2008]玩具取名(动态规划) 题面 BZOJ 洛谷 题解 裸的区间\(dp\),设\(f[i][j][W/I/N/G]\)表示区间\([i,j]\)能否由某个字母替换过 ...
- (转)Maven学习总结(三)——使用Maven构建项目
孤傲苍狼 只为成功找方法,不为失败找借口! Maven学习总结(三)——使用Maven构建项目 maven作为一个高度自动化构建工具,本身提供了构建项目的功能,下面就来体验一下使用maven构建项目的 ...
- Python之旅:MySQL系列
第一篇:初识数据库 第二篇:库操作 第三篇:表操作 第四篇:数据操作 第五篇:索引原理与慢查询优化 第六篇:数据备份.pymysql模块 第七篇:视图.触发器.事务.存储过程.函数 第八篇:ORM框架 ...
- ASP.NET MVC验证框架中关于属性标记的通用扩展方法
http://www.cnblogs.com/wlb/archive/2009/12/01/1614209.html 之前写过一篇文章<ASP.NET MVC中的验证>,唯一的遗憾就是在使 ...
- Redis在window上安装
转:https://www.cnblogs.com/M-LittleBird/p/5902850.html 在windows上启动RabbitMQ: 这是Redis的启动,需要制定配置文件,否则连接不 ...