sdnu 1513 字符串翻转

1513.K.Reversed Words

Time Limit: 2000 MS    Memory Limit: 32768 KB
Total Submission(s): 56    Accepted Submission(s): 31

Description

Some aliens are learning English. They have a very strange way in writing that they revered every word in the sentence but keep all the words in common order. For example when they want to write “one two three”, they will write down “eno owt eerht”.

 

Now we’ve got some sentence written by these aliens, translate them! And maybe we will know some of their secrets!

Input

 Multiple test cases. The first line contains a positive integer T (T <= 1000), indicating the number of test cases.

 

For each test cases, there will be one line contains only lower case letters and spaces. The length of each line will be no more than 10000. Test cases which are longer than 5000 will be less than 50. Continuous letters are seen as a word, words are separated by spaces. There won’t be two adjacent spaces in the input. Space won’t be the first or the last character.

Output

 One line per case, the translated sentence.

Sample Input

2
eno owt eerht
abcde

Sample Output

one two three
edcba
两种思路QAQ
第一种 借助stringstream
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    string s,sss;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        getline(cin,s);
        stringstream ss(s);
        while(ss>>s)
        {
            reverse(s.begin(),s.end());
            cout<<s;
            if(!ss.eof())
            {
                cout<<' ';
            }
        }
        cout<<'
';
    }
    return 0;
}

  第二种 模拟

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    string s;
    getchar();
    while(t--)
    {
        getline(cin,s);
        for(int i=0,tmp=0;i<=s.size();i++)
        {
            if(s[i]==' ')
            {
                for(int j=i-1;j>=tmp;j--)
                {
                    cout<<s[j];
                }
                cout<<s[i];
                tmp=i+1;
                continue;
            }
            else if(i==s.size())
            {
                for(int j=i-1;j>=tmp;j--)
                {
                    cout<<s[j];
                }
                cout<<'
';
                continue;
            }
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/guanwen769aaaa/p/10548300.html