当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
Redis常用命令
对角矩阵(diagonal matrix)
-一尺之棰-
Djiango第三次培训
0.ROS常用命令
【编程学习新起点】记录写博客的第一天
让小程序开发进入 `tailwind jit` 时代
OptionError: ‘Pattern matched multiple keys‘
令人愉快的 Nuxt3 教程 (一): 应用的创建与配置
Kaggle(四)Scikit-learn
7.16(6)
嵌入式-I2C-物理电路图
Makefile 遍历子目录模板
背压机制
跨域错误的原因及处理方法
用C语言来实现扫雷小游戏
ss-4.1-1个eurekaServer+1个providerPayment+1个consumerOrder
【数组排序】+日常
MySQL EXPLAIN 性能分析工具详解
MySQL 唯一索引 UNIQUE KEY 会导致死锁?









