zoukankan      html  css  js  c++  java
  • 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A

    Polycarp has interviewed Oleg and has written the interview down without punctuation marks and spaces to save time. Thus, the interview is now a string s consisting of n lowercase English letters.

    There is a filler word ogo in Oleg's speech. All words that can be obtained from ogo by adding go several times to the end of it are also considered to be fillers. For example, the words ogo, ogogo, ogogogo are fillers, but the words go, og, ogog, ogogog and oggo are not fillers.

    The fillers have maximal size, for example, for ogogoo speech we can't consider ogo a filler and goo as a normal phrase. We should consider ogogo as a filler here.

    To print the interview, Polycarp has to replace each of the fillers with three asterisks. Note that a filler word is replaced with exactly three asterisks regardless of its length.

    Polycarp has dealt with this problem in no time. Can you do the same? The clock is ticking!

    Input

    The first line contains a positive integer n (1 ≤ n ≤ 100) — the length of the interview.

    The second line contains the string s of length n, consisting of lowercase English letters.

    Output

    Print the interview text after the replacement of each of the fillers with "***". It is allowed for the substring "***" to have several consecutive occurences.

    Examples
    input
    7
    aogogob
    output
    a***b
    input
    13
    ogogmgogogogo
    output
    ***gmg***
    input
    9
    ogoogoogo
    output
    *********
    Note

    The first sample contains one filler word ogogo, so the interview for printing is "a***b".

    The second sample contains two fillers ogo and ogogogo. Thus, the interview is transformed to "***gmg***".

    就是将ogo、ogogo、ogogogo……输出为***。

    这道题我卡了好久,写得很繁杂,最后的代码如下:

    #include<cstdio>
    #include<cstring>
    #define ll long long
    using  namespace std;
    int d;
    char s[1000];
    int main(){
        scanf("%d",&d);
        scanf("%s",s);
        int f=0,b=0,e=0,i;
        for(i=0;i<=strlen(s);){
            if(f==0){
                if(s[i]=='o'&&s[i+1]=='g'&&s[i+2]=='o'){
                    printf("%.*s",i-e,s+e);
                    f=1;
                    b=i;
                    i+=3;
                    e=i;
                }else i++;
            }else if(f==1){
                if(s[i]=='g'&&s[i+1]=='o'){
                    i+=2;
                    e=i;
                }else {
                    f=0;
                    printf("***");
                }
            }
        }
        printf("%.*s",i-e,s+e);
        return 0;
    }

    然后结束后参考了一下别人的代码,重写了一份代码,特别简洁清楚:

    #include<cstdio>
    char s[101];
    int main(){
        scanf("%*d%s",s);
        for(int i=0;s[i];){
            if(s[i]=='o'&&s[i+1]=='g'&&s[i+2]=='o'){
                i++;
                while(s[i]=='g'&&s[i+1]=='o')i+=2;
                printf("***");
            }else
                putchar(s[i++]);
        }
        return 0;
    }

    所以说,我真是菜啊。

  • 相关阅读:
    服务器上传大小限制 --- 来自 FastAdmin 项目开发的引发的问题 (TODO)
    英语中的各种“破坏”
    PADS Layout CAM 的中高级焊盘选项
    FastAdmin 推荐 Git 在线学习教程
    Vue 将样式绑定到一个对象让模板更清晰
    jQuery动态的给页面中添加一条样式表的链接
    页面根据不同的情况添加不同的样式表
    jQuery屏蔽浏览器的滚动事件,定义自己的滚轮事件
    如何判断自己的浏览器支持的是javascript的那个版本
    ie下 iframe在页面中显示白色背景 如何去掉的问题
  • 原文地址:https://www.cnblogs.com/flipped/p/6083484.html
Copyright © 2011-2022 走看看