码迷,mamicode.com
首页 > 其他好文 > 详细

sdnu 1513 字符串翻转

时间:2019-03-17 20:13:08      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:begin   sla   using   red   space   des   order   tip   translate   

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<<‘\n‘;
    }
    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<<‘\n‘;
                continue;
            }
        }
    }
    return 0;
}

  

sdnu 1513 字符串翻转

标签:begin   sla   using   red   space   des   order   tip   translate   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!