杭电 hdu 1062 Text Reverse

来源:互联网 发布:淘宝权重怎么提升 编辑:程序博客网 时间:2024/06/04 01:25

Text Reverse

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


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.
Hint
Remember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.


str.find(' ’,n)   //从n开始寻找出现' '字符的位置。

str.find_find_last_of(' ')   //寻找最后出现‘ ’字符的位置。

reverse(str.begin()+n,str.begin()+str.find(' ',n));   //string中从n这个位置(str.begin()+n),到n位置后面 首次出现' '字符的位置(str.begin()+str.find(' ',n))的这一段字符串中的所有字符逆转,也就是反序排列。


#include <iostream>  #include <string>  using namespace std;  int main()  {      string str;      int c,i,n;      cin>>c;      getchar();      while(c--)      {          getline(cin,str);          n = 0;          while(str.find(' ',n) != std::string::npos)          {              reverse(str.begin()+n,str.begin()+str.find(' ',n));              n = str.find(' ',n) +1;              while(str[n] == ' ')                  n++;          }          reverse(str.begin() + str.find_last_of(' ') + 1,str.end());          cout<<str<<endl;      }      return 0;  }  



0 0