CodeForces 219C Color Stripe

Color Stripe

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status Practice CodeForces 219C

Description

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 Input

Input

6 3ABBACC

Output

2ABCACA

Input

3 2BBB

Output

1BAB

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 int main()
 6 {
 7   int n,m;
 8   char s[500100];
 9   while(scanf("%d%d",&n,&m)!=EOF)
10   {
11   scanf("%s",s);
12   int i,ans=0;
13   m+=64;
14   if(n==1)
15  {
16      cout<<ans<<endl<<s<<endl;
17      continue;
18  }
19      if(m==66)
20      {
21          int l=0,r=0;
22          for(i=0;i<n;i++)
23          if(i%2==0)
24          {
25              if(s[i]==‘B‘) l++;
26              else r++;
27          }
28          else
29          {
30              if(s[i]==‘A‘) l++;
31              else r++;
32          }
33          if(l<r)
34          {
35              cout<<l<<endl;
36              for(i=0;i<n;i++)
37              if(i%2==0) cout<<‘A‘;
38              else cout<<‘B‘;
39          }
40          else
41          {
42              cout<<r<<endl;
43              for(i=0;i<n;i++)
44              if(i%2==0) cout<<‘B‘;
45              else cout<<‘A‘;
46          }
47          cout<<endl;
48          continue;
49      }
50   s[n]=‘#‘;
51    for(i=1;i<n;i++)
52   if(s[i]==s[i-1])
53   {
54       ans++;
55       for(int j=65;j<=m;j++)
56       if(j==s[i]||j==s[i+1]) continue;
57       else
58       {
59           s[i]=j;
60           break;
61       }
62   }
63   s[n]=‘\0‘;
64   cout<<ans<<endl<<s<<endl;
65
66  }
67     return 0;
68 }

时间: 2025-01-02 06:04:45

CodeForces 219C Color Stripe的相关文章

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

题目传送门 1 /* 2 贪心:当m == 2时,结果肯定是ABABAB或BABABA,取最小改变量:当m > 2时,当与前一个相等时, 改变一个字母 3 同时不和下一个相等就是最优的解法 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 const int MAXN = 5e5 + 10; 11 const int IN

codeforces219C - Color Stripe DP+贪心

题意:给你n,k,大意就是说给你一个已经涂满颜色长为n的字符串,现有k种颜色可以选择,问你最少要改变多少个箱子的颜色使得相邻箱子之间颜色不同. 解题思路:当k = 2 时单独讨论,不能用贪心,其余情况都可贪心得到. 解题代码: 1 // File Name: 219c.cpp 2 // Author: darkdream 3 // Created Time: 2014年07月26日 星期六 15时45分56秒 4 5 #include<vector> 6 #include<list>

PAT 甲级 1045 Favorite Color Stripe(DP)

题目链接 Favorite Color Stripe 题意:给定A序列和B序列,你需要在B序列中找出任意一个最长的子序列,使得这个子序列也是A的子序列 (这个子序列的相邻元素可以重复) 只要求出这个子序列长度的最大值即可 很基础的DP题,设f[i]为当前以a[i]结尾的子序列的长度的最大值,扫描B序列的每个元素时实时更新即可. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i

PAT 1045. Favorite Color Stripe (30)

1045. Favorite Color Stripe (30) Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form

PAT 1045. Favorite Color Stripe

Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe. It is

PAT1045. Favorite Color Stripe

Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe. It is

1045. Favorite Color Stripe (30)

LCS 最大公共子序列. memset函数需要include <string.h> 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cut

codeforces 349B Color the Fence 贪心,思维

1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1-9每个字母分别要ai升油漆,问最多可画多大的数字. 贪心,也有点考思维. #include<bits/stdc++.h> using namespace std; #define LL long long #define INF 0x3f3f3f3f int main() { int v,a[1

pat1045. Favorite Color Stripe (30)

1045. Favorite Color Stripe (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off t