zoukankan      html  css  js  c++  java
  • codeforces 354 div2 C Vasya and String 前缀和

    C. Vasya and String
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotesbeauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.

    Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?

    Input

    The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n) — the length of the string and the maximum number of characters to change.

    The second line contains the string, consisting of letters 'a' and 'b' only.

    Output

    Print the only integer — the maximum beauty of the string Vasya can achieve by changing no more than k characters.

    Examples
    input
    4 2
    abba
    output
    4
    input
    8 1
    aabaabaa
    output
    5
    题意:一个只由'a','b'组成的字符串,长度为n,可以改变k个,求连续最长;
    思路:找出连续a的长度,包括0,同样的b也是,求前缀和,求出以k为长度最长的子串;
    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define mod 1000000007
    #define inf 999999999
    #define pi 4*atan(1)
    //#pragma comment(linker, "/STACK:102400000,102400000")
    int flaga[100010],jia;
    int flagb[100010],jib;
    int disa[100010];
    int disb[100010];
    int suma[100010];
    int sumb[100010];
    char a[100010];
    int main()
    {
        int x,y,z,i,t;
        scanf("%d%d",&x,&y);
        scanf("%s",a+1);
        flaga[0]=0;
        jia=1;
        flagb[0]=0;
        jib=1;
        for(i=1;i<=x;i++)
        {
            if(a[i]=='a')
            flaga[jia++]=i;
            else
            flagb[jib++]=i;
        }
        flaga[jia++]=x+1;
        flagb[jib++]=x+1;
        for(i=1;i<jib;i++)
        disa[i]=flagb[i]-flagb[i-1]-1;
        for(i=1;i<jia;i++)
        disb[i]=flaga[i]-flaga[i-1]-1;
        for(i=1;i<=100000;i++)
        suma[i]+=suma[i-1]+disa[i];
        for(i=1;i<=100000;i++)
        sumb[i]+=sumb[i-1]+disb[i];
        int ans=0;
        for(i=1;i<jia;i++)
        ans=max(ans,sumb[i+y]-sumb[i-1]);
        int ans1=0;
        for(i=1;i<jib;i++)
        ans1=max(ans1,suma[i+y]-suma[i-1]);
        if(min(jia,jib)-1<=y)
        printf("%d
    ",x);
        else
        {
            printf("%d
    ",max(ans1,ans)+y);
        }
        return 0;
    }
  • 相关阅读:
    Asp.net 基础4(自定义控件的使用之客户端脚本生成)
    Asp.net 基础3(自定义控件的使用)
    wpf 可以取消的单选checkbox
    wpf MaskedTextBox
    自定义 日期格式的datePicker
    wpf datagrid no record found style
    Sql语句绝妙用法
    .net反射简介
    c# 正则表达式小结
    如何获取地址栏地址
  • 原文地址:https://www.cnblogs.com/jhz033/p/5530168.html
Copyright © 2011-2022 走看看