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 (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 的子串的起始点,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 }

原文地址:https://www.cnblogs.com/ymzjj/p/10300631.html

时间: 2024-07-30 07:20:25

Codeforces Round #533 (Div. 2) B. Zuhair and Strings 【模拟】的相关文章

Codeforces Round #417 (Div. 2) A. Sagheer and Crossroads 模拟 枚举

Codeforces Round #417 (Div. 2) A. Sagheer and Crossroads 模拟  枚举 题意 一个红绿灯 按逆时针方向一次给出各个路口的左转,直行,右转,以及行人车道让你判断,汽车是否有可能撞到行人 注意 当前车道的左转有可能撞到别的车道的行人的 题解 一大堆特判 1 #include <cstdio> 2 #include <cmath> 3 #include <cstdlib> 4 #include <cstring&g

水题 Codeforces Round #302 (Div. 2) A Set of Strings

题目传送门 1 /* 2 题意:一个字符串分割成k段,每段开头字母不相同 3 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <string> 9 #include <algorithm> 10 using namespace std; 11 12 con

Codeforces Round #533 (Div. 2)

A. Salem and Sticks 由于长度很小,所以直接暴力枚举最后的长度即可,取最小值即可. #include<bits/stdc++.h> #define CLR(a,b) memset(a,b,sizeof(a)); using namespace std; typedef long long ll; const int inf=0x3f3f3f3f; const int maxn=10010; int a[1100],n; int cost,ans; int main(){ ci

Codeforces Round #533 (Div. 2) Solution

A. Salem and Sticks 签. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define N 1010 5 int n, a[N]; 6 7 int work(int x) 8 { 9 int res = 0; 10 for (int i = 1; i <= n; ++i) 11 res += max(0, abs(x - a[i]) - 1); 12 return res; 13 } 14 15 int m

Codeforces Round #533 (Div. 2) ABCD 题解

题目链接 A. Salem and Sticks 分析 暴力就行,题目给的n<=1000,ai<=100,暴力枚举t,t从2枚举到98,复杂度是1e5,完全可行. 代码 1 #include <cstdio> 2 #include <cmath> 3 #include <iostream> 4 #include <cstring> 5 #include <algorithm> 6 #include <vector> 7 #

Codeforces Round #533 (Div. 2) D. Kilani and the Game(BFS)

题目链接:https://codeforces.com/contest/1105/problem/D 题意:p 个人在 n * m 的地图上扩展自己的城堡范围,每次最多走 a_i 步(曼哈顿距离),按 1 ~ p 的顺序,问最后每个人占领的点的数量. 题解:用一个队列维护当前起点,用另一个队列模拟当前起点走 a_i 步可以到达的全部点.(60 ~ 63行关键代码,多源bfs,不可分开跑) 数据: 4 3 2 2 1 1.. 1.. ..2 ... output:10 2 1 #include <

Codeforces Round #533 (Div. 2)C. Ayoub and Lost Array

C. Ayoub and Lost Array Ayoub had an array ?? of integers of size ?? and this array had two interesting properties: All the integers in the array were between ?? and ?? (inclusive). The sum of all the elements was divisible by 3. Unfortunately, Ayoub

Codeforces Round #533 (Div. 2)E. Helping Hiasat_求最大独立子集转换求最大团

题目链接:E. Helping Hiasat 题解:把夹在同一段1里面的人互相连边,然后问题就转换为求最大独立子集的大小,最大独立子集大小等于补图的最大团.最大图不会的可以看这篇博客,我也是看这个的 #include<bits/stdc++.h> #include<iostream> #include<string> #include<cstring> #include<algorithm> #define pb push_back #defin

Codeforces Round #256 (Div. 2) B. Suffix Structures(模拟)

题目链接:http://codeforces.com/contest/448/problem/B ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943/ma