1. 程式人生 > 其它 >Text Reverse

Text Reverse

技術標籤:杭電OJ

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;
}