Codeforces 676C Vasya and String (ruler)

Source: Internet
Author: User

The topic probably says to a string consisting of a and B, which can change up to K characters, and ask how long the longest consecutive and identical string can be obtained by changing it.

Use the ruler, change to a and change to B to do once: double pointer I and j,j non-stop +, and then if you encounter the need to change and change the number of times to let i++ correct the number of changes, and finally update the answer. Time complexity O (n).

Also, note the situation of the k=0.

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 Charstr[111111];6 intMain () {7     intn,k;8scanf"%d%d%s",&n,&k,str);9     intI=0, j=0, tmpn=0, tmpk=0, ans=0;Ten      while(j<N) { One         if(str[j]=='a'){ A++TMPN; -}Else if(k==0){ -tmpn=0; the}Else{ -              while(tmpk==k) { -                 if(str[i]=='b'){ ---TMPK; +                 } ---TMPN; +++i; A             } at++TMPK; -++TMPN; -         } -ans=Max (ANS,TMPN); -++J; -     } inI=0; j=0; tmpn=0; tmpk=0; -      while(j<N) { to         if(str[j]=='b'){ +++TMPN; -}Else if(k==0){ thetmpn=0; *}Else{ $              while(tmpk==k) {Panax Notoginseng                 if(str[i]=='a'){ ---TMPK; the                 } +--TMPN; A++i; the             } +++TMPK; -++TMPN; $         } $ans=Max (ANS,TMPN); -++J; -     } theprintf"%d", ans); -     return 0;Wuyi}

Codeforces 676C Vasya and String (ruler)

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.