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的值. 有多少种填法,使得数组每个位置相 ... 
随机推荐
- 开启停止wifi热点bat脚本
			@echo offcolor 2title 启停无线WIFI echo 启动WIFI=======>按1键 echo ... 
- PHP学习5——异常处理
			主要内容: PHP错误类型 异常的产生 错误日志 日志信息记录到操作系统日志 异常处理 扩展异常处理类 PHP错误类型 语法错误 执行时错误 逻辑错误 异常的产生 如果安装了xampp之后,在php. ... 
- golang学习之生成代码文档
			go doc 工具会从 Go 程序和包文件中提取顶级声明的首行注释以及每个对象的相关注释,并生成相关文档. 一般用法: go doc package 获取包的文档注释,例如:go doc fmt 会显 ... 
- 第八章.Java集合
			Java集合类是一种特别有用的工具类,可用于存储数量不等的对象.Java集合大致可分为Set.List.Queue和Map四种体系 Set代表无序.不可重复的集合 List代表有序.重复的集合 Map ... 
- 域名解析成功后,怎样访问服务器上Eclipse中的Web工程
			右击工程选择Export-->选择Web文件夹下的WAR file-->Destination下选择文件存放的地址-->Finish就可以获得WAR文件了然后将WAR文件放到tomc ... 
- Recursive sum in parent-child hierarchy T-SQL
			---树形(父子关系类)分级类统计(父子统计) --涂聚文 2014-08-14 drop table BookKindList create table BookKindList ( BookKin ... 
- linux  cut: invalid byte, character or field list Try 'cut --help' for more information.
			1. 概述 centos执行简单shell 脚本 报错 cut: invalid byte, character or field listTry 'cut --help' for more info ... 
- Python基础-I/O模型
			一.I/O模型 IO在计算机中指Input/Output,也就是输入和输出.由于程序和运行时数据是在内存中驻留,由CPU这个超快的计算核心来执行,涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接 ... 
- 浅谈用于WEBGIS开发最重要的4个HTML5特性
			WebGIS是GIS与Internet相结合的产物,一般Internet的开发手段都可用于WEBGIS的开发,比较流行的有Javascript.FLash,到现在应该说市面上的WEBGIS产品和具有的 ... 
- Goclipse的Eclipse插件包安装升级地址
			http://goclipse.github.io/releases/ Eclipse Software Site for Goclipse This URL is an Eclipse softwa ... 
