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
Output
Input
Output
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; }
相关文章推荐
- Codeforces 2A
- codeforces 556A Case of the Zeros and Ones
- Codeforces 14D Two Path Solution
- 【Codeforces 729 A Interview with Oleg 】
- codeforces 817C Really Big Numbers 二分
- Codeforces 877D. Olya and Energy Drinks BFS+并查集
- 2014.7.14 CodeForces C题 字符串上的动态规划
- CodeForces 429B Working out(dp,四个角递推)
- Codeforces 543A Writing Code
- CodeForces 738B - Spotlights(前缀和)
- CodeForces 550B Preparing Olympiad(DFS回溯+暴力枚举)
- CodeForces 844A Diversity (超级超级水唯一需要想得就是开个标记数组吧)
- CodeForces 86D(Yandex.Algorithm 2011 Round 2)
- Codeforces 585D Lizard Era: Beginning
- Codeforces 22B Bargaining Table
- codeforces - 707B - Bakery(图的遍历)
- Codeforces 600E Lomsat gelral [dsu on tree(树上启发式合并)]
- codeForces 192A.Funky Numbers(二分)
- CodeForces 370A Rook, Bishop and King
- Codeforces 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest A题 (String模拟)