hdu---1022---Train Problem I

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

Mean:

 有n辆火车,按一定的顺序进站(s1),问是否能按规定的顺序出站(s2),如果能输出每辆火车进出站的过程。

 

analyse:

///此题不真正的仔细读难理解出事栈的应用,容易想成反序相等就yes的问题,
///此题题意是说输入的两列字符串,第一列表示进入的顺序,第二列表示出去的顺序,问你是否符合后进先出。
///举出一列数据 7 1234567 4321576
///上面数据应该是 in in in in out out out out in out in in out out;

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;
typedef long long LL;

const int maxn=15;
const int INF=0x3f3f3f3f;

char s1[maxn], s2[maxn];
int Stack[maxn];
int vis[maxn<<1];///标记数组1表示进栈,0表示出栈
int n;

int main()
{
    while(~scanf("%d %s %s", &n, s1, s2))
    {
        int i, A, B, f=1;
        A=0;///s1的当前位置
        B=0;///s2的当前位置
        int top=0;///栈顶

        memset(vis, 0, sizeof(vis));
        while(B<n)
        {
            if(s2[B]==s1[A])
            {
                vis[i++]=1;///进栈
                vis[i++]=0;///出栈
                A++;
                B++;
            }

            else if(top && Stack[top]==s2[B])///如果栈非空,而且栈顶元素=str2当前元素则弹出栈
            {
                vis[i++]=0;
                top--;
                B++;
            }

            else if(A <= n)
            {
                Stack[++top]=s1[A++];///压入栈
                vis[i++]=1;
            }

            else
            {
                f=0;
                break;
            }
        }

        if(!f)
        {
            puts("No.");
            puts("FINISH");
        }

        else
        {
            puts("Yes.");

            for(int j=0; j<i; j++)
            {
                if(vis[j])
                    puts("in");
                else
                    puts("out");
            }
            puts("FINISH");
        }
    }
    return 0;
}

 

posted @ 2016-08-17 16:42  爱记录一切美好的微笑  阅读(141)  评论(0编辑  收藏  举报