HDU 1062.Text Reverse "stack or array or string stream" "Character processing" "August 30"

Source: Internet
Author: User
Tags sin

Text Reverse

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

Inputthe input contains several test cases. The first line of the input was a single integer T which is the number of test cases. T test Cases follow.
Each test case contains a, with several words. There'll is at the most of characters in a line.

Outputfor Each test case, you should the output of the text which is processed.

Sample Input
3olleh!dlrowm ' I Morf. Udhi 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 the If use gets () to read a line and process it.
This problem can be used as an array or as a stack, and it can be done with a C string stream. Note that there may be more than 1 spaces in the middle. Keng! Pay attention to the special. Use the stack code as follows:

#include <cstdio> #include <stack> #include <cstring>using namespace Std;int main () {    int t;    scanf ("%d", &t);    GetChar ();//absorbs carriage return    while (t--) {        char x;        stack<char>f;        while (1) {            X=getchar ();            if (x!= ' &&x!= ' \ n ' &&x!=eof) F.push (x);            else{                while (!f.empty ()) {                    printf ("%c", F.top ());                    F.pop ();                }                if (x== ' \ n ' | | x==eof) break;                printf ("");            }        }        printf ("\ n");    }    return 0;}

Use the C string stream code as follows:

#include <cstdio> #include <sstream> #include <iostream>using namespace Std;int main () {    int T;    string S,ss;    scanf ("%d", &t);    GetChar ();    while (t--) {        getline (cin,s);        int x=0;        while (s[x]== ') {//may have a space at            the beginning of printf ("");            x + +;        }        Istringstream sin (s);        while (SIN>>SS) {            x+=ss.length ();            for (int i=ss.length () -1;i>-1;i--)                printf ("%c", Ss[i]);            while (X<s.length () &&s[x]== ") {///middle space may not be a                printf (" ");                x + +;            }        }        printf ("\ n");    }    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1062.Text Reverse "stack or array or string stream" "Character processing" "August 30"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.