hdu 1022 Train Problem I

5 篇文章 0 订阅
2 篇文章 0 订阅

这道题比较简单,直接建立一个栈,模拟火车的进出过程就好,值得注意的一点是,栈顶的火车可以随时出栈,而不必等到所有火车都进完。

通过这个问题正好学习一下C++ STL中栈的使用:

使用STL中的栈要加入头文件

#include <stack>

using namespace std;

栈的定义是模板类 template <class T,class Container = deque<T>>

栈的操作有:

empty()   栈为空时返回真

pop()       移除栈顶元素,但不会返回栈顶元素的值

push()     在栈顶增加元素

size()       返回栈中元素的数目

top()         返回栈顶元素

#include <cstdio>
#include <stack>
#include <iostream>

using namespace std;

int main()
{
    int n;
    char q[1000],w[1000];
    while (scanf("%d",&n)!=EOF)
    {
        scanf("%s %s",q,w);
        stack <char> sta;
        int a[1000],la=-1,lw=0;
        for (int i=0;q[i]!='\0';i++)
        {
            sta.push(q[i]);
            a[++la]=1;
            while ((!sta.empty())&&(sta.top()==w[lw])&&(w[lw]!='\0')) { sta.pop(); a[++la]=0; lw++;}
        }
        if (w[lw]=='\0')
        {
            printf("Yes.\n");
            for (int i=0;i<=la;i++) if (a[i]) printf("in\n"); else printf("out\n");
        }
        else printf("No.\n");
        printf("FINISH\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值