HDU 1022 Train Problem I

这题我的想法使用栈的思想去解决的,就是根据进站的序列加入栈中,若和出站最开始的序号相同,则出栈;然后继续进行此操作,和下一个出站的火车序号相比较。这样操作完后看看栈是否为空,就可以判断了。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
int main()
{
    char s1[10],s2[10],s3[20][5];
    int n;
    while(scanf("%d%s%s",&n,s1,s2)!=EOF)
    {
        stack<char> s;
        int top2=0,top3=0;
        for(int i=0;i<n;i++)
        {
            s.push(s1[i]);
            strcpy(s3[top3++],"in");//此处就是我的思想
            while(!s.empty())
            {
                char a=s.top();
                if(a==s2[top2])
                {
                    strcpy(s3[top3++],"out");
                    s.pop();
                    top2++;
                }
                else
                    break;
            }
        }
        if(s.empty())
        {
            printf("Yes.\n");
            for(int i=0;i<top3;i++)
                printf("%s\n",s3[i]);
        }
        else
            printf("No.\n");
        printf("FINISH\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值