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 ...
随机推荐
- 【BZOJ1413】[ZJOI2009]取石子游戏(博弈论,动态规划)
[BZOJ1413][ZJOI2009]取石子游戏(博弈论,动态规划) 题面 BZOJ 洛谷 题解 神仙题.jpg.\(ZJOI\)是真的神仙. 发现\(SG\)函数等东西完全找不到规律,无奈只能翻题 ...
- 【转】如何快速识别应用MOS管,几张图片就搞定了
三极管是流控型器件,MOS管是压控型器件,两者存在相似之处.三极管机可能经常用,但MOS管你用的可能较少.对于MOS管先抛出几个问题: 如何区分P-MOS和N-MOS: 如何区分MOS的G.D.S管脚 ...
- 洛谷 P4112 [HEOI2015]最短不公共子串 解题报告
P4112 [HEOI2015]最短不公共子串 题目描述 在虐各种最长公共子串.子序列的题虐的不耐烦了之后,你决定反其道而行之. 一个串的"子串"指的是它的连续的一段,例如bcd是 ...
- 【HDU5687】Trie
题目大意:需要维护一个支持以下操作的数据结构:(1)支持插入一个字符串(2)支持删除所有前缀等于给定字符串的单词(3)查询该数据结构中是否存在一个以给定字符串为前缀的字符串 题解:由题目可知,需要维护 ...
- C++11 & C++14 & C++17新特性
C++11:C++11包括大量的新特性:包括lambda表达式,类型推导关键字auto.decltype,和模板的大量改进. 新的关键字 auto C++11中引入auto第一种作用是为了自动类型推导 ...
- Mac OS利用ssh访问ubuntu虚拟机及云端操作
1.桥接模式 将该虚拟机的网口设置成桥接模式(Bridged Adapter),以确保主机可以ping通虚拟机: 2.安装ssh 在ubuntu虚拟机上安装ssh server: sudo apt-g ...
- android studio run的时候一直卡在waiting for debug
原因如下: 选择ok就可以,同时我们也可以从这里找到 平常遇到跟真机有关的问题,三步大法,1,插拔手机,2.adb kill-server;adb start-server 3.重启as
- AngulaJs -- 隔离作用域
具有隔离作用域的指令最主要的使用场景是创建可复用的组件 创建具有隔离作用域的指令需要将scope属性设置为一个空对象{}.如果这样做了,指令的 模板就无法访问外部作用域了: <div ng-co ...
- AngularJS入门基础——$provide.decorator 实例讲解
<body ng-controller="OneController"> <script> var Foobar = function() { ...
- expect 交互 模拟ssh 登陆
模拟ssh登录 #!/bin/bash Ip='192.168.1.6' # 循环就行 RemoteUser='user' # 普通用户 RemotePasswd='userpasswd' # 普通用 ...