Codeforces Round #533 (Div. 2) B. Zuhair and Strings 【模拟】
传送门:http://codeforces.com/contest/1105/problem/B
1 second
256 megabytes
standard input
standard output
Given a string ss of length nn and integer kk (1≤k≤n1≤k≤n). The string ss has a level xx, if xx is largest non-negative integer, such that it's possible to find in ss:
- xx non-intersecting (non-overlapping) substrings of length kk,
- all characters of these xx substrings are the same (i.e. each substring contains only one distinct character and this character is the same for all the substrings).
A substring is a sequence of consecutive (adjacent) characters, it is defined by two integers ii and jj (1≤i≤j≤n1≤i≤j≤n), denoted as s[i…j]s[i…j]= "sisi+1…sjsisi+1…sj".
For example, if k=2k=2, then:
- the string "aabb" has level 11 (you can select substring "aa"),
- the strings "zzzz" and "zzbzz" has level 22 (you can select two non-intersecting substrings "zz" in each of them),
- the strings "abed" and "aca" have level 00 (you can't find at least one substring of the length k=2k=2 containing the only distinct character).
Zuhair gave you the integer kk and the string ss of length nn. You need to find xx, the level of the string ss.
The first line contains two integers nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105) — the length of the string and the value of kk.
The second line contains the string ss of length nn consisting only of lowercase Latin letters.
Print a single integer xx — the level of the string.
8 2
aaacaabb
2
2 1
ab
1
4 2
abab
0
In the first example, we can select 22 non-intersecting substrings consisting of letter 'a': "(aa)ac(aa)bb", so the level is 22.
In the second example, we can select either substring "a" or "b" to get the answer 11.
题意概括:
给一串长度为 N 的字符,求长度为 K 的由相同单种字母组成的子串的最大数量。
解题思路:
单指针模拟长度为 K 的子串的起始点,O(N)遍历一遍整个字符,记录每种字母满足条件的子串所对应的数量。
AC code:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 2e5+;
char str[MAXN];
int num[];
int main()
{
int N, K;
scanf("%d %d", &N, &K);
scanf("%s", str);
int len = strlen(str);
int st = ;
bool flag;
while(st+K- < len){
flag = true;
for(int i = st+; i <= st+K-; i++){
//printf("%c %c\n", str[i], str[i-1]);
if(str[i] != str[i-]){
flag = false;
st = i;
break;
}
}
if(flag){
//printf("%d %c\n",st, str[st]);
num[str[st]-'a']++;
st = st+K;
}
//st+=K;
}
int ans = ;
for(int i = ; i < ; i++){
//printf("%d %d\n", i, num[i]);
ans = max(num[i], ans);
} printf("%d\n", ans);
return ; }
Codeforces Round #533 (Div. 2) B. Zuhair and Strings 【模拟】的更多相关文章
- Codeforces Round #533(Div. 2) B.Zuhair and Strings
链接:https://codeforces.com/contest/1105/problem/B 题意: 给一个字符串和k,连续k个相同的字符,可使等级x加1, 例:8 2 aaacaabb 则有aa ...
- Codeforces Round #533 (Div. 2) B. Zuhair and Strings(字符串)
#include <bits/stdc++.h> using namespace std; int main() { int n,k;cin>>n>>k; stri ...
- Codeforces Round #533 (Div. 2)题解
link orz olinr AK Codeforces Round #533 (Div. 2) 中文水平和英文水平都太渣..翻译不准确见谅 T1.给定n<=1000个整数,你需要钦定一个值t, ...
- 水题 Codeforces Round #302 (Div. 2) A Set of Strings
题目传送门 /* 题意:一个字符串分割成k段,每段开头字母不相同 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 */ #include <cstdio> ...
- Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)
Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...
- Codeforces Round #533 (Div. 2) Solution
A. Salem and Sticks 签. #include <bits/stdc++.h> using namespace std; #define N 1010 int n, a[N ...
- Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS
题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...
- Codeforces Round #533 (Div. 2) C. Ayoub and Lost Array 【dp】
传送门:http://codeforces.com/contest/1105/problem/C C. Ayoub and Lost Array time limit per test 1 secon ...
- Codeforces Round #533(Div. 2) C.Ayoub and Lost Array
链接:https://codeforces.com/contest/1105/problem/C 题意: 给n,l,r. 一个n长的数组每个位置可以填区间l-r的值. 有多少种填法,使得数组每个位置相 ...
随机推荐
- Windows Server 2008系统中IE8启用和禁用JS
Windows Server 2008系统中IE8默认是启用IE ESC(ie 增强)的,这样会导致该IE不支持JS,开启方法: 1.开始->管理工具->服务器管理器 2.点击服务器管理- ...
- 记一次java程序内存溢出问题
一个自然语言处理程序,在封装为web-service后,部署到线上运行. 但最近出现了内存溢出的情况,频繁的out of memory. 先盲目尝试在启动脚本中增加-XX:-UseGCOverhead ...
- JVM(四) G1 收集器工作原理介绍
此篇文章半原创是对参考资料中的知识点进行总结,欢迎评论指点,谢谢! 部分知识点总结来自R大的帖子,下文有参考资料的链接 概述 G1 收集是相比于其他收集器(可见 上一篇文章),可以独立运 ...
- java爬虫之入门基础
相比于C#,java爬虫,python爬虫更为方便简要,首先呢,python的urllib2包提供了较为完整的访问网页文档的API,再者呢对于摘下来的文章,python的beautifulsoap提供 ...
- ubuntu 16.10安装nginx
1 : cd /usr/local 2 : sudo wget http://nginx.org/download/nginx-1.2.8.tar.gz 3 : sudo tar -zxvf ngin ...
- JavaScript 事件委托
JavaScript事件委托,或者叫事件代理,是利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件. 借花献佛的例子(取快递): 有三个同事预计会在周一收到快递.为签收快递,有两种办法 ...
- 【转】ArcGIS Server10.1安装常见问题及解决方案
转载自:http://www.higis.cn/Tech/tech/tId/85/ 最近因为更换系统的原因,重新安装了ArcGISServer 10.1.过程中遇到了几个小问题,虽然都一一解决了,但也 ...
- mongodb 3.4复制搭建
mongodb数据库主从复制的原理:在主从结构中,主节点的操作记录称为oplog(operation log).oplog存储在一个系统数据库local的集合oplog.$main中,这个集合的每个文 ...
- Excel 函数使用
字符串 20180613 转为日期 2018-06-13,单元格内输入如下公式 =DATE(LEFT(),MID(,),RIGHT()) IF 函数内的或.与 =IF(AND(A=B,C=D),&q ...
- 实验 MPLS LDP配置
实验 MPLS LDP配置 一.学习目的 掌握启用和关闭MPLS的方法 掌握启用和关闭MPLS LDP配置方法 掌握使用MPLS LDP配置LSP的方法 二.拓扑图 三.场景 你是公司的网管员,公司的 ...