题目链接: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(思维+字符串)的更多相关文章

  1. CodeForces 219C Color Stripe

    Color Stripe Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submi ...

  2. CF--思维练习--CodeForces - 219C Color Stripe (思维)

    ACM思维题训练集合 A colored stripe is represented by a horizontal row of n square cells, each cell is paine ...

  3. Codeforces 219C - Color Stripe - [DP]

    题目链接:http://codeforces.com/problemset/problem/219/C 题意: 给你 $n$ 个方块排成水平一排,每个方块都涂上 $k$ 种颜色中的一种.要求对尽量少的 ...

  4. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

  5. 贪心 Codeforces Round #135 (Div. 2) C. Color Stripe

    题目传送门 /* 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 同时不和下一个相等就是最优的解法 */ #incl ...

  6. 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 ...

  7. Codeforces 18C C. Stripe

    Codeforces 18C  C. Stripe 链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/E 题 ...

  8. 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 ...

  9. PAT 甲级 1045 Favorite Color Stripe

    https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 Eva is trying to make ...

随机推荐

  1. 贝云cms内容管理系统(thinkphp5.0开源cms管理系统)

    byCms包含文章,图片,下载,视频模型,基于thinkphp5.0.9,可无缝升级至thinkphp.1.0,是一套简单,易用的内容管理系统,旨在帮助开发者节约web应用后台开发时间和精力,以最快的 ...

  2. iOS程序执行顺序 AppDelegate及 UIViewController 的生命周期

    iOS程序的启动执行顺序 AppDelegate 及 UIViewController 的生命周期 iOS应用程序的状态切换很重要,而UIViewControler对于iOS这种MVC模式来说尤为重要 ...

  3. NOIP2017逛公园(park)解题报告

    park作为今年noipday1最后一道题还是相比前面几道题还是有点难度的 首先你可以思考一下,第一天dp不见了,再看一下这题,有向图,看起来就比较像一个dp,考虑dp方程,首先肯定有一维是到哪个节点 ...

  4. bzoj1008/luogu3197 越狱 (快速幂)

    算$m^n-m*(m-1)^{n-1}$,就是总的减去不越狱的,不越狱就每次都选一个和上一个不一样的

  5. Hello,Power BI

    Power BI 是什么 Power BI 是一套业务分析工具,用于分析数据和理解数据,快速便捷地监控数据变化,为商务决策提供依据. Power BI 有用户组的概念.分享权限等概念 Power BI ...

  6. hdu3374解题报告

    hdu3374 Solution: 最小表示法+KMP 设一个字符串S的最小循环节是T.(如S=“abababab”,则T=“ab”) 在最小循环节T中,只有1个最小字符串和最大字符串.则最小字符串的 ...

  7. HDU 3966 树链剖分+树状数组 模板

    Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. HDU 6153 扩展kmp

    A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)Total ...

  9. cookie工具包

    package com.taotao.common.utils; import java.io.UnsupportedEncodingException; import java.net.URLDec ...

  10. Helm二:安装

    目录 Helm安装 Helm client安装 Helm tiller安装 Chart仓库配置 私有chart仓库 chart仓库的组成 创建本地仓库 chart仓库基本管理 Helm安装 Helm ...