堆栈练习3:HDOJ1022

同样的跑火车问题,这次改变了循环停止的条件,使用了我之前就一直想用的2*N次循环,发现果然是可以的。

顺利的1A很开心。

利用栈模拟车站,利用队列记录进出站的顺序,代码会比较高效。

#include <iostream>
#include<stack>
#include<queue>
#include<string.h>
using namespace std;


int main()
{
    int N ;
    int pointin, pointout ;
    stack<char> s ;
    queue<int> q ;
    char in[11], out[11] ;
    while(cin >> N)
    {
        while(!s.empty())
            s.pop();
        while(!q.empty())
            q.pop();
        pointin = 0, pointout = 0 ;
        cin >> in >> out ;
        for(int i = 1 ; i<= 2*N ; i++)
        {
            if(!s.empty()&&s.top()==out[pointout])
            {
                pointout++ ;
                s.pop();
                q.push(0);
                continue ;
            }
            else if(pointin < N)
            {
                s.push(in[pointin]);
                pointin++;
                q.push(1);
                continue ;
            }
            else
                continue;
        }
        if(pointin==N&&pointout==N)
        {
            cout << "Yes." << endl ;
            while(!q.empty())
            {
                if(q.front()==1)
                {
                    cout << "in" << endl ;
                    q.pop();
                }
                else
                {
                    cout << "out" << endl ;
                    q.pop();
                }
            }
            cout << "FINISH" << endl ;
        }
        else
            cout << "No." << endl << "FINISH" << endl ;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值