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 3ABBACC

output

2ABCACA

input

3 2BBB

output

1BAB
/*
对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;
}

  

时间: 2024-10-07 02:05:38

Codeforce219C——贪心——Color Stripe的相关文章

codeforces219C - Color Stripe DP+贪心

题意:给你n,k,大意就是说给你一个已经涂满颜色长为n的字符串,现有k种颜色可以选择,问你最少要改变多少个箱子的颜色使得相邻箱子之间颜色不同. 解题思路:当k = 2 时单独讨论,不能用贪心,其余情况都可贪心得到. 解题代码: 1 // File Name: 219c.cpp 2 // Author: darkdream 3 // Created Time: 2014年07月26日 星期六 15时45分56秒 4 5 #include<vector> 6 #include<list>

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

题目传送门 1 /* 2 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 3 同时不和下一个相等就是最优的解法 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 const int MAXN = 5e5 + 10; 11 const int IN

PAT 甲级 1045 Favorite Color Stripe(DP)

题目链接 Favorite Color Stripe 题意:给定A序列和B序列,你需要在B序列中找出任意一个最长的子序列,使得这个子序列也是A的子序列 (这个子序列的相邻元素可以重复) 只要求出这个子序列长度的最大值即可 很基础的DP题,设f[i]为当前以a[i]结尾的子序列的长度的最大值,扫描B序列的每个元素时实时更新即可. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i

PAT 1045. Favorite Color Stripe (30)

1045. Favorite Color Stripe (30) Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form

PAT 1045. Favorite Color Stripe

Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe. It is

PAT1045. Favorite Color Stripe

Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe. It is

1045. Favorite Color Stripe (30)

LCS 最大公共子序列. memset函数需要include <string.h> 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cut

CodeForces 219C Color Stripe

Color Stripe Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 219C Description A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors.

pat1045. Favorite Color Stripe (30)

1045. Favorite Color Stripe (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off t