题目描述

来源:牛客网

In some social platforms, there are some netizen who like to post "www". Since the string is like the grass, which contains plenty of "/" shapes, so there are also some netizen who post "grass".

As a fast, smart, and convenient browser, MI Browser can recognize this kind of text and show some special effects. Specifically, given a string, MI Browser will determine the number of "/" shapes made by character "w", where "w" itself and the gap between two adjacent "w"s both give one/\shape. If still not clear, please turn to sample input/output for more details.As a MI fan, you want to detect the relationship between the special effects and the number of "/" shapes, so the thing you should do first is to determine the number of "/" shapes by yourself.

输入描述:

Only one line containing a string s(1<|s| <=10^5).
It's guaranteed that s only contains lowercase letters.

输出描述:
Only one line containing one integer, denoting the answer.
示例1
输入
wwwvwwvw
输出

9

思路:

先统计出连续w的个数nu,可以发现其中/\的个数符合等差数列 2*nu-1,最后求和
最后优化一下循环 我觉得这个地方没有必要用一个临时变量来存i,不存反而跑的更快
if(s[i]=='w')
   {
    int nu=0;
    while(s[i]=='w')
       ++i,++nu;
    an+=(nu*2-1);
   }

AC代码

#include<bits/stdc++.h>
using namespace std;
char s[100005];
int main()
{
    scanf("%s",s);
    int an=0;
    int ls=strlen(s);
    for(int i=0;i<ls;++i)
    {
        if(s[i]=='w')
        {
            int nu=0;
            while(s[i]=='w')
                ++i,++nu;
            an+=(nu*2-1);
        }
    }
    printf("%d\n",an);
    return 0;
}