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的值. 有多少种填法,使得数组每个位置相 ...
随机推荐
- 创建一个自定义的Application类
由于每个应用程序必须创建一个Application对象,vs为开发人员提供了模板来减轻开发人员的重复工作.当使用vs创建一个WPF应用程序是,vs会自动创建一个app.xaml文件, <Appl ...
- css三栏布局方案整理
日常开发中,经常会用到css三栏布局,现将工作中常用的css 三栏布局整理如下: 什么是三栏布局: 三栏布局,顾名思义就是两边固定,中间自适应. 一. float布局 <!DOCTYPE htm ...
- 转IE9以下$.ajax跨域返回“No transport”
$.ajax({ type: "POST", async: false, contentType: "application/x-www-form-urlencoded; ...
- idea快捷键、常用设置
代码提示: code completion :ALT + / 选用Eclipse的keymap. 设置代码助手快捷键(keymap-basic) 代码上下行复制(Ctrl + Alt + Down ...
- Nginx 504错误总结
Nginx 504错误(Gateway time-out 网关超时)的含义是所请求的网关没有请求到,简单来说就是没有请求到可以执行的PHP-CGI. 一般看来, 这种情况可能是由于nginx默认的f ...
- div的隐藏占用空间位置关系
display:none和visibility:hidden都是把网页上某个元素隐藏起来的功能,但两者有所区别,经过亲自实验,我发现使用 visibility:hidden属性会使对象不可见,但该对象 ...
- Memcache 学习笔记(二)---- PHP 脚本操作 Memcache 服务器
PHP 脚本操作 Memcache 服务器 一.PHP脚本操作Memcache方法 使用 PHP 脚本操作 Memcache,在 PHP 手册中有详细的介绍,我们可以实例化 Memcache 类,根 ...
- Java——实现对密码进行MD5加密
package common; /** *@author作者 E-Mail: *@version 创建时间:2015-9-24+下午01:22:44 *类说明 **/ import java.secu ...
- python SyntaxError: EOL while scanning string literal
错误原因是,字符串以 \ 结尾 或者字符串缺少引号. 写代码拼接windows 路径出现这个错误, 查资料才知道 python中字符串不能以 \ 结尾 我的代码如下 import os dirname ...
- idea 出现 java.noSuchMechodFound
公司 用了多个项目来相互之间形成依赖.每次修改或者添加新功能,会升级版本.用的是maven,这几天 一直 出现一个问题就是:本地 升级版本完后 使用 git命令 mvn -deploy -e 打包后, ...