当前位置:网站首页>UPC-Longest X
UPC-Longest X
2022-08-03 05:14:00 【程序的搬运工Hunter】
UPC个人训练赛21
题目描述
Given is a string S consisting of X and ..
You can do the following operation on S between 0 and K times (inclusive).
Replace a . with an X.
What is the maximum possible number of consecutive Xs in S after the operations?
Constraints
1≤∣S∣≤2×105
Each character of S is X or ..
0≤K≤2×105
K is an integer.
输入
Input is given from Standard Input in the following format:
S
K
输出
Print the answer.
输入:
【样例1】 XX...X.X.X. 2 【样例2】 XXXX 200000输出:
【样例1】 5 【样例2】 4
题目大意:
给定一个由X和.组成的字符串s,可以进行K次操作将字符串中的.变为X,问最多可以有多少个连续的X
思路:
这个题目可以考虑使用一个指针,从当前的第i个往后指,知道指针超出字符串或者已经有k个点了,然后每次判断区间的最大,最终输出这个最大的长度。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
string s;
int k;
int cnt = 0;
int ans = 0;
int r = 0;
cin >>s >> k;
int len = s.size();
for(int i = 0;i < len;i++){
while(r < len&&s[r]== 'X'||(s[r] == '.' && cnt+1<=k)){
if(s[r] == '.')
cnt++;
r++;
}
ans = max(ans,r-i);
if(s[i] == '.')
cnt--;
}
cout << ans;
}
边栏推荐
猜你喜欢
随机推荐
《录取通知》 观后感
idea uses @Autowired annotation to explain the reasons and solutions
ss-5.consul服务端+生产者+消费者
一劳永逸解决vs编译器无法使用scanf函数
celery工作原理图
NotImplementedError: file structure not yet supported
vim命令
Modelarts第一次培训
7.17(7)
如何不耍流氓的做运维之-SHELL脚本
Go (一) 基础部分2 -- if条件判断,for循环语句
MySQL 索引检索原理和B+Tree数据结构详解
MySQL EXPLAIN 性能分析工具详解
玩转Markdown(2) —— 抽象语法树的提取与操纵
-整数求和-
JS学习笔记(三)
【XSS,文件上传,文件包含】
npm run dev/serve 时报错
flask 面试题 问题
浏览器中的 preview 和 response 的值不一致