2021-07-15 AcWing 3768. 字符串删减

输入样例1:

6
xxxiii

输出样例1:

1

输入样例2:

5
xxoxx

输出样例2:

0

输入样例3:

10
xxxxxxxxxx

输出样例3:

8

双指针

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
    int n;
    int res=0;
    string s;
    cin >> n >> s;
    for(int i=0; i < n; i++)
    {
        if(s[i]!='x') continue;
        int j=i+1;
        while(j<n && s[j]=='x'){
            j++;
        }
        res += max(0,j-i-2);
        i = j-1;
    }
    cout << res;
    return 0;
}

posted @ 2021-07-16 13:44  泥烟  阅读(26)  评论(0编辑  收藏  举报