Codeforces Round #354 (Div. 2) C. Vasya and String
2016-06-01 23:29
232 查看
题目大意:有一个(a|b)^N的由a和b构成的长度为N的字符串,允许修改其中k位。问能构成的最长的全为a或全为b的子串的长度最长为多少。
思路,用两个队列分别保存前K+1个a和b的位置,以i为结尾的最长的子串的长度就是i减去队列头元素的值。
思路,用两个队列分别保存前K+1个a和b的位置,以i为结尾的最长的子串的长度就是i减去队列头元素的值。
#include <iostream> #include <cstdio> #include <memory.h> #include <queue> using namespace std; int main(int argc, const char * argv[]) { char input[1000005]; int N,K,res1; queue<int> posa; queue<int> posb; scanf("%d%d",&N,&K); for(int i=1;i<=N;i++){ cin>>input[i]; } posa.push(0);//预存0的位置,实际输入下标从1开始 posb.push(0); res1=0; for(int i=1;i<=N;i++){ if(input[i]=='a'){ posa.push(i); if(posa.size()>K+1) posa.pop(); } else{ posb.push(i); if(posb.size()>K+1) posb.pop(); } res1=max(res1,i-posb.front()); res1=max(res1,i-posa.front()); } printf("%d",res1); return 0; }
相关文章推荐
- 【原创】安装LoadRunner12.53 版本时出现Critical error的解决方法
- android无法监听软键盘收起解决方案
- 获得UITableViewCell 在屏幕中的位置
- 跟着沈毅学微电商(PHP):二
- 法线贴图——Normal Mapping
- python爬虫抓取豆瓣电影
- MyBatis基本使用步骤
- 【OpenCV】--Windows下配置OpenCV的Qt开发环境
- 非ARC下返回Block
- oracle 查询上机实验
- SQL 部分函数的使用,子查询,group by,虚拟字段,case……
- Java高级特性-annotation
- 【C++】关于const的使用
- 如何判断两个单向链表是否有相交,并找出交点
- Git && Python
- Linux下task_struct详解
- redis数据丢失及解决
- 第十四周上机实践——项目2-两个成员的类模板
- Android 使用service实现多线程断点续传下载
- 第二次团队冲刺--6