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;
}


 

你可能感兴趣的:(HDU 1022 Train Problem I 栈、队列)