Color Stripe--codeForces 219C
来源:互联网 发布:开淘宝店应该注意什么 编辑:程序博客网 时间:2024/05/21 21:38
Description
A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.
Input
The first input line contains two integers n and k (1 ≤ n ≤ 5·105; 2 ≤ k ≤ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.
Output
Print a single integer — the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
Sample Input
6 3ABBACC
2ABCACA
3 2BBB
1BAB
//奇怪。怎么还是WA。我的思路是把这根棍子看成很多段。从第二段开始遍历,如果和前一段相同,那么此段和前一段必须有一段要变色。先看看此段能否变色(变色完确保3个连在一起的段没同色),如果不能变色的话就只能变此段的前一段了。。。WAWAWAWA。。。
#include <iostream>#include <cstdio>#include <string>#include <cstring>using namespace std;#define maxn 500080bool vis[maxn];char A[maxn];int main(){int n,k;while(scanf("%d%d",&n,&k)==2){memset(vis,0,sizeof(vis));cin>>A;int sum=0;for(int i=1;i<n;i++){if(A[i]!=A[i-1]){continue;}bool flag=false;for(int j=0;j<k;j++){if('A'+j!=A[i]){if((i<n-1&&'A'+j!=A[i+1])||i==n-1){vis[i]=1;A[i]='A'+j;sum++;flag=true;break;}}}if(!flag){for(int j=0;j<k;j++){if('A'+j!=A[i]){if((i>1&&'A'+j!=A[i-2])||i==1){A[i-1]='A'+j;if(!vis[i-1]){vis[i-1]=1;sum++;}flag=true;break;}}}}}cout<<sum<<endl;cout<<A<<endl;}return 0;}
- CodeForces 219C Color Stripe
- Color Stripe--codeForces 219C
- codeforces 219C Color Stripe
- codeforces C. Color Stripe
- codeforces 219C C. Color Stripe(dp)
- CodeForces 219C Color Stripe (贪心)
- codeforces 219C Color Stripe(贪心)
- Codeforces 219C Color Stripe 贪心
- Codeforces 219C C. Color Stripe【dp+输出路径】
- 219C - Color Stripe-模拟
- Codeforces Round #135 (Div. 2)C. Color Stripe
- Codeforces Round #135 (Div. 2)-C. Color Stripe
- CodeForces 18C - Stripe
- Codeforces Round #135 (Div. 2) C. Color Stripe(字符串dp)
- CodeForces 21 C Stripe 2
- CodeForces 21C Stripe 2 (简单题)
- CodeForces 18C Stripe (简单题)
- CodeForces 21C Stripe 2 构造题
- Java Socket实战之三 传输对象
- 消息模式Toast.makeText用法
- SSH服务器登录慢的解决办法
- Intellij IDEA常用配置详解
- 功能强大的JS虚拟键盘
- Color Stripe--codeForces 219C
- Android开发精要1--Android系统架构
- LINUX c++线程池框架
- hdu 1134 卡特兰数+大数
- java获得当前时间的前一天时间
- Win7设置WIFI后自动断开的解决方法
- android 内存
- MACHINE_START MACHINE_END 宏
- 2.C++中虚析构函数的作用