#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
int main(int argc, char** argv)
{
int n, k;
cin >> n >> k;
string line;
cin >> line;
vector<int> robot, package;
for(int i=0; i<line.size(); i++){
if(line[i]=='P') robot.push_back(i);
if(line[i]=='H') package.push_back(i);
}
int ans=0, idxR=0, idxM=0;
while(idxR < robot.size() && idxM < package.size()){
if(abs(robot[idxR]-package[idxM]) <= k){
idxR++; idxM++;
ans++;
}
else if(robot[idxR] > package[idxM]) idxM++;
else if(package[idxM] > robot[idxR]) idxR++;
}
cout << ans << endl;
return 0;
}
Leave a comment