HDU 1022 Train Problem I 栈、队列

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1022

 

货车进出站,相当于STL里面的栈stack,知道了这一点,就很简单了。

stack函数:

1、push():进栈。

2、pop():出栈。

3、empty():栈是否为空。

4、top():栈顶。

 

代码如下:

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

/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/

char a[10],b[10];
char ans[100];

int main()
{
    int i,j,n;
    while(cin>>n)
    {//以b形式进站,以a形式出站
        for(i=1;i<=n;i++)
            cin>>b[i];
        for(i=1;i<=n;i++)
            cin>>a[i];
        int A=1,B=1,t=0,flag=0;
        stack<int>q;
        while(A<=n)
        {
            if(a[A]==b[B]&&B<=n)//B<=n一定要加,因为上一组可能对这一组有影响
            {//a与b相等,进去后马上出栈
                A++;B++;
                ans[t++]='i';
                ans[t++]='o';
            }
            else if(!q.empty()&&q.top()==a[A])
            {//栈顶与a相等,出栈
                A++;
                q.pop();
                ans[t++]='o';
            }
            else if(B<n)
            {//b进栈
                q.push(b[B]);
                B++;
                ans[t++]='i';
            }
            else
            {//不可能,标记break
                flag=1;
                break;
            }
        }
        if(flag==0)
        {
            printf("Yes.\n");
            for(i=0;i<t;i++)
            {
                if(ans[i]=='i')
                    printf("in\n");
                else
                    printf("out\n");
            }
        }
        else
            printf("No.\n");
        printf("FINISH\n");
    }
    return 520;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值