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
- codeforces C. An impassioned circulation of affection
- 【Codeforces 814 C. An impassioned circulation of affection】+ 尺取法
- codeforces 814C An impassioned circulation of affection
- codeforces 814C An impassioned circulation of affection
- codeforces 814 C An impassioned circulation of affection
- Codeforces 814C-An impassioned circulation of affection
- codeforces 814 C. An impassioned circulation of affection(暴力)
- Codeforces 814C An impassioned circulation of affection【Dp】
- codeforces 814C An impassioned circulation of affection
- Codeforces 814 C An impassioned circulation of affection
- Codeforces 814C An impassioned circulation of affection (dp)
- CodeForces-814C An impassioned circulation of affection
- Codeforces 814C An impassioned circulation of affection(dp)
- 814C An impassioned circulation of affection
- Codeforces#418C An impassioned circulation of affection
- Codeforces Round #418 C--An impassioned circulation of affection
- Codeforces#418 An impassioned circulation of affection
- An impassioned circulation of affection CodeForces
- leetcode-51/52-N皇后问题
- 【视觉 SLAM-2】 视觉SLAM- ORB 源码详解 2
- 百度开发者中心聚力AI开放能力,打造新时代下开发者的“淘金矿”
- android:字符串转码的使用
- MySQL执行外部sql脚本文件的命令
- Codeforces 814 C An impassioned circulation of affection
- MySQL知识GET
- SDCC中中断函数的注意事项
- iBase4J部署总结
- Java之多态知识点和使用详解
- Sublime3配置简单的python环境(自动补全,自动格式化,调试,汉化)
- 细说C#多线程那些事-线程基础
- linux基础之守护进程
- Bootstrap 下拉框搜索多选