CF Round 650 --C. Social Distance

Polycarp and his friends want to visit a new restaurant. The restaurant has n tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from 1 to n in the order from left to right. The state of the restaurant is described by a string of length n which contains characters "1" (the table is occupied) and "0" (the table is empty).

Restaurant rules prohibit people to sit at a distance of k or less from each other. That is, if a person sits at the table number i, then all tables with numbers from i−k to i+k (except for the i-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than k.

For example, if n=8 and k=2, then:

  • strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant;
  • strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to k=2.

In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.

You are given a binary string s that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string s.

Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?

For example, if n=6, k=1, s= "100010", then the answer to the problem will be 1, since only the table at position 3 can be occupied such that the rules are still satisfied.

Input

The first line contains a single integer t (1≤t≤10^4) — the number of test cases in the test. Then t test cases follow.

Each test case starts with a line containing two integers n and k (1≤k≤n≤2⋅10^5) — the number of tables in the restaurant and the minimum allowed distance between two people.

The second line of each test case contains a binary string s of length n consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than k.

The sum of n for all test cases in one test does not exceed 2⋅10^5.

Output

For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output 0.

input

6
6 1
100010
6 2
000000
5 1
10101
3 1
001
2 2
00
1 1
0

output

1
2
0
1
1
1

题意:给定长度为n的字符串和整数k,字符串中1表示该位置有人坐,需要满足每个人左右距离k内没有人,初始字符串是已经满足条件的,问满足该条件下,最多还能添加多少人 。

解析:我们可以先考虑每一对相邻1, 区间内添加人,这样最后就只剩  00001......1000的状态,然后左右延申添加即可,这是基于存在1的情况,因此我们可以优先特判全0的情况即可。

#include 
using namespace std;
const int N=2e5+5;
char a[N];
int pos[N];
void solve()
{
    int n,k,cnt=0;
    scanf("%d%d%s",&n,&k,a+1);
    for(int i=1;i<=n;i++)
    {
        if(a[i]=='1') pos[++cnt]=i;//读出每一个1的位置
    }
    int ans=0;
    if(cnt==0)//全0的情况
    {
        int r=1;//直接从第一个位置放人
        ans++;
        while(r+k+1<=n) ans++,r=r+k+1;
        printf("%d\n",ans);
        return;
    }
    for(int i=1;i=1) ans++,l=l-k-1;
    while(r+k+1<=n) ans++,r=r+k+1;
    printf("%d\n",ans);
}
int main()
{
    int t=1;
    scanf("%d",&t);
    while(t--) solve();
    return 0;
}

你可能感兴趣的:(c语言,c++)