Train Problem I HDU - 1022(栈 || 数组模拟)

地址:点击打开链接

As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2. 

题意: 给你一个 n : 火车辆数

            string a : 进入顺序

            string b : 出去顺序

         是否可以按a进入b出来 能输入案例 不能输出No.


这里给出2种方法: 一种用栈 模拟

                             一种用数组 模拟 


思路是一样的:   1. 先把 2个串给保存了

                          2.然后把串a进行一次遍历:

  每次进栈进行判断 

        如果此时满足一部分出栈顺序就出栈 

       并通过 d(result) 来记录进出情况 (0是出 1是进

                        

栈:

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define mst(a,b) memset(a,b,sizeof(a))

using namespace std;

typedef long long ll;
const double eps=1e-9;
const int M=1e4;
const int mod=1e7;


int main(){
    stack<char>s;
    int n,j,i,k,result[M];
    string a,b;

    while(cin>>n>>a>>b){
        k=0;i=0;j=0;
        s.push(a[0]);
        result[k++]=1;
        while(i<n&&j<n){
            if(s.size()&&s.top()==b[j]){
                j++;
                s.pop();
                result[k++]=0;
            }
            else{
                if(i==n) break;
                s.push(a[++i]);
                result[k++]=1;
            }
        }
        
        if(i==n)  puts("No.");
        else{
			puts("Yes.");
			for(i=0;i<2*n;i++){
				printf(result[i]==0?"out\n":"in\n");
			}
		}
        puts("FINISH");
    }
}

数组:

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define mst(a,b) memset(a,b,sizeof(a))

using namespace std;

typedef long long ll;
const double eps=1e-9;
const int M=1e4;
const int mod=1e7;

int main() {
	int n;
	while(cin>>n){
		int d[M];
		mst(d,0);
		string a,b,c;
		cin>>a>>b;
		int i,k,top,j;
		k=top=j=0;
		for(i=0;i<a.size();i++){
			c[top++]=a[i];
			d[k++]=1;
			while(top>0&&c[top-1]==b[j]){
				j++;
				top--;
				d[k++]=0;
			}
		}
		if(top<=0){
			puts("Yes.");
			for(i=0;i<2*n;i++){
				printf(d[i]==0?"out\n":"in\n");
			}
		}
		else{
			puts("No.");
		}
		puts("FINISH");
	}
}



   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值