您的位置:首页 > 其它

CodeForces 219C Color Stripe

2012-09-17 22:19 399 查看
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
;

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: