Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25817    Accepted Submission(s): 9752

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 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. 

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 321
3 123 312
 
Sample Output
Yes.
in
in
in
out
out o
ut
FINISH
No.
FINISH
Hint

Hint

For the first Sample Input, we let train 1 get in, then train 2 and train 3. So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1. In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3. Now we can let train 3 leave. But after that we can't let train 1 leave before train 2, because train 2 is at the top of the railway at the moment. So we output "No.".

这个题意说简单形象点就是火车进站,只有一条路,先进站的后出站,输入的前面字符串时进站的顺序,后面是出站的顺序,问是否可按后面顺序出来!

显然这是一个典型的栈的问题!

 #include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
char a[],b[];
int c[];
int main()
{
int n,i,j,k;
stack<char>s;//初始化一个栈
while(scanf("%d",&n)!=EOF)
{
getchar();
scanf("%s%s",a,b);
memset(c,-,sizeof(c));
while(!s.empty())
s.pop();
i=j=k=;
while(i<n)
{
s.push(a[i++]);
c[j++]=;
while(!s.empty()&&s.top()==b[k])//为什么要判断是否为空栈?这个是为了跳出while循环,如果是空栈了。就没栈顶了,没比较的意义了!
{
k++;
c[j++]=;
s.pop();//消栈
}
}
if(!s.empty())
{
printf("No.\n");//如果栈不为空,就是没消完
printf("FINISH\n"); }
else
{
printf("Yes.\n");
for(i=;i<j;i++)
{
if(c[i]==)
printf("in\n");
else if(c[i]==)
printf("out\n"); }
printf("FINISH\n");
}
}
return ;
}

最新文章

  1. DOM对象 与 jQuery对象 之间的相互装换
  2. android修改系统时区
  3. 深入理解javascript中执行环境(作用域)与作用域链
  4. Codeforces Round #320 (Div. 2) D. &quot;Or&quot; Game 数学
  5. status pending状态
  6. iOS - 文件操作(File Operating)
  7. leetcode学习笔记--开篇
  8. 006.Compile方法
  9. jrae源码解析(一)
  10. C++静态成员函数和静态成员变量的探索
  11. PHP - 接口 - 多接口
  12. Silverlight之 xaml布局
  13. JavaScript Function arguments.callee caller length return
  14. 【翻译】LPeg编程指南
  15. 《Python网络编程》学习笔记--使用谷歌地理编码API获取一个JSON文档
  16. 微信小程序 发现之旅(一)—— 项目搭建与页面跳转
  17. Java第四次实践作业
  18. Go断言
  19. 团队协作统一vue代码风格,vscode做vue项目时的一些配置
  20. object-c中疑问

热门文章

  1. python之3内置容器
  2. 【3】python核心编程 第六章-序列:字符串、列表和元组
  3. T-SQL 语法元素
  4. javascript之对象
  5. j2ee概览
  6. bzoj1675 [Usaco2005 Feb]Rigging the Bovine Election 竞选划区
  7. 那些年的那些事CISC和RISC发展中的纠缠
  8. 【LeetCode练习题】Combination Sum
  9. IOS开发之---触摸和手势
  10. HINTERNET 句柄