String Reversion powered by wangbin
































1001. String Reversion

 












Total:

74

Accepted:

54


 

 

 

 








Time Limit: 1sec    Memory Limit:256MB

Description

Given a valid identifier in C programs, please write a program to reverse it by respectively reversing

each part separated by ‘’.

Input

The first line is an integer t, indicating the number of test cases.
Then there are t lines and each line contains a string. A string will contain no more than 100 characters and there is one or more ‘
’.

Output

For each test case, print out the string with each part reversed.

Sample Input

 Copy sample input to clipboard



3


nt45


_me


abc123_456


Sample Output



tn_54


_em


cba_321_654


 












Total:

74

Accepted:

54


 

 

 

以下为代码:

  1. // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
  2. // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
  3. // All Copyright reserved by Informatic Lab of Sun Yat-sen University
  4. int main(){
  5. int t;
  6. int i, start, end;
  7. string s;
  8. cin>>t;
  9. while(t--){
  10. cin>>s;
  11. start=0;
  12. end=s.find_first_of( '_', start );
  13. while ( end != string::npos ){
  14. for ( i = end - 1; i >= start; i-- )
  15. cout << s[ i ];
  16. cout <<'_';
  17. start = end + 1;
  18. end= s.find_first_of( '_', start );
  19. }
  20. for( i = s.size() - 1; i >= start; i-- )
  21. cout << s[i];
  22. cout<<endl;
  23. }
  24. return 0;
  25. }

发表评论

表情:
评论列表 (有 0 条评论,24人围观)

还没有评论,来说两句吧...

相关阅读

    相关 LeetCode:Reverse String

    最近新加的一道题,用C++秒过,12秒,反转字符串,思路很简单,一种暴力方法就用直接用一个新的字符串存储,然后一个一个从后往前遍历旧的字符串。第二种就是原地用2个指针从两端往中

    相关 Power Strings

    写在题目前:POJ太好用啦! 描述 给定两个字符串a和b,我们将\ b定义为它们的串联。例如,如果a =“abc”而b =“def”则a \ b =“abcdef”。如果我