B. Zuhair and Stringstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven 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.InputThe 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.OutputPrint a single integer xx — the level of the string.ExamplesinputCopy8 2
aaacaabb
outputCopy2
inputCopy2 1
ab
outputCopy1
inputCopy4 2
abab
outputCopy0
NoteIn 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。
题解:按照题目的内容进行推理,注意题目中说明“ZZZZ” 4 2 是 2 而不是1,一定要注意。
题意 :第一个数字是字符串长度,第二个数字是找出相同的字母的子串的长度,找出这样子串的最大数量。
推理过程:ASCAll码的数量是有限的,也就是说我们可以统计每一个字符形成子串的个数,然后进行最大值查找。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <stack>
using namespace std;
const int MAXN = 1e6;
char tab[MAXN];
int num[400];
int n,t;
int main()
{
memset(num,0,sizeof(num));
scanf("%d %d",&n,&t);
getchar();
for(int i=0;i<n;i++){
scanf("%c",&tab[i]);
}
char temp;
int flag = 0;
for(int i=0;i<n;i++){
if(!i){
temp = tab[i];
flag++;
}
else{
if(tab[i]==temp){
flag++;
}
else{
temp = tab[i];
flag = 1;
}
}
if(flag==t){
num[tab[i]]++;
flag = 0;
}
}
int mmax = 0;
for(int i=0;i<380;i++){
if(num[i]>mmax){
mmax =num[i];
}
}
printf("%d\n",mmax);
return 0;
}