hdu 1022 Train Problem I

来源:互联网 发布:网络黄牛是什么意思 编辑:程序博客网 时间:2024/06/05 17:43

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

通过这个问题正好学习一下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;}


0 0
原创粉丝点击