Text Reverse

Text Reverse (1062)

原题链接

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K
(Java/Others) Total Submission(s): 60259 Accepted Submission(s):
23157

Problem Description

Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.

Output

For each test case, you should output the text which is processed.

Sample Input

3
olleh !dlrow
m’I morf .udh
I ekil .mca

Sample Output

hello world!
I’m from hdu.
I like acm.

Hint

Remember to use getchar() to read ‘\n’ after the interger T, then you may use gets() to read a line and process it.

题目大意:
题目描述

伊格纳修斯喜欢用相反的方式写词。 给定单行文字,由Ignatius撰写把每个单词都反转并输出它们

输入

输入包含多组测试样例。第一行为一个整数T,代表测试样例的数量,后面跟着T个测试样例。
每个测试样例占一行,包含多个单词。一行最多有1000个字符。

输出

对于每一个测试样例,你应该输出转换后的文本。

样例输入

3
olleh !dlrow
m’I morf .udh
I ekil .mca

样例输出

hello world!
I’m from hdu.
I like acm.

对于每行字符串处理的思路:

  • 建立一个栈
  • 遍历每一行的字符,若其不是空格,则将其入栈。若遍历到空格(或者遍历到最后一位字符),则打印栈顶字符并且使栈顶的字符出栈。当栈空时,还应输出一个空格。
  • 注意: 如果忽略了 i == len - 1的输出情况,则只会在遇到空格的时候输出字符,从而导致最后一个单词不会输出。若使用s += " " 手动在字符串后面加上空格也可以解决该问题。

AC代码1

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <stack>

using namespace std;

int main()
{
    int T;
    cin >> T;
    
    //输入T之后需按下回车,一定要getchar()吃回车
    getchar();
    
    while (T--)
    {
        //字符类型的栈
        stack<char> st;
        string s;
        getline(cin,s);
        
        s+=" ";//字符串末尾加上空格,可输出最后一个单词
        
        int len = (int)s.size();
        
        //遍历字符串
        for (int i = 0;i < len;i++)
        {
            if (s[i] == '\0') break;
            
            //非空格则入栈
            else if (s[i] != ' ') st.push(s[i]);
            
            //遇到空格输出栈中字符
            else if (s[i] == ' ')
            {
                //输出栈顶元素并删除栈顶元素
                while (!st.empty())
                {
                    cout << st.top();
                    st.pop();
                }
                //输出两个单词之间的空格
                if (i != len - 1) cout << ' ';
            }
        }
        cout << endl;
    }
    return 0;
}

AC代码2

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <stack>

using namespace std;

int main()
{
    int T;
    cin >> T;
    
    //吃回车
    getchar();
    
    while (T--)
    {
        stack<char> st;
        string s;
        getline(cin,s);
        
        int len = (int)s.size();
        
        //遍历字符串
        for (int i = 0;i < len;i++)
        {
            //非空格则入栈
            if (s[i] != ' ') st.push(s[i]);
            
            //遇到空格和遍历到最后一位,输出栈中字符
            if (s[i] == ' ' || i == len - 1)
            {
                //输出栈顶元素并删除栈顶元素
                while (!st.empty())
                {
                    cout << st.top();
                    st.pop();
                }
                //输出两个单词之间的空格
                if (i != len - 1) cout << ' ';
            }
        }
        cout << endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值