CodeForces - 814C An impassioned circulation of affection

An impassioned circulation of affection
题 意:给你一串长度为n的字符串,q个询问,mi,ci。mi代表你可以修改字符的次数,ci代表修改后ci连续最长是多少。
数据范围:
1<=n<=1500
1<=q<=2e5
样例输入:

6
koyomi
3
1 o
4 o
4 m

样例输出:

3
6
5

思 路:连续的操作,可以考虑用尺取法。这题就是一个很裸的尺取法。
收 获:连续的操作,考虑用尺取。div2的abc题其实都不难,多思考,多总结

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e3+5;
map<int,int> mp;
int n,m;
char a[maxn];
int solve(int time,char ch[2]){
    int MAX = -1;
    int res = n+1;
    int s = 0,t = 0,ans=0;
    for(;;){
        while(t < n){
            if(a[t] == ch[0]){
                ans++;t++;
            }
            else if(time>0){
                time--;
                ans++;t++;
            }else if(time<=0){
                break;
            }
        }
        MAX = max(MAX,ans);
        if(t == n)return MAX;
        if(a[s] == ch[0]){
            ans--;
            s++;
        }else{
            ans--;
            s++;
            time++;
        }
    }
}
int main() {
    scanf("%d",&n);
    scanf("%s",a);
    scanf("%d",&m);
    int t;char ch[2];
    while(m--){
        scanf("%d %s",&t,ch);
        int MAX = solve(t,ch);
        printf("%d\n",MAX);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37129433/article/details/81667365