Topic Portal
1 /*2 greedy: when m = = 2 o'clock, the result must be Ababab or Bababa, take the minimum amount of change; when M > 2 o'clock, change a letter when it is equal to the previous one3 at the same time, not equal to the next is the optimal solution4 */5#include <cstdio>6#include <cstring>7#include <algorithm>8 using namespacestd;9 Ten Const intMAXN = 5e5 +Ten; One Const intINF =0x3f3f3f3f; A CharS[MAXN]; - - intMainvoid) {//codeforces Round #135 (Div. 2) C. Color Stripe the //freopen ("c.in", "R", stdin); - - intN, M; - while(SCANF ("%d%d", &n, &m) = =2) { +scanf ("%s", S +1);intLen = strlen (S +1); - + if(M = =2) { A intC1 =0, C2 =0; at for(intI=1; i<=len; ++i) { - if(I &1) { - if(S[i] = ='A') c2++; - Elsec1++; - } - Else { in if(S[i] = ='A') c1++; - Elsec2++; to } + } - theprintf ("%d\n", Min (c1, C2)); * for(intI=1; i<=len; ++i) { $ if(I &1) printf ("%c", (C1 < C2)?'A':'B');Panax Notoginseng Elseprintf ("%c", (C1 < C2)?'B':'A'); - } thePuts (""); + } A Else { the intAns =0; s[len+1] = s[0] ='@'; + for(intI=2; i<=len; ++i) { - if(S[i] = = s[i-1]) { $ans++; $ for(intj=1; j<=m; ++j) { - CharCH ='A'+ J-1; - if(s[i] = = CH | | s[i+1] = = ch)Continue; the Else { -S[i] = ch; Break;Wuyi } the } - } Wu } -printf ("%d\n", ans); s[len+1] =' /'; Aboutprintf ("%s\n", S +1); $ } - } - - return 0; A}
Greedy codeforces Round #135 (Div. 2) C. Color Stripe