火车问题 I (栈+字符匹配)

火车问题 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.

随着新学期的来临,伊格纳修斯火车站现在非常繁忙。很多学生想乘火车回学校(因为伊格纳修斯火车站的火车是世界上最快的。v ^)。但这里出现了一个问题,只有一条铁路,所有的火车都停在那里。所以所有的火车都是从一边进来,从另一边出来。对于这个问题,如果A列车先进入铁路,然后火车B在火车A开出前进入铁路,则A列车在B列车离开之前不能离开。下面的图片找出了问题所在。现在的问题是,车站最多有9列火车,所有的列车都有一个ID(编号为1到n),火车按O1顺序进入铁路,你的任务是确定列车是否可以按O2顺序开出。


  

Input

The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.

Output

The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.

输入

输入包含几个测试案例。每个测试案例由整数、列车数量和两个字符串组成,列车顺序为:O1,列车顺序为:O2。输入在文件末尾终止。示例输入中的更多详细信息。

输出

输出包含一个字符串"否",如果您无法将 O2 交换到 O1,或者您应该输出包含"是"的行,然后按您的方式输出订单(您应该输出"进入"进入铁路的列车,以及"输出"为火车离开铁路)。在每个测试案例之后打印一行包含"FINISH"。样本输出中的更多详细信息。

Sample Input

3 123 321
3 123 312

Sample Output

Yes.
in
in
in
out
out
out
FINISH
No.
FINISHz

遵从后进先出

AC

#include<stdio.h>
#include<stack>
#include<string.h>
using namespace std;
int main(void)
{
	int n,i,j,k,flag[50];
	char s1[15],s2[15];
	stack<char>s;
	while(~scanf("%d %s%s",&n,s1,s2))
	{
		while(!s.empty())//防止栈内含上次未出的栈存留 
			s.pop();
		memset(flag,-1,sizeof(flag));
		j=k=0;
		for(i=0;i<n;i++)
		{
			s.push(s1[i]);
			flag[k++]=1;
			while(!s.empty()&&s.top()==s2[j])
			{
				flag[k++]=0;
				s.pop();
				j++;//弹出次数,即s1 s2匹配了多少次 
			}
		}
		if(j==n)
		{
			printf("Yes.\n");
		
			for(i=0;i<k;i++)
			{
				if(flag[i]!=0)//flag==1时 进栈 
					printf("in\n");
				else//弹出 
				printf("out\n");
				
			}
		}
		else
			printf("No.\n");
		printf("FINISH\n");
	}
	return 0;
 } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值