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)去跑一边就好了
题意:给你一个字符串,以及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; }
相关文章推荐
- codeforces 814 C. An impassioned circulation of affection(暴力)
- codeforces 814 C An impassioned circulation of affection
- Codeforces 814 C. An impassioned circulation of affection (dp)
- 【Codeforces 814 C. An impassioned circulation of affection】+ 尺取法
- 【codeforces 814C】An impassioned circulation of affection
- Codeforces-An impassioned circulation of affection (动态规划/尺取法)
- Codeforces Round #418 (Div. 2) 814 C. An impassioned circulation of affection
- Codeforces 814C-An impassioned circulation of affection
- Codeforces-An impassioned circulation of affection (尺取法)
- An impassioned circulation of affection CodeForces - 814C
- Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection
- An impassioned circulation of affection
- Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection
- C. An impassioned circulation of affection
- (dp)Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection
- C. An impassioned circulation of affection-尺取法或者DP
- Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection(双指针)
- Codeforces Round #418 (Div. 2) -- C. An impassioned circulation of affection(DP预处理)
- Codeforces#418 An impassioned circulation of affection
- CF Round #418( Div.2) An impassioned circulation of affection