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++;}}
}
B. Partial Replacement相关推荐
- 【竞赛题解】Codeforces Round #710 (Div. 3)
B. Partial Replacement 题意:有字符串由.和*组成,可标记其中*,首尾的*必须被标记,使被标记的*之间距离不超过k,求最少的标记量 思路:首先从首尾出发确定首尾*的位置,再由首b ...
- arcgis出界址点成果表_界址点成果表打印
#coding: UTF-8 importarcpyimportosimporttypesimportstringimportshutilimportsysimportreimportopenpyxl ...
- Codeforces Round #710 (Div. 3)
文章目录 A. Strange Table 题意 解题思路 Code B. Partial Replacement 题意 解题思路 Code C. Double-ended Strings 题意 解题 ...
- Codeforces Round #710 (Div. 3) ABCDE 题解
我的博客园传送门,看的方便些 A. Strange Table 签到题,算出对应行列即可. view code #include<iostream> #include<string& ...
- Codeforeces #710 div3题解报告
A. Strange Table 题意:给你一个矩阵(n*m)的.这个矩阵从上到下由1,2,3````n * m填满. 再给你一个数x.问你在另一个矩阵(也是n * m,不过是从水平填起),这个x的位 ...
- Codeforces Round #710 (Div. 3)个人题解
Codeforces Round #710 (Div. 3) 文章目录 [Codeforces Round #710 (Div. 3)](https://codeforces.com/contest/ ...
- python 操作excel实现替换特定内容
本文介绍使用python语言,借助openyxl库来实现操作excel(xlsx)文件,实现替换特定内容的需求. 目前实现了3个小功能: 1. 全字匹配替换(mode1):(如:全字匹配 yocich ...
- 微软BI 之SSIS 系列 - Lookup 组件的使用与它的几种缓存模式 - Full Cache, Partial Cache, NO Cache...
开篇介绍 先简单的演示一下使用 Lookup 组件实现一个简单示例 - 从数据源表 A 中导出数据到目标数据表 B,如果 A 数据在 B 中不存在就插入新数据到B,如果存在就更新B 和 A 表数据保持 ...
- Partial Class部分类
Partial Class ,部分类 或者分布类.顾名思义,就是将一个类分成多个部分.比如说:一个类中有3个方法,在VS 2005将该类中3个方法分别存放在3个不同的.cs文件中. 这样做的好处: 1 ...
最新文章
- Mac的brew和brew cask区别以及安装brew cask
- 一个模型搞定图像标注、读图问答两件事,VQA准确率逼近人类水平 | Demo可玩...
- php如何返回数据请求数据格式化,PHP以json或xml数据格式返回请求数据的封装代码...
- Hadoop 核心编程之 HDFS 的文件操作
- 关于 SAP Spartacus 的 featureModules
- java将json转为hashmap_java – 将JSON字符串转换为HashMap
- 用Python给头像加上圣诞帽或圣诞老人小徽章
- javascript call apply
- 如何让盒子实现水平和垂直居中
- LeetCode 102. 二叉树的层次遍历(递归)(迭代)
- 解压rar文件压缩包时出现“An error occurred while loading the archive”的解决办法
- MarkDown编辑器中数学公式与符号-LaTeX 各种数学命令,符号
- 【LKJ】LKJ2000型记录装置显示界面说明
- 左耳朵耗子给出的学习指南
- ISTA与稀疏编码 both python matlab
- 【51单片机】基于51单片机的时钟电子锁设计
- 树莓派自带wifi工作不正常
- 网站三级域名是什么样?
- Chapter9.2:线性系统的状态空间分析与综合(上)
- P02014026黄一洋————信息论问题回答