题目1 : Same Letters In A Row

版权声明:如有错误,请指出,不胜感激。 https://blog.csdn.net/qq_36424540/article/details/84260330

描述

Litter Ho has a string of lowercase letters. He wants to re-order the letters so that the same letters are placed together in a row.

Unfortunately he can do at most K times of swaps. Each time he can only swap two letters. What is the maximum number of the consective same letters?

Assume the string is "bababbaa" and K = 1. By 1 swapping Little Ho can get "aabbbbaa". "bbbb" has length 4 which is also the maximum number of the consective same letters Little Ho can get.  

输入

The fist line contains an integer K. (1 <= K <= 100000)  

The second line contain a string of lowercase letters. The length is no more than 100000.

输出

Output the answer.

样例输入

1  
bababbaa

样例输出

4

  我脑子里出现的第一个想法就是 二分,应该也是可以的,只不过复杂度稍微高一点。需要  O(nlog(n)) 的复杂度。

还可以用 滑动窗口来做,利用这样的一个性质。

感觉这个性质真的是,太妙了。

判断一个窗口可不可行,只需要 总的 \large ch>=j-i+1 并且不是 ch 的字符小于等于 n 个就行了。

所以我们枚举左端点,确定右端点 就好了。

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;++i)

const int N=100010;

char str[N];

int sum[N][26];
int num[26];

int n;
int is_ok(int l,int r)
{
    for(int i=0; i<26; i++) {
        int t=sum[r][i]-sum[l-1][i];
        int tmp=r-l+1-t;
        if(num[i]>=r-l+1&&tmp<=n)return 1;
    }
    return 0;
}

int main()
{

    scanf("%d",&n);
    scanf("%s",str+1);

    int len=strlen(str+1);

    for(int i=1; i<=len; i++) {
        num[str[i]-'a']++;
        sum[i][str[i]-'a']=sum[i-1][str[i]-'a']+1;

        for(int j=0; j<26; j++) {
            if(j==str[i]-'a')continue;
            sum[i][j]=sum[i-1][j];
        }
    }

    int l=1,r=1,ans=1;

    while(r<=len) {
        while(r<=len&&is_ok(l,r))r++;
        ans=max(ans,r-l);
        l++;
    }
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/84260330
Row