『算法-ACM竞赛-思维题』-CodeForces - 219C Color Stripe (思维)
『算法-ACM 竞赛-思维题』-CodeForces - 219C Color Stripe (思维)
ACM 思维题训练集合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.
InputThe 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
Input6 3
ABBACC
Output2
ABCACA
Input
3 2
BBB
Output1
BAB
题目很简单,类比平面图的四色定理,线性上有 3 色定理,即如果 k>=3 那么无论遇到什么情况 AABB 的时候,我们都可以将相同的变成第三种颜色,所以这里要特判 K=2 的时候,我一开始想了很久,就是不知道怎么处理,但是 k=2 的串只有 ABABA……和 BABABA……这两种情况,判断当前穿变成这两种那种花费少,就是答案了。
1 |
|