哈理工OJ 2305 Reversed Word(文字翻转stack)

来源:互联网 发布:oracle认证 java 编辑:程序博客网 时间:2024/06/13 00:13

题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2305

Reversed Word
Time Limit: 1000 MS Memory Limit: 131072 K
Total Submit: 55(22 users) Total Accepted: 25(21 users) Rating: Special Judge: No
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
“尚学堂杯”哈尔滨理工大学第六届程序设计竞赛

【思路分析】用栈存一下就OK了,遇到空格的时候先把栈里的东西都输出,然后再输出空格。
【AC代码】

#include<cstdio>#include<cstring>#include<algorithm>#include<stack>using namespace std;char str[10005];int main(){    int t;    scanf("%d\n",&t);    while(t--)    {        stack<char>s;        gets(str);        int len=strlen(str);        for(int i=0; i<len; i++)        {            if(str[i]!=' ')            {                s.push(str[i]);            }            else            {                while(!s.empty())                {                    printf("%c",s.top());                    s.pop();                }                printf(" ");            }        }        while(!s.empty())        {            printf("%c",s.top());            s.pop();        }        printf("\n");    }    return 0;}
0 0
原创粉丝点击