Greedy codeforces Round #135 (Div. 2) C. Color Stripe

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.