C. Social Distance

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

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

For example, if n=8n=8 and k=2k=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=2k=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 ss that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string ss.

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=6n=6, k=1k=1, s=s= "100010", then the answer to the problem will be 11, since only the table at position 33 can be occupied such that the rules are still satisfied.

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases in the test. Then tt test cases follow.

Each test case starts with a line containing two integers nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105) — 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 ss of length nn 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 kk.

The sum of nn for all test cases in one test does not exceed 2⋅1052⋅105.

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 00.

Example

input

Copy

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

output

Copy

1
2
0
1
1
1

Note

The first test case is explained in the statement.

In the second test case, the answer is 22, since you can choose the first and the sixth table.

In the third test case, you cannot take any free table without violating the rules of the restaurant.

这个题目当时做的时候就总感觉差那么一点意思,就也一直没出,一周全在忙课程设计也就把补提给忘了,现在距离做这个题也好几天了,现在从新看这个题目就是 另一个感觉了

首先应该分情况讨论即可:
第一种情况就是当全部是0时:
我们先在第0个位置上安一个1,然后后面一直找000…01(k个0)这种子结构就可以了,所以答案为: 1 + (n-1)/(k+1) 找还能有多少个上述子结构。

另外当有1的时候又分三种情况
(1)我们先看第0个位置能不能安一个1,如果能,回到1情况,只不过这个时候的就是 1 + (还有多少区间长度)/(k+1)

(2)然后考虑俩个1之间的情况,求出俩个1之间的长度(不包含边界),那么由于右边界为1,那么我们还要让出来k个0出来,然后一样的操作 答案加上 (还有多少区间长度)/(k+1)

(3)然后再考虑最后一个1到字符串末尾的长度区间,答案加上(区间长度)/(k+1)
 

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        char A[200005];
        int n, k, total = 0;
        cin >> n >> k >> A;
        for (int i = 0; i < n; )
        {
            if (A[i] == '1')
            {
                i += (k + 1);//如果是1,直接跳过k个数字
            }
            else if (A[i] == '0')
            {
                int flag = 1;
                for (int j = i - k; j <= i + k; j++)//从i-k枚举到i+k
                {
                    if (j < 0)//index不能小于0
                        continue;
                    else if (j >= n)
                        break;
                    else if (A[j] == '1')
                    {
                        i = j;
                        flag = 0;
                        break;
                    }
                }
                if (flag == 0)
                    i += (k + 1);//跳
                else if (flag == 1)
                {
                    A[i] = '1';
                    total++;
                    i += (k + 1);//跳
                }
            }
        }
        cout << total << endl;
    }
    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值