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

原创 2016年08月30日 17:38:35

原题链接

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

1.如果n == 1则不需要改动。

2.如果k == 2 && n > 1则比较将原字符串改为ABABABA..和BABABAB...的代价哪个小

3.如果k > 2 && n > 1从下标1开始遍历整个字符串str, 若str[i] == str[i-1]则改动str[i]这个字符使之不等于str[i-1]和str[i+1]

#include <bits/stdc++.h>
#define maxn 500005
using namespace std;
typedef long long ll;

char str[maxn];
char p[26];
int main(){
	
	//freopen("in.txt", "r", stdin);
	for(int i = 0; i < 26; i++)
	 p[i] = 'A' + i;
	int n, k;
	
	scanf("%d%d%s", &n, &k, str);
	int len = strlen(str);
	if(n == 1){
		puts("0");
		puts(str);
	}
	else if(k == 2){
		
		int k1 = 0, k2 = 0, d1 = 0, d2 = 1;
		for(int i = 0; i < len; i++){
			if(str[i] != p[d1]){
				k1++;
			}
			if(str[i] != p[d2]){
				k2++;
			}
			d1 ^= 1;
			d2 ^= 1;
		}
		if(k1 < k2){
			d1 = 0;
			for(int i = 0; str[i]; i++, d1 ^= 1)
			 str[i] = p[d1];
			printf("%d\n", k1);
			puts(str);
		}
		else{
			d2 = 1;
			for(int i = 0; str[i]; i++, d2 ^= 1)
			  str[i] = p[d2];
			printf("%d\n", k2);
			puts(str);
		}
	}
	else{
		int ans = 0;
		for(int i = 1; i < len; i++){
			if(str[i] == str[i-1]){
				for(int j = 0; j < k; j++){
					if(p[j] != str[i-1]){
						if(i < len-1){
							if(p[j] != str[i+1]){
								str[i] = p[j];
								ans++;
								break;
							} 
						}
						else{
						 str[i] = p[j];
						 ans++;
						 break;
					    }
					}
				}
			}
		}
		printf("%d\n", ans);
		puts(str);
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

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

C. Color Stripe time limit per test 2 seconds memory limit per test 256 megabytes i...
  • tjqACM
  • tjqACM
  • 2014年08月22日 21:27
  • 256

Codeforces Round #135 (Div. 2) C. Color Stripe(字符串dp)

C. Color Stripe time limit per test 2 seconds memory limit per test 256 megabytes i...

Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

Codeforces Round #135 (Div. 2) - D. Choosing Capital for Treeland(dfs / 树形DP)

题目: http://codeforces.com/problemset/problem/219/D 题意: 给出n个点,n-1条有向边的树,以一个点为中心,从这个点出发到其他各点需要更改的...

Codeforces Round #202 (Div. 2)B. Color the Fence(贪心,模拟)

B. Color the Fencetime limit per test:2 secondsmemory limit per test:256 megabytesinput:standard inp...

CodeForces 219C Color Stripe (贪心)

题意: 用前k个大写字母,替代原来字符串的大写字母,使得两两相邻的字母不一样。要求使用最少的替代次数。 思路: 因为是在dp专题里看到,所以一直想dp的做法,后来实在不知道应该怎么搞。 ...

codeforces C. Color Stripe

A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k ...

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

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

codeforces 219C C. Color Stripe(dp)

题目链接:codeforces 219C题目大意:给出一个字符串,只包含k种字符,问最少修改多少个字符(不增长新的种类)能够得到一个新的字符串,这个字符串满足相邻的字符没有相同的。题目分析: 定义状态...

【Codeforces Round 374 (Div 2)C】【DAG上的DP】Journey 有向无环图从1到n时刻T内的最多经过点数

C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #135 (Div. 2)-C. Color Stripe
举报原因:
原因补充:

(最多只允许输入30个字)