Codeforces Round #533 (Div. 2) B. Zuhair and Strings 【模拟】

传送门:http://codeforces.com/contest/1105/problem/B

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 (1kn1≤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:

  • xnon-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 (1ijn1≤i≤j≤n), denoted as s[ij]s[i…j]= "sisi+1sjsisi+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 (1kn21051≤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 的子串的起始点,O(N)遍历一遍整个字符,记录每种字母满足条件的子串所对应的数量。

 

AC code:

 1 #include <bits/stdc++.h>
 2 #define INF 0x3f3f3f3f
 3 #define LL long long
 4 using namespace std;
 5 const int MAXN = 2e5+10;
 6 char str[MAXN];
 7 int num[30];
 8 int main()
 9 {
10     int N, K;
11     scanf("%d %d", &N, &K);
12     scanf("%s", str);
13     int len = strlen(str);
14     int st = 0;
15     bool flag;
16     while(st+K-1 < len){
17         flag = true;
18         for(int i = st+1; i <= st+K-1; i++){
19             //printf("%c %c\n", str[i], str[i-1]);
20             if(str[i] != str[i-1]){
21                 flag = false;
22                 st = i;
23                 break;
24             }
25         }
26         if(flag){
27             //printf("%d %c\n",st, str[st]);
28             num[str[st]-'a']++;
29             st = st+K;
30         }
31         //st+=K;
32     }
33     int ans = 0;
34     for(int i = 0; i < 26; i++){
35         //printf("%d %d\n", i, num[i]);
36         ans = max(num[i], ans);
37     }
38 
39     printf("%d\n", ans);
40     return 0;
41 
42 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值