Codeforce219C——贪心——Color Stripe
2015-07-20 21:16
393 查看
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
output
input
output
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
/* 对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; }
相关文章推荐
- POJ 3261 Milk Patterns sa+二分
- 剑指offer-面试题2.实例Singleton模式
- Cookbook编写
- C++中的四种强制类型转换
- 防火墙修改
- IT常用Terms
- ros创建package 总结
- hdu4135 co-prime(容斥原理)
- x86 INSTALL_FAILED_SHARED_USER_INCOMPATIBLE
- 2015072003 - 中国人去日本就医
- Log4net使用
- [LeetCode][Java] Construct Binary Tree from Preorder and Inorder Traversal
- javascript中英文字符长度判断
- 如何添加 actions
- Gcc对头文件与库文件的搜索路径
- MYCNCART博客系统
- MYCNCART博客系统
- HDJ2024C语言合法标识符
- 如何在myeclipse中使用自己的tomcat
- C++静态成员变量和静态成员函数