HDU 1020 Train Problem I 栈和队列的应用

/*
----------------------------------------
    stratege :stack && queue && simulation
    Theam : 给你一系列数,一次进栈,问出栈的
        顺序是否符合
    Author: Johnsondu
    Time: 2012-04-04 
    type: data structure

3 123 --- 213  ---》 Yes. in, in, out, out, in, out
-----------------------------------------    
*/


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
using namespace std ;


char in[1000], out[1000] ;
int n ;
char res[1000] ;
bool flag ;

int main ()
{
    int i, j ;
    int cnt, len ;
    char ch, tmp ;

    queue <string> op ;
    stack <char> Q ;

    while (scanf ("%d %s %s", &n, in, out) != EOF)
    {
        //cout << in << endl ;
        //cout << out << endl ;
        flag = true ;
        while (!op.empty())           
            op.pop () ;
        while (!Q.empty ())
            Q.pop () ;

        cnt = 0 ;
        len = strlen (in) ;
        Q.push (in[0]) ;
        op.push ("in") ;

        for (i = 1; i < len; i ++)
        {
            ch = Q.top () ;
            if (Q.empty ())       //火车进栈,如果栈空,直接入栈
            {
                Q.push (in[i]) ;
                op.push ("in") ;
                continue ;
            }
            if (ch == out[cnt])    //如果栈顶元素和结果一致
            {
                Q.pop () ;
                op.push ("out") ;   // 出栈
                cnt ++ ;            // out 的元素下标
                if (!Q.empty ())    //在此情况下,判断接下去的栈顶元素是否与当前out的值相同
                {                   
                    tmp = Q.top () ;
                    while (tmp == out[cnt])  //相同则继续出栈
                    {
                        Q.pop () ;
                        cnt ++ ;
                        op.push ("out") ;
                        if (!Q.empty ())
                            tmp = Q.top () ;
                        else
                            break ;
                    }
                }
                Q.push (in[i]) ;   //将当前值压入栈
                op.push ("in") ;
            }
            else
            {
                Q.push (in[i]) ;
                op.push ("in") ;
            }
        }
        while (!Q.empty ())
        {
            ch = Q.top () ;
            if (ch != out[cnt])
            {
                flag = false ;
                break ;
            }
            else
            {
                Q.pop () ;
                op.push ("out") ;
                cnt ++ ;
            }
        }

        if (flag)
        {
            printf ("Yes.\n") ;
            while (!op.empty())
            {
                cout << op.front() << endl ;
                op.pop () ;
            }
        }
        else
            printf ("No.\n") ;
        printf ("FINISH\n") ;
    }
    return 0 ;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值