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++;}}
}

B. Partial Replacement相关推荐

  1. 【竞赛题解】Codeforces Round #710 (Div. 3)

    B. Partial Replacement 题意:有字符串由.和*组成,可标记其中*,首尾的*必须被标记,使被标记的*之间距离不超过k,求最少的标记量 思路:首先从首尾出发确定首尾*的位置,再由首b ...

  2. arcgis出界址点成果表_界址点成果表打印

    #coding: UTF-8 importarcpyimportosimporttypesimportstringimportshutilimportsysimportreimportopenpyxl ...

  3. Codeforces Round #710 (Div. 3)

    文章目录 A. Strange Table 题意 解题思路 Code B. Partial Replacement 题意 解题思路 Code C. Double-ended Strings 题意 解题 ...

  4. Codeforces Round #710 (Div. 3) ABCDE 题解

    我的博客园传送门,看的方便些 A. Strange Table 签到题,算出对应行列即可. view code #include<iostream> #include<string& ...

  5. Codeforeces #710 div3题解报告

    A. Strange Table 题意:给你一个矩阵(n*m)的.这个矩阵从上到下由1,2,3````n * m填满. 再给你一个数x.问你在另一个矩阵(也是n * m,不过是从水平填起),这个x的位 ...

  6. Codeforces Round #710 (Div. 3)个人题解

    Codeforces Round #710 (Div. 3) 文章目录 [Codeforces Round #710 (Div. 3)](https://codeforces.com/contest/ ...

  7. python 操作excel实现替换特定内容

    本文介绍使用python语言,借助openyxl库来实现操作excel(xlsx)文件,实现替换特定内容的需求. 目前实现了3个小功能: 1. 全字匹配替换(mode1):(如:全字匹配 yocich ...

  8. 微软BI 之SSIS 系列 - Lookup 组件的使用与它的几种缓存模式 - Full Cache, Partial Cache, NO Cache...

    开篇介绍 先简单的演示一下使用 Lookup 组件实现一个简单示例 - 从数据源表 A 中导出数据到目标数据表 B,如果 A 数据在 B 中不存在就插入新数据到B,如果存在就更新B 和 A 表数据保持 ...

  9. Partial Class部分类

    Partial Class ,部分类 或者分布类.顾名思义,就是将一个类分成多个部分.比如说:一个类中有3个方法,在VS 2005将该类中3个方法分别存放在3个不同的.cs文件中. 这样做的好处: 1 ...

最新文章

  1. Mac的brew和brew cask区别以及安装brew cask
  2. 一个模型搞定图像标注、读图问答两件事,VQA准确率逼近人类水平 | Demo可玩...
  3. php如何返回数据请求数据格式化,PHP以json或xml数据格式返回请求数据的封装代码...
  4. Hadoop 核心编程之 HDFS 的文件操作
  5. 关于 SAP Spartacus 的 featureModules
  6. java将json转为hashmap_java – 将JSON字符串转换为HashMap
  7. 用Python给头像加上圣诞帽或圣诞老人小徽章
  8. javascript call apply
  9. 如何让盒子实现水平和垂直居中
  10. LeetCode 102. 二叉树的层次遍历(递归)(迭代)
  11. 解压rar文件压缩包时出现“An error occurred while loading the archive”的解决办法
  12. MarkDown编辑器中数学公式与符号-LaTeX 各种数学命令,符号
  13. 【LKJ】LKJ2000型记录装置显示界面说明
  14. 左耳朵耗子给出的学习指南
  15. ISTA与稀疏编码 both python matlab
  16. 【51单片机】基于51单片机的时钟电子锁设计
  17. 树莓派自带wifi工作不正常
  18. 网站三级域名是什么样?
  19. Chapter9.2:线性系统的状态空间分析与综合(上)
  20. P02014026黄一洋————信息论问题回答

热门文章

  1. HA架构中的脑裂,什么是脑裂,怎样预防脑裂?
  2. 上传音乐到Android模拟器的SD卡,并在Android模拟器上播放
  3. 机器学习服务助应用内文本语种在线和离线检测
  4. ZCU104开发板MIPI摄像头开发(详细介绍移植ZCU102example design)
  5. Tensorboard可视化
  6. 怎样去掉QMainWindow最大化和最小化按钮
  7. 操作系统 实验三:线程的互斥
  8. 零零散散之Java异常
  9. SpringBoot企业级开发
  10. 【 javascript】JS语法 ES6、ES7、ES8、ES9、ES10、ES11、ES12新特性