CodeForces 219C Color Stripe

C. Color Stripe
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <ctime>
#include <cstdlib>
#include <stack>
#include <map>
#include <set>
#include <list>

#define MP make_pair
#define PB push_back
#define INT_INF 0x3fffffff
#define LL_INF 0x3fffffffffffffff
#define EPS 1e-12
#define MOD 1000000007
#define PI 3.14159265358979323846
#define N 500010
#define E 100010

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned int Uint;
typedef double DB;

int n,m;
char s[N];

vector<int> deal()
{
    int cnt=0;
    for(int i=0; i<n; i++)
    {
        if((i&1) && s[i]!='B') cnt++;
        if(!(i&1) && s[i]!='A') cnt++;
    }
    vector<int> res;
    if(cnt<n-cnt)
    {
        res.PB(0);
        res.PB(cnt);
    }
    else
    {
        res.PB(1);
        res.PB(n-cnt);
    }
    return res;
}

char chose(char a,char b)
{
    for(char i='A'; i<'A'+m; i++)
        if(i!=a && i!=b) return i;
    return 0;
}

int main()
{
	while(~scanf("%d%d",&n,&m))
	{
	    scanf("%s",s);
	    if(m==2)
	    {
	        vector<int> ans=deal();
	        printf("%d\n",ans[1]);
	        if(ans[0]==0)
	        {
	            for(int i=0; i<n; i++)
                {
                    if((i&1)) printf("B");
                    else printf("A");
                }
	        }
	        else
	        {
	            for(int i=0; i<n; i++)
                {
                    if(i&1) printf("A");
                    else printf("B");
                }
	        }
	        printf("\n");
	    }
	    else
	    {
	        int ans=0;
	        for(int i=0; i<n; i++)
            {
                if(i+1<n && s[i]==s[i+1])
                {
                    char color;
                    if(i+2<n) color=chose(s[i],s[i+2]);
                    else color=chose(s[i],s[i]);
                    s[i+1]=color;
                    ans++;
                }
            }
            printf("%d\n%s\n",ans,s);
	    }
	}
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值