B. Zuhair and Strings
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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.
Output
Print a single integer xx — the level of the string.
Examples
input
Copy
8 2 aaacaabb
output
Copy
2
input
Copy
2 1 ab
output
Copy
1
input
Copy
4 2 abab
output
Copy
0
Note
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的子串.
大致思路:我们从第一个字符串开始,设置步长为k,统计i ~i+k内有多少个符合条件的字符串,并用一个数组记录一下,最后遍历整个数组取最大值.
代码:
#include <bits/stdc++.h>
using namespace std;
string ss;
int n,k;
map<char,int> mp;
int main(int argc, char const *argv[])
{
cin >> n >> k;
cin >> ss;
int ans = 0;
for(int i = 0; i < n;){
int j = i;
while(j < min(n,i + k) && ss[j] == ss[i]) j++;
if(j >= i + k){
mp[ss[i]]++;
}
i = j;
}
map<char,int> :: iterator ite;
for(ite = mp.begin(); ite != mp.end(); ite++){
ans = max(ite->second,ans);
}
cout << ans << endl;
return 0;
}