[hdu 1062] Text Reverse | STL-stack

原题

题目大意:
t组数据,每组为一行,遇到空格时讲前面的单词反转输出。

题解:
显然的栈题,遇到空格时将当前栈输出清空即可

#include<cstdio>
#include<stack>
#include<cstring>
using namespace std;
int t,l;
char s[1010];
stack <char> stk;

int main()
{
    scanf("%d",&t);
    getchar();
    while (t--)
    {
        gets(s+1);
        l=strlen(s+1);
        for (int i=1;i<=l;i++)
        {
            if (s[i]==' ')
            {
                while (!stk.empty())
                {
                    putchar(stk.top());
                    stk.pop();
                }
                putchar(' ');
            }
            else stk.push(s[i]);
        }
        while (!stk.empty())
        {
            putchar(stk.top());
            stk.pop();
        }
        putchar('\n');
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/mrha/p/11955100.html