B. Partial Replacement

B. Partial Replacement

题面:

You are given a number k and a string s of length n, consisting of the characters ‘.’ and ‘’. You want to replace some of the '’ characters with ‘x’ characters so that the following conditions are met:

The first character ‘’ in the original string should be replaced with ‘x’;
The last character '
’ in the original string should be replaced with ‘x’;
The distance between two neighboring replaced characters ‘x’ must not exceed k (more formally, if you replaced characters at positions i and j (i<j) and at positions [i+1,j−1] there is no “x” symbol, then j−i must be no more than k).
For example, if n=7, s=..* and k=3, then the following strings will satisfy the conditions above:

.xx.xx;
.x
.xx;
.xx.xxx.
But, for example, the following strings will not meet the conditions:
.**.xx (the first character '’ should be replaced with ‘x’);
.x
.xx* (the last character ‘’ should be replaced with ‘x’);
.x
.xx (the distance between characters at positions 2 and 6 is greater than k=3).
Given n, k, and s, find the minimum number of '
’ characters that must be replaced with ‘x’ in order to meet the above conditions.

Input

The first line contains one integer t (1≤t≤500). Then t test cases follow.

The first line of each test case contains two integers n and k (1≤k≤n≤50).

The second line of each test case contains a string s of length n, consisting of the characters ‘.’ and ‘*’.

It is guaranteed that there is at least one ‘*’ in the string s.

It is guaranteed that the distance between any two neighboring ‘*’ characters does not exceed k.

Output

For each test case output the minimum number of ‘*’ characters that must be replaced with ‘x’ characters in order to satisfy the conditions above.

题解:

本体首先可以先找出"*“的个数如果其个数小于等于0或1就输出0或1,当其个数大于1的时候可以记录下第一个” * ",并且记录其位置,在让其变成x,再从最后找出第一个” * “,也让其变成x,再从记录第一个“ * ”点后面开始寻找x,如果是“ * ”则更新记录的位置如果为x,则输出ans+2,下面上代码。

代码:

#include<iostream>
using namespace std;


int main()
{
    int t;
    cin>>t;
    
    while(t--)
    {
        string s;
        int n,ans=0,cut=0,m,l;
        int p=-1;
        
        cin>>n>>m>>s;
        
        for(int i=0;i<n;i++)
        {
            if(s[i]=='*')cut++;
        }
        
        if(cut==0||cut==1)
        {
            cout<<(cut==0?"0":"1")<<endl;
            continue;
        }
        
        for(int i=0;i<n;i++)
        if(s[i]=='*')
        {
            s[i]='x';
            p=i;
            break;
        }
        
        for(int i=n-1;i>=0;i--)
        if(s[i]=='*')
        {
            s[i]='x';
            break;
        }
        
        int b=p;
        l=p;
        int f=0;
        for(int i=p;i<n;i++)
        {
            for(int j=i+1;j<=i+m;j++)
            {
                if(s[j]=='*')
                b=j;
                else if(s[j]=='x')
                {
                    cout<<ans+2<<endl;
                    f=1;
                    break;
                }
            }
            if(f==1) break;
            i=b-1;
            ans++;
        }
        
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值