Codeforces 676 C. Vasya and String

Source: Internet
Author: User

Title Link: Http://codeforces.com/contest/676/problem/C

A string containing only A and B, you can change the K, to find the most beautiful value, the beauty value is defined as the substring contains the same characters of the oldest string length

Topic Analysis: Define the starting point and end point, direct greed. For K-Times operation, either full-or full-change B, the two cases to take the maximum, the calculation of the time with a two-point method, R right until K runs out, and then L from the left to add, constantly restore K value (because it is a substring of changes, to make the substring the longest, Then change the character must be in the order in which the change is continuous in the middle can not have no change)

The code is as follows:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
using namespace std;
typedef __int64 LL;

Double dp[12][12];
int n, K;
String str;

int solve (char ch)
{
    int l = 0, r = 0, ans = 0, cnt = 0;
    while (R < n && L < N)
    {while

        ((str[r] = = CH | | cnt < k) && R < N)//Find the location of the right endpoint 
  {
            if (str[r]! = ch) cnt++;
            r++;
        }
        ans = max (ans, r-l);
        while (l<=r && str[l] = = ch) The left end of the l++;//substring is shifted to the right
        l++;
        cnt--;
    }
    return ans;
}

int main ()
{
    while (scanf ("%d%d", &n, &k)! = EOF)
    {
        cin>>str;
        printf ("%d\n", Max (Solve (' a '), Solve (' B ')));
    }
    return 0;
}





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.