Codeforces 1105B:Zuhair and Strings(字符串水题)

本文介绍了一种算法,用于在一个给定的字符串中查找连续出现k次的相同字符,且这些字符不能有重叠,旨在确定这种模式在字符串中最多出现的次数。通过实例解释了如何实现这一算法,并提供了一个简单的C++代码示例来解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Given a string s s s of length n n n and integer k   ( 1 ≤ k ≤ n ) k\ (1≤k≤n) k (1kn). The string s s s has a level x x x, if x x x is largest non-negative integer, such that it’s possible to find in s s s:

  • x x x non-intersecting (non-overlapping) substrings of length k k k,
  • all characters of these x 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 i i i and j   ( 1 ≤ i ≤ j ≤ n ) j\ (1≤i≤j≤n) j (1ijn), denoted as s [ i … j ] s[i…j] s[ij] = “ s i s i + 1 … s j s_is_{i+1}…s_j sisi+1sj”.

For example, if k = 2 k=2 k=2, then:

the string “aabb” has level 1 1 1 (you can select substring “aa”),
the strings “zzzz” and “zzbzz” has level 2 2 2 (you can select two non-intersecting substrings “zz” in each of them),
the strings “abed” and “aca” have level 0 0 0 (you can’t find at least one substring of the length k = 2 k=2 k=2 containing the only distinct character).
Zuhair gave you the integer k k k and the string s of length n n n. You need to find x x x, the level of the string s s s.

Input

The first line contains two integers n n n and k   ( 1 ≤ k ≤ n ≤ 2 ⋅ 1 0 5 ) k\ (1≤k≤n≤2⋅10^5) k (1kn2105) — the length of the string and the value of k k k.

The second line contains the string s s s of length n consisting only of lowercase Latin letters.

Output

Print a single integer x x x — the level of the string.

Examples

input
8 2
aaacaabb
output
2
input
2 1
ab
output
1
input
4 2
abab
output
0

Note

In the first example, we can select 2 2 2 non-intersecting substrings consisting of letter ‘a’: “(aa)ac(aa)bb”, so the level is 2 2 2.

In the second example, we can select either substring “a” or “b” to get the answer 1 1 1.

题意

给出一个长度为 n n n的字符串,在字符串中查找连续出现 k k k次的字母,中间不能有重叠,连续出现 k k k次的字母最多出现了几次

AC代码

暴力查找即可,注意 k = 1 k=1 k=1的情况

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#include <time.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
#define bug cout<<"-------------"<<endl
#define debug(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<"\n"
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
char ch[maxn];
int a[maxn];
bool cmp(int a,int b)
{
	return a>b;
}
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	int n,k;
	cin>>n>>k;
	cin>>ch;
	int res=1;
	for(int i=0;i<n;i++)
	{
		if(k==1)
			a[ch[i]-'a']++;
		else
		{
			if(ch[i]==ch[i+1])
				res++;
			else
				res=1;	
			if(res==k)
			{
				a[ch[i]-'a']++;
				res=1;
				i+=1;
			}
		}
		
	}
	sort(a,a+26,cmp);
	cout<<a[0]<<endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值