【简单栈应用】 HRBUST 2305 Reversed Word

Reversed Word

Time Limit: 1000 MS Memory Limit: 131072 K

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), indicates 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

Source

“尚学堂杯”哈尔滨理工大学第六届程序设计竞赛

题意

给你一串字符串,让你对于字符串当中的每一个单词进行反转操作。

思路

拿数组模拟或者栈模拟整个操作就好,可以作为新手联系栈时候的入门题型。

AC代码

我是拿栈写的

#include<bits/stdc++.h>
using namespace std;

void solve(void)
{
    int n;
    cin>>n;
    getchar();
    while(n--)
    {
        string a;
        getline(cin,a);
        stack<char>p;
        int k = a.size();
        for(int i = 0 ; i < k ; i++){
            if(a[i]!=' '){
                p.push(a[i]);
            }
            else{
                while(!p.empty()){
                    cout<<p.top();
                    p.pop();
                }
                cout<<' ';///把每个单词输出之后要输出一个空格。
            }
        }
        while(!p.empty()){
            cout<<p.top();
            p.pop();
        }
        cout<<endl;
    }
}


int main(void)
{
    solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

两米长弦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值