zoukankan      html  css  js  c++  java
  • 字符串-07. 说反话-加强版 (20)

    给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。

    输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过500 000的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用若干个空格分开。

    输出格式:每个测试用例的输出占一行,输出倒序后的句子,并且保证单词间只有1个空格。

    输入样例:

    Hello World   Here I Come
    

    输出样例:

    Come I Here World Hello
    

     关键在于50000个字符,不能用字符数组

    #include <iostream>
    #include <stack>
    #include <string>
    using namespace std;
    int main()
    {
        stack<string> s;
        string  sp;
        bool flag=false;
        while(cin>>sp)
            s.push(sp);
        
        while(!s.empty())
        {
            if(flag)
                cout<<" ";
            else
                flag=true;
            cout<<s.top();
            s.pop();
        
        }
        return 0;
    }
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <iostream>
     4 #include <string.h>
     5 #include <string>
     6 #include <math.h>
     7 
     8 
     9 using namespace::std; 
    10 
    11 int main(){
    12   
    13    
    14     char s[500001];
    15    
    16     gets(s);
    17     int start=0;
    18     while(s[start] == ' ')
    19         ++start;
    20     /* 当输入为空格时,结束程序 */
    21     if(s[start] == '')
    22         return 0;
    23    //从后向前检测单词
    24    int i=strlen(s)-1,count=0;
    25    char *p;
    26    while(i>=0)
    27    {
    28        if(s[i]==' ')
    29        {
    30            if(s[i+1]!=' '&&s[i+1]!='')
    31            {
    32                p=s+i+1;
    33                
    34                count++;
    35                if(count>1)printf(" ");
    36                printf("%s",p);
    37            }
    38            s[i]='';
    39        }else if(i==0&&s[i]!=' ')
    40        {
    41            p=s;
    42            if(count>=1)printf(" ");
    43            printf("%s",p);
    44        }
    45        i--;
    46    } 
    47    
    48       return 0;
    49 }
  • 相关阅读:
    C#中的委托和事件(续)
    C# 中的委托和事件
    玩转JS插件系列
    gusfield
    线性规划
    后缀自动机
    bitset
    二维树状数组
    线段树合并
    求凸包
  • 原文地址:https://www.cnblogs.com/ligen/p/4286196.html
Copyright © 2011-2022 走看看