当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
pta a.1003 的收获
C语言简单实现扫雷小游戏
Length n of condensed distance matrix ‘y‘ must be a binomial coefficient
HarmonyOS应用开发第一次培训
ModelArts第二次培训
【反弹shell与提权】
Navicat 解决隔一段时间不操作出现延时卡顿问题
令人愉快的 Nuxt3 教程 (一): 应用的创建与配置
斐讯K2路由编译Padavan华硕固件和心得
-寻找鞍点-
celery工作原理图
Modelarts第一次培训
动态调整web系统主题? 看这一篇就够了
飞机大战完整版
背压机制
Pr第四次培训笔记
vivado遇到的问题
曲线特征----曲线弯曲程度的探究
-完全数-
陆运信息系统——班列项目总结(一)








