Problem Statement
Given is a string SS consisting of
X
and.
.You can do the following operation on SS between 00 and KK times (inclusive).
- Replace a
.
with anX
.What is the maximum possible number of consecutive
X
s in SS after the operations?Constraints
- 1 \leq |S| \leq 2 \times 10^51≤∣S∣≤2×105
- Each character of SS is
X
or.
.- 0 \leq K \leq 2 \times 10^50≤K≤2×105
- KK is an integer.
Input
Input is given from Standard Input in the following format:
SS KKOutput
Print the answer.
Sample Input 1 Copy
Copy
XX...X.X.X. 2Sample Output 1 Copy
Copy
5After replacing the
X
s at the 77-th and 99-th positions withX
, we haveXX...XXXXX.
, which has five consecutiveX
s at 66-th through 1010-th positions.
We cannot have six or more consecutiveX
s, so the answer is 55.
Sample Input 2 Copy
Copy
XXXX 200000Sample Output 2 Copy
Copy
4It is allowed to do zero operations.
题目类型:单调队列;
解题目标:找到可能在k次内被补齐的最长序列长度
解题思路:
第一种:(思路简单,耗费空间)
1)用vector记录每个‘.’的下标;
2)遍历vector数组,比较下标相隔k的两个元素的之间差的最大值。
(左边界和右边界也要加入, 0, len+1, 因为最长的序列可能在两端)
第二种:(单调队列)
1)读入string,
2)遍历,遇到‘.’,计数, 当计到k时,比较当前右界与左界(左界所在的点是‘.’)的差,留取最大的;模拟了宽度为K的窗口滑动问题。
AC代码:
1)
#include <bits/stdc++.h>
#define rep(x, a, b) for(int x=a; x<=b; x++)
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int N= 3e5+10;
int main()
{
string s;
int k, cot = 0;
cin>>s>>k;
vector<int> v;
v.push_back(0);
int len = (int)s.length();
for(int i = 0; i<=len-1; i++)
if(s[i] == '.')
v.push_back(i+1);
v.push_back(len+1);
int len2 = (int)v.size();
int ans = 0;
if(k > len2-2)
{
cout<<len<<endl;
return 0;
}
else {
for(int i = 0; i<= len2-2-k ; i++)
{
//cout<<v[i+k+1]<<' '<<v[i]<<endl;
ans = max(v[i+k+1] - v[i], ans);
}
ans--;
cout<<ans<<endl;
}
return 0;
}
2)(更优)
#include <bits/stdc++.h>
#define rep(x, a, b) for(int x=a; x<=b; x++)
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int N= 3e5+10;
int main()
{
string s;
int k, cot = 0;
cin>>s>>k;
int ans = 0;
vector<int> v;
int d =0, l =0 ,r=0;
int len = (int)s.length();
for(int i =0; i<=len-1; i++)
{
if(s[i] == '.') d++;
if(d > k)
{
if(s[l] == '.')
d--;
l++;
}
ans = max(ans, i -l +1);
}
cout<<ans;
return 0;
}