HDU 1022 火车进站【栈】

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

 

题目大意:

题目大概意思:有N辆火车,以序列1方式进站,判断是否能以序列2方式出栈。进站不一定是一次性进入,也就是说中途可以出站。

 

#include <cstdio>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int i, j, n;char a[10], b[10], c[30];
    while (scanf("%d", &n) != EOF)
    {
        getchar();
        stack<char>q;
        scanf("%s", a);
        scanf("%s", b);
        int k = 0,m=0;
        for (i = 0; i < n; i++)
        {
            q.push(a[i]);
            c[m++] = 1;
            while (q.top() == b[k])
            {
                k++;
                q.pop();
                c[m++] = 0;
                if (!q.size())break;         //也一定要加上这句,不然如果栈中无元素的话,q.top()的时候就会出错   ******
                /*if (k >= n)break; */           //不一定要加上这一句,因为栈空的时候,b[]数组一定没有越界,两种情况都符合
            }                          
        }
        if (!q.size())
        {
            printf("Yes.\n");
            for (i = 0; i < m; i++)
            {
                if (c[i] == 1)printf("in\n");
                else
                    printf("out\n");
            }
        }
        else printf("No.\n");
        printf("FINISH\n");
    }
    return 0;
}

 

 

2018-04-04

posted @ 2018-04-04 13:19  悠悠呦~  阅读(335)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end