Smart Browser题解

Smart Browser

https://ac.nowcoder.com/acm/contest/7501/C

题目描述

来源:牛客网

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;
}

全部评论

相关推荐

zzzzhz:兄弟你先猛猛投简历至少三百家,能约到面试就去面。最近可以速成智能小车,智慧家居烂大街的项目,不需要自己写,只需要把里面的代码讲解看明白就行。把其中涉及到的八股文都拿出来单独背一下,我去年找工作就一个智能小车智慧家居找了10k差不多。
点赞 评论 收藏
分享
07-03 16:13
嘉应学院 Python
xiaolihuam...:很明显骗子,如果是hr直接约你面试了,哪用得着内推,如果是员工的话,你得多优秀,一线员工直接加你微信,
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务