Codeforces Round #418 (Div. 2) C (尺取+预处理)

Examples
Input

6
koyomi
3
1 o
4 o
4 m

Output

3
6
5

Input

15
yamatonadeshiko
10
1 a
2 a
3 a
4 a
5 a
1 b
2 b
3 b
4 b
5 b

Output

3
4
5
7
8
1
2
3
4
5

Input

10
aaaaaaaaaa
2
10 b
10 z

Output

10
10

Note

In the first sample, there are three plans:

In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3 is the best achievable;
In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6;
In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5. 

题意:给出一个长度为 n 的小写字母构成的字符串,询问 q 次每当替换了 m 个非 c 字符后最大的连续 c 子串的长度。(c表示Koyomi可能喜欢的颜色)

**分析:一开始以为是不可以暴力的。看了题解之后发现状态原来那么少。。
然后用尺取就可以了。。。。。预处理一下**

int last[30];
int vis[30];
int dp[27][N];
int num[N];
int n;
int solve(int c,int k){
    int ret=0;
    int l=1,r=1;
    int use=0;
    int maxx=0;
    while(l<=r&&r<=n){
        while(use<=k&&l<=r&&r<=n){
            ret=max(ret,maxx);
            if(num[r]==c){maxx++;r++;}
            else{use++;maxx++;r++;}
        }
        while(use>k&&l<=r){
            if(num[l]==c){l++;maxx--;}
            else{use--;l++;maxx--;}
        }
        ret=max(ret,maxx);
    }
    return ret;
}
void init(){
    for(int c=0;c<=25;++c)
        for(int k=1;k<=n;++k)
            dp[c][k]=solve(c,k);
}
int main(){
    //freopen("in.txt","r",stdin);
    sf("%d",&n); sf("%s",s+1);
    rep(i,1,n)num[i]=s[i]-'a';
    init();
    int q;sf("%d",&q);
    rep(i,1,q){
        int a;char c;sf("%d %c",&a,&c);
        pf("%d\n",dp[c-'a'][a]);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值