Codeforces 219C C. Color Stripe【dp+输出路径】

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.

Examples

Input

6 3
ABBACC

Output

2
ABCACA

Input

3 2
BBB

Output

1
BAB

 

题目大意:

给你一个长度为N的字符串,其中一共允许有K个字符(从A开始数);问将这个字符串变成一个没有相邻的两个字符相等的字符串,最少需要改动多少个字符。并且输出任意一个可行解。


思路:


1、考虑dp求解,设定dp【i】【j】表示前i个(包含第i个)字符满足了要求之后的最少改动次数。

那么不难想到dp方程:

dp【i】【j】=min(dp【i】【j】,dp【i-1】【k】+a【i】-‘A’==j?0:1);【其中要求:j!=k】;

表示当前情况可以从上一位子的状态转移过来,如果当前字符修改为字符j,那么需要花费一次改动,否则不需要改动。

那么最小改动次数,维护一下min(dp【n】【i】)即可。


2、那么考虑如何输出路径,我们设定pre【i】【j】=x,表示当前dp【i】【j】的最优状态是从dp【i-1】【x】的最优状态转移过来的。

那么我们在其dp过程中维护最后一个x,然后逆序回溯记录其解输出即可。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int dp[500400][30];
int pre[500400][30];
char a[500400];
char ans[500400];
int main()
{
    int n,k;
    while(~scanf("%d%d",&n,&k))
    {
        scanf("%s",a+1);
        memset(dp,0,sizeof(dp));
        memset(pre,0,sizeof(pre));
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<k;j++)
            {
                dp[i][j]=0x3f3f3f3f;
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<k;j++)
            {
                for(int kk=0;kk<k;kk++)
                {
                    if(j==kk)continue;
                    if(j==a[i]-'A')
                    {
                        if(dp[i][j]>dp[i-1][kk])
                        {
                            dp[i][j]=dp[i-1][kk];
                            pre[i][j]=kk;
                        }
                    }
                    else
                    {
                        if(dp[i][j]>dp[i-1][kk]+1)
                        {
                            dp[i][j]=dp[i-1][kk]+1;
                            pre[i][j]=kk;
                        }
                    }
                }
            }
        }
        int minn=0x3f3f3f3f;
        int pos;
        for(int i=0;i<k;i++)
        {
            if(dp[n][i]<minn)
            {
                minn=dp[n][i];
                pos=i;
            }
        }
        printf("%d\n",minn);
        if(minn==0)
        {
            printf("%s\n",a+1);
            continue;
        }
        int tt=n;
        ans[tt]=pos+'A';
        while(1)
        {
            ans[tt-1]=pre[tt][pos]+'A';
            pos=pre[tt][pos];
            tt--;
            if(tt==1)break;
        }
        for(int i=1;i<=n;i++)
        {
            printf("%c",ans[i]);
        }
        printf("\n");
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值