您的位置:首页 > Web前端

Codeforces 814 C An impassioned circulation of affection

2017-06-08 23:09 344 查看
题目址: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 0x3f3f3f3f
using namespace std;
string str;
int n;
int dp
[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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: