zoukankan      html  css  js  c++  java
  • “玲珑杯”线上赛 Round #15 河南专场 F 咸鱼文章

    elttiL moT nwod eht teerts sllac ruo god ” ehT peek god ” . piZ si a peehs god . tuB nehw moT seirt ot yas ” peeS ” , ti semoc tuo ” peek ” . dnA ni a yaw moT si thgir . piZ si syawla gnignirb sgniht oh rof su ot peek ! ll’I llet uoy tuoba emos fo meht .
    s’piZ tsrif tneserp saw a eohs . tI saw edam fo neerg klis .
    eW t’ndid wonk woh piZ dnuof eht eohs . tuB retfa a tnemom yraM , ym gib retsis , dlot em eht eohs dah a egnarts llems . I deddon dna dleh ym eson . ” tahW od uoy kniht ti si ? ”
    ” tI sllems ekil gnihtemos rof gninaelc . I kniht enoemos deirt ot naelc a tops ffo eht eohs . nehT eh tup ti ta eht rood ot yrd . ”
    ” gnolA emac piZ . dnA eyb-doog eohs ! ” I dias . ” eW dluohs ekat ti kcab . ”
    ” eW t’nac ” . dias ym rettsis .
    ” ebyaM elttil moT si thgir , ” yraM dias . ” ebyaM piZ si a peek god ! “

    你正在做英语阅读,可哪知这是一篇咸鱼文章,整个文章的所有单词都是翻转的,你很慌。

    不过你是咸鱼程序员,你可以写代码将这篇文章翻转回来,那么翻转回来吧。

    INPUT
    输入一篇英文文章。

    输入数据中只包含空格、换行符和小写大写字母。

    满足总字数小于等于100000
    OUTPUT
    你应该把这个文章的所有单词都翻转回来,输出即可。
    SAMPLE INPUT
    AAA BBB
    AB AB
    SAMPLE OUTPUT
    AAA BBB
    BA BA
    sb题搞死我,气死我啦!!!
    刚开始在想空格与换行的区分(其实不用区分),想它们的码值。
    后来看来题解之后
    题解是这样的:F 咸鱼文章

    用栈来做这道题,遇到字母,我们就扔到栈里面去,遇到空格或者换行的时候,我们就把栈里面的元素输出,然后再输出空格/换行就好了。
    然后我就超时了,超时代码附上

    //超时代码,两层for,gg
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<queue>
    #include<stack>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int inf=0x3f3f3f3f;
    char str[10000][10000];
    int main()
    {
        int len=0;
        memset(str,0,sizeof(str));
        while(gets(str[len++]))
        {
            if(str[len-1][0]==0)
                break;
        }
        for(int i=0;i<len-1;i++)
        {
            stack<char>q;
            int p=strlen(str[i]);
            for(int j=0;j<=p;j++)
            {
                //printf("%d ",j);
                if((str[i][j]>='a'&&str[i][j]<='z')||(str[i][j]>='A'&&str[i][j]<='Z'))
                {
                    q.push(str[i][j]);
                }
                else
                {
                    while(!q.empty())
                    {
                        printf("%c",q.top());
                        q.pop();
                    }
                    printf(" ");
                }
            }
            printf("
    ");
        }
        return 0;
    }

    大概看了别人代码后,又想怎么结束输入(不用想,Ctrl+z,结束输入,水题刷的不够啊!)
    然后粘正确代码

    #include<cstdio>
    #include<cstring>
    #include<stack>
    #include<string>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    char a[100005];
    char b[100005];
    int main()
    {
        int perlen=0;
        while(gets(a))
        {
            int p=0;
            int len=strlen(a);//长度正好包含最后一个字母,没有'
    '
            for(int i=perlen; i<perlen+len; i++)
                b[i]=a[p++];
            b[perlen+len]='
    ';
            perlen=perlen+len+1;
        }
        stack<char>q;
        for(int i=0; i<perlen; i++)
        {
            if((b[i]>='a'&&b[i]<='z')||(b[i]>='A'&&b[i]<='Z'))
                q.push(b[i]);
            else
            {
                while(!q.empty())
                {
                    printf("%c",q.top());
                    q.pop();
                }
                printf("%c",b[i]);
            }
        }
        return 0;
    }
    

    1.不用考虑输入终止
    2.学习这种记录空格与换行
    3.strlen()的长度是到最后一个字母的长度,没有’ ’

  • 相关阅读:
    把Windows装进U盘
    Java 潜拷贝和深拷贝(浅复制和深复制)
    《远大前程——从软件新手到行业大牛》读书笔记
    解决windows8 metro应用不能联网
    [转载]schtasks命令使用实例介绍
    程序员网址
    VS2008,vs2010 快捷键大全 转载
    题解:2018级算法第一次练习赛 妙妙趣排序
    题解:2018级算法第一次上机 C1pair
    #编译原理# 词法分析(三)第二部分
  • 原文地址:https://www.cnblogs.com/zxy160/p/7215099.html
Copyright © 2011-2022 走看看