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

Input
6 3ABBACC
Output
2ABCACA
Input
3 2BBB
Output
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;}

原创粉丝点击