思想:
i 作为某一连续的 "xxx" 区间的右端点,慢指针 j 作为该连续的 "xxx" 区间的左端点;s,当 s[i] == 'x' 时,将 j = i 标记为左端点,i 不断向后推进: s[i] == 'x' 且 i - j == 2 时说明存在了连续的 "xxx",需要删除,删除后 i 和 j 都要向后移动。"xxx",跳出循环。res 记录删去的 'x' 的数量。代码:
#include <bits/stdc++.h>
using namespace std;
void solve(){
int n; string s;
cin >> n >> s;
int res = 0;
for(int i = 0; i < s.size(); i ++){
if(s[i] == 'x'){
int j = i;
while(i < s.size() && s[i] == 'x'){ //指针 j i
if(i - j == 2) res ++, j ++; //下标 2 3 4
i ++; //s[i] x x x
} // i - j = 2 说明存在连续的 "xxx"
}
}
cout << res << endl;
}
int main(){
solve();
return 0;
}