HDOJ1022(栈)

来源:互联网 发布:域名备案有什么用 编辑:程序博客网 时间:2024/06/05 05:24
Problem Description
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 Bleaves.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.
 

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.
 

Sample Input
3 123 3213 123 312
 

Sample Output
Yes.inininoutoutoutFINISHNo.FINISH
 
 
 
 
#include<stdio.h>#include<string.h>int main(){int a[20],n,i,j,k,t;//a[20]是用来记录车是进站还是出站char str1[15],str2[15];//记录进站和出栈站while (scanf ("%d",&n)!=EOF){scanf ("%s %s",str1,str2);char stack[1000];//进行模拟栈的进出i=j=k=0;t=-1;while (j<n+1&&i<n){if (stack[t]==str2[i]&&t!=-1)//看栈顶和出栈是否匹配,匹配则下一次是出站
{t--;//出站
a[k]=0;//记录该车是出站
k++;i++;//车号进位}else//若匹配{t++;//进站stack[t]=str1[j];//将进来的那一辆作为下一次的栈顶k++;//记录进站
j++;//车辆继续进站
}}if (k!=n*2)printf ("No.\n");//进栈的车辆与出栈的数目不同else{printf ("Yes.\n");for (i=0;i<n*2;i++){if (a[i])printf ("in\n");elseprintf ("out\n");}}printf ("FINISH\n");}return 0;}

 
 /*题意,输入n代表车的数量,后面两行,分别代表进站的车和出站的车,进站要满足先进后出的原则,然后判断输入胡进站顺序和出站顺序是否匹配*/
 
 
 
 
0 0
原创粉丝点击