Codeforces 814 C An impassioned circulation of affection

来源:互联网 发布:字幕编辑软件 编辑:程序博客网 时间:2024/05/20 22:40

题目址:http://codeforces.com/contest/814/problem/C
题意:给你一个字符串,以及m次查询,每次查询告诉你一个次数num和字符c,求在这个字符串中最多改变num次字符,求最长连续的字符c有多长。
思路:m的范围很大,如果每一次都去查询的话肯定会超时,但是字符串最多才1000个字符,所以说应该是有重复查询的,我们可以这样想,如果次数num>=n的话是不是任何字符的最长连续区间长度为n,然而其他情况如果没有查询过的话就直接用尺取法O(n)去跑一边就好了

#include <iostream>#include <cstring>#include <string>#include <queue>#include <vector>#include <map>#include <algorithm>#define N 1510#define LL long long #define inf 0x3f3f3f3fusing namespace std;string str;int n;int dp[N][30];int solve(int num, char c) {    int sum = 0;    int l = 0, r = 0;    int ans = 0, cnt = 0;    while (l <= r&&r < str.length()) {        cnt++;        if (str[r] != c) {            ans++;        }        if (ans <= num) {            sum = max(sum, cnt);        }        else if (ans > num) {            cnt--;            if (str[l] != c) {                ans--;                sum = max(sum, cnt);            }            l++;        }        r++;    }    return sum;}int main() {    int m, num;    char c;    cin.sync_with_stdio(false);    while (cin >> n) {        cin >> str;        cin >> m;        memset(dp, 0, sizeof(dp));        while (m--) {            cin >> num >> c;            if (num >= n) {                cout << n << endl;            }            else {                if (!dp[num][c - 'a']) {                    dp[num][c - 'a'] = solve(num, c);                }                cout << dp[num][c - 'a'] << endl;            }        }    }    return 0;}
阅读全文
0 0