【codeforces 814C】An impassioned circulation of affection

【题目链接】:http://codeforces.com/problemset/problem/814/C

【题意】

给你一个只含小写字母的字符串;
长度<=1500;
然后你可以最多将m个字符改为任意字符;
然后问你字符为全c的最长的子串的最长长度;
有q个询问;

【题解】

对于每一个询问;
O(2*N)即可得到答案;
具体的;
维护以第i个字符作为最后的答案子串的最后一个位置,这个子串最左能到达哪里->l;
这个l是随着i的增加,单调不递减的;
且i向右移动一位,能很轻松地搞出新的l的位置;
对于每个i,算出子串的长度,然后取最大值就好;
(可以写个记忆化->f[26][1500];比Q*2*2500会优很多)

【Number Of WA

1

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1500+100;

int n,m,q,f[255][N];
char ts[5],s[N];

int main(){
    //Open();
    Close();//scanf,puts,printf not use
    //init??????
    cin >> n;
    cin >> (s+1);
    cin >> q;
    while (q--){
        cin >> m >> ts;
        char key = ts[0];
        if (f[key][m]!=0){
            cout <<f[key][m]<<endl;
            continue;
        }
        int l = 1,t = 0,ans = 0;
        rep1(i,1,n){
            if (s[i]!=key){
                t++;
            }
            while (t>m){
                if (s[l]!=key) t--;
                l++;
            }
            ans = max(ans,i-l+1);
        }
        f[key][m] = ans;
        cout << ans << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626271.html