您的位置:首页 > 其它

HDU 1062 Text Reverse

2015-01-20 17:08 204 查看
题目链接: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
3
olleh !dlrow
m'I morf .udh
I 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: