Vasya and String CodeForces
来源:互联网 发布:人工智能语言 编辑:程序博客网 时间:2024/05/16 19:09
题目链接
题目大意: 给定一个只含a,b的字符串,可以将其中有限个数的a转化成b,b转化成a。
问获得的最大的连续的串的长度
Input
4 2abba
Output
4
Input
8 1aabaabaa
Output
5
code 1 尺取法:
#include <cstring>#include <iostream>#include <algorithm>#include <cstdio>#include <cmath>#define ms(x) memset(x, 0, sizeof(x))using namespace std;typedef long long LL;#define inf 10000000#define N 210010int n, k;char s[N];int ruller(char x){ int ans = 0; int r=0, l=0, cnt = 0; while(l<n && r<n) { while((s[r]==x||cnt<k)&&r<n) { if(s[r]!=x) cnt++; r++; } ans = max(ans, r-l); while(s[l]==x&&l<=r) l++; l++; cnt--; } return ans;}int main (){ cin>>n>>k; scanf("%s",s); int a1 = ruller('a'); int a2= ruller('b'); int ans = max(a1, a2); cout<<ans<<endl; return 0;}
code 2 二分查找:
#include <cstring>#include <iostream>#include <algorithm>#include <cstdio>#include <cmath>#define ms(x) memset(x, 0, sizeof(x))using namespace std;typedef long long LL;#define inf 10000000#define N 210010int n, k;char s[N];int sum[N];int bina(char x){ ms(sum); for(int i=1;i<=n;i++) { sum[i] = sum[i-1]; if(s[i] == x) sum[i]++; } int ans = 0; for(int i=0;i<=n;i++) { int l=i, r=n; while(l<=r) { int mid = (l+r)>>1; if(sum[mid] - sum[i] <=k) l = mid+1; else r = mid-1; } ans = max(ans, r-i); } return ans;}int main (){ cin>>n>>k; scanf("%s",s+1); int a1 = bina('a'); int a2= bina('b'); int ans = max(a1, a2); cout<<ans<<endl; return 0;}
阅读全文
0 0
- Vasya and String CodeForces
- codeforces 676C Vasya and String
- Codeforces 676C Vasya and String
- codeforces 676C. Vasya and String
- Codeforces 676 C. Vasya and String
- 【CodeForces 676】C - Vasya and String
- codeforces Vasya and Basketball
- Codeforces Vasya and Socks
- Codeforces Round #354 (Div. 2)676C. Vasya and String
- Codeforces Round #354 (Div. 2) C .Vasya and String
- Codeforces 676C Vasya and String (两点法)
- codeforces 676C Vasya and String (尺取法)
- codeforces 676C Vasya and String 前缀数组+二分查找
- codeforces #354 div.2 C &&676C Vasya and String
- codeforces 676C Vasya and String(尺取法)
- Codeforces 676C Vasya and String 尺取法
- Codeforces Round #354 (Div. 2)-C. Vasya and String
- Codeforces Round #354 (Div. 2) C Vasya and String
- mac命令行工具
- LeetCode刷题(17)
- 统计输入字符中的空白符、换行符、制表符
- 深入 JavaScript 数组:进化与性能
- 链表类的设计——学生信息管理系统
- Vasya and String CodeForces
- flume学习01-flume介绍
- TreeSet 的功能特点
- LintCode 二分查找
- Contest4:A+B problem
- 一封23岁女孩ISTJ职业及副业求助信
- Android如何获取assets或者raw目录的视频文件路径播放视频
- 刚坐地铁回来, 买了个token
- 归档,压缩,rsync传输文件