Codeforce219C——贪心——Color Stripe

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 test(s)
input
6 3
ABBACC
output
2
ABCACA
input
3 2
BBB
output
1
BAB
/*

对m == 2 的情况特殊处理  假顶A出现在奇数项,B出现在奇数项,比较出现的次数,如果A出现次数少于B,那么该A,反之改B

对于 m > 2 的情况 正常处理

*/

#include <cstdio>

#include <cstring>

#include <algorithm>

using namespace std;

const int MAX = 500000 + 10;

int main()

{

    int n, m;

    char s[MAX];

    while(~scanf("%d%d", &n, &m)){

        scanf("%s", s);

        int len = strlen(s);

        char ch;

        int ans = 0;

        if(m == 2){

           int c1 = 0, c2 = 0;

           for(int i = 0 ; i < len; i++){

                   if(i % 2 == 1) {

                           if(s[i] == 'A') c1++;

                           else c2++;

                   }

                   else {

                           if(s[i] == 'A') c2++;

                           else c1++;

                   }

           }

                   int ans = min(c1, c2);

                   for(int i = 0; i < len; i++){

                           if(ans == c1){

                                if(i % 2 == 1){

                                        s[i] = 'B';

                                }

                                else s[i] = 'A';

                           }

                           else {

                                if(i % 2 == 1){

                                        s[i] = 'A';

                                }

                                else s[i] = 'B';

                           }

                   }

                   printf("%d\n", ans);

                   printf("%s\n",s);

        }

        else {

        for(int i = 1 ; i < len; i++){

                if(s[i-1] == s[i]){

                        ans++;

                        for(int j = 1; j <= m ;j++){

                                ch = 'A' + j - 1;

                                if(s[i] == ch || s[i+1] == ch);

                                else {

                                                s[i] = ch;

                                                break;

                                        }

                                }

                        }

                }

        printf("%d\n%s\n", ans, s);

        }

    }

    return 0;

}

                 

  

你可能感兴趣的:(color)