HDU 1062 Text Reverse

来源:互联网 发布:小学奥数 知乎 编辑:程序博客网 时间:2024/04/27 18:33

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062

Text Reverse

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18063    Accepted Submission(s): 6845


Problem Description
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
 

Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
 

Output
For each test case, you should output the text which is processed.
 

Sample Input
3olleh !dlrowm'I morf .udhI ekil .mca
 

Sample Output
hello world!I'm from hdu.I like acm.
给出一行字符串,这一行的字符串的每个单词是反了的,每个单词之间是以空格隔开的,但是具体多少空格就不知道了

原字符串不动,然后检测出每个单词,分别倒置,水题吧。

下面是AC代码。

#include<iostream>#include<algorithm>#include<sstream>#include<stack>#include<vector>using namespace std;void rev(string &str,int s,int e){    while(s<=e) swap(str[s++],str[e--]);}int main(){    int t;    string s,x;    cin>>t;    getline(cin,s);//吸收回车符    while(t--)    {        bool flag=0;        getline(cin,s);        int start;        for(int i=0;i<s.size();i++)        {            if((i==0&&s[i]!=' ')||(s[i]!=' '&&s[i-1]==' ')) start=i;            else if(s[i]!=' '&&s[i+1]==' ') rev(s,start,i);        }        if(s[s.size()-1]!=' ') rev(s,start,s.size()-1);        cout<<s<<endl;    }    return 0;}


0 0
原创粉丝点击