#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int n;
scanf("%d", &n);
string str;
cin >> str;
bool flag = false;
int res = 0;
for (int i = 0; i < n;)
{
if(str[i] != 'x')
{
i ++ ;
continue;
}
int l = i;
while (str[i] == 'x' && i < n) i ++ ;
if(i - l >= 3) res += i - l - 2;
}
cout << res << endl;
return 0;
}
Leetcode每日一题:减小和重新排列数组后的最大值
方法一:排序+贪心
class Solution {
public:
int maximumElementAfterDecrementingAndRearranging(vector<int>& f) {
sort(f.begin(), f.end());
int n = f.size();
f[0] = 1;
for (int i = 1; i < n; i ++ )
{
if(f[i] - f[i - 1] > 1)
f[i] = f[i - 1] + 1;
}
return f[n - 1];
}
};
方法二:哈希
class Solution {
public:
int maximumElementAfterDecrementingAndRearranging(vector<int>& f) {
int n = f.size();
vector<int> cnt(n + 1);
for (auto &t : f)
++ cnt[min(t, n)];
int miss = 0;
for (int i = 1; i <= n; i ++ )
{
if(cnt[i] == 0) miss ++ ;
else
{
miss -= min(cnt[i] - 1, miss);
}
}
return n - miss;
}
};