zoukankan      html  css  js  c++  java
  • Codeforces Vasya and String 尺取法

    Vasya and String
    Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u


    Description
    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.

    Sample Input
    Input
    4 2
    abba
    Output
    4
    Input
    8 1
    aabaabaa
    Output
    5

    复制一段dalao的对尺取法的解释感觉很有道理

    题意:给你一串字符串和k个修改字符串的机会,让这个字符串获得最长连续相同子串

    题解:所谓尺取法,顾名思义,就是一把尺子(固定某一条件),不断向右(向左)移动,不断更细我们要的答案。在这里,我们只要从左往右,让修改的字符个数从0慢慢增加到k,中途将字符改成同一个字符(a改成b或者b改成a都行),最后修改字符数固定为k,每次向右移动时,如果字符串需要修改,那就改掉右面的字符,将之前最左边的字符换回来。那么我们可以用一个队列去实现。


    如果你看不懂上面也没关系,我再讲具体一些。我们从左边开始,扫描字符串。如果遇到a,那就把a丢进队列,如果遇到b,且此时队列中b的个数不超过k的话,就把b丢进去。如果b的个数等于k,且又遇到一个b,那就记录下此时队列长度,这个时候队列里k个b可以当作a,所以可以记录队列元素个数,更新答案最大值。之后将队列前面元素弹出,直到弹出一个b,再将新的b压进队列……一直扫描直到字符串尾。


    接下来将a和b互换,重复上面步骤,更新最大值。最后输出最大值。


    #include <iostream>
    #include <cstdio>
    #include <stdio.h>
    #include <stdlib.h>
    #include <algorithm>
    #include <queue>
    
    using namespace std;
    
    char a[1000005];
    
    int main()
    {
        //freopen("input.txt","r",stdin);
        int n,k;
        while(~scanf("%d%d",&n,&k))
        {
            scanf("%s",&a);
            queue<char> q;
    
            int maxn=0;
            int len=0;
    
            for(int i=0;i<n;i++)
            {
                if(a[i]=='a')
                    q.push(a[i]);
                else if(len<k)
                {
                    q.push(a[i]);
                    len++;
                }
                else
                {
                    
                    maxn=max(maxn,(int)q.size());
                    while(!q.empty()&&q.front()=='a')
                        {
                            q.pop();
                        }
                    if(!q.empty())
                    {
                        q.pop();
                        q.push(a[i]);
                    }
                }
            }
            maxn=max(maxn,(int)q.size());
            while(!q.empty())
            {
                q.pop();
            }
            len=0;
             for(int i=0;i<n;i++)
            {
                if(a[i]=='b')
                    q.push(a[i]);
                else if(len<k)
                {
                    q.push(a[i]);
                    len++;
                }
                else
                {
                    
                    maxn=max(maxn,(int)q.size());
                    while(!q.empty()&&q.front()=='b')
                        {
                            q.pop();
                        }
                    if(!q.empty())
                    {
                        q.pop();
                        q.push(a[i]);
                    }
                }
            }
            maxn=max(maxn,(int)q.size());
            printf("%d
    ",maxn);
    
        }
    }
    

      


  • 相关阅读:
    高手详解:sscanf函数的高级用法
    堆排序——BuildHeap和Heapify函数的实现
    递归与动态规划求解最长公共子序列
    分享:crpcut 1.8.4 发布,C++ 的单元测试框架
    团队展示 京拍档 电商运营服务、电子商务服务外包 首家京东代运营电子商务服务平台
    Linux中link,unlink,close,fclose详解
    常用排序算法的c++实现(冒泡,选择,插入,堆,shell,快速,归并 )与sort()对比 coder_xia的专栏 博客频道 CSDN.NET
    CAJ文件转PDF文件方法
    递归与动态规划求解最长公共子序列
    NLP Job 专注自然语言处理&机器学习等领域的求职招聘 | 关注自然语言处理|机器学习|数据挖掘|搜索引擎|计算广告|推荐算法等相关领域的工作机会
  • 原文地址:https://www.cnblogs.com/Hyouka/p/5702105.html
Copyright © 2011-2022 走看看