Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection(尺取)

来源:互联网 发布:python经典视频教程 编辑:程序博客网 时间:2024/06/11 15:13

题意:给你一个长度为n的字符串,q次询问m ch,每次问可最多改变m个字符,问最多连续多少个ch。

n <= 1000, q <= 200000.


思路:尺取法预处理记录出每个字母,改变m次能大连续长度。


代码:

#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 1505;char str[maxn];int n, dp[30][maxn], sum[30][maxn];void solve(){    for(int i = 0; i < 26; i++)    {        for(int j = 0; j <= n; j++)        {            int l = 1, r = 1;            int tmp = 0;            while(r <= n)            {                while(r-l+1 - (sum[i][r]-sum[i][l-1]) <= j) r++;                tmp = max(tmp, r-l);                l++;            }            dp[i][j] = tmp;        }    }}int main(void){    while(cin >> n)    {        memset(sum, 0, sizeof(sum));        memset(dp, 0, sizeof(dp));        for(int i = 1; i <= n; i++)        {            scanf(" %c", &str[i]);            for(int j = 0; j < 26; j++)                sum[j][i] = sum[j][i-1];            sum[str[i]-'a'][i]++;        }        solve();        int q;        scanf("%d", &q);        while(q--)        {            int c;            char ch;            scanf("%d %c", &c, &ch);            printf("%d\n", dp[ch-'a'][c]);        }    }    return 0;}


阅读全文
2 0
原创粉丝点击