sdnu 1513 字符串翻转

Posted guanwen769aaaa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

  

以上是关于sdnu 1513 字符串翻转的主要内容,如果未能解决你的问题,请参考以下文章

华为OD机试真题Python实现翻转单词顺序真题+解题思路+代码(2022&2023)

SDNU 1410.FJ的字符串

SDNU 1093.DNA排序(水题)

2022&2023华为OD机试 - 单词反转 2(Python)

翻转数组

SDNU 1206.蚂蚁感冒 代码如此简单,思维练习7月29