博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1022 Train Problem I
阅读量:5064 次
发布时间:2019-06-12

本文共 4305 字,大约阅读时间需要 14 分钟。

Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 14413    Accepted Submission(s): 5310

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 out 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.".
 
该题就是模拟栈的操作,检验是否能按后面的那个栈出栈,能的话输出栈的操作,
我使用一个数组a[]来保存进栈和出栈的的过程(1表示进栈,2表示出栈)
结束条件,当第一个字符串全部进入栈中操作,又出现没有不相等时这种情况是不可满足的(no)
当这个栈指针 <= 0 题目给我的出栈顺序指针指向等该大小时 这种出栈顺序可以,并且这个顺序保存在了数组a中
 
1 #include 
2 #include
3 #include
4 using namespace std; 5 6 int a[100]; 7 8 bool judge_char(const char *p, const char *q) 9 {10 char top_ch[100];11 top_ch[0] = '\0';//栈12 int a_t = 0;13 int p_t = 0;14 int q_t = 0;15 int top_t = 0;16 int i;17 a[a_t] = 1;18 top_ch[top_t] = p[0];19 20 int flag = 0;21 while(1)22 {23 if(top_t == strlen(q))24 flag =1;25 if(q_t == strlen(p) && top_t <= 0)26 {27 return 1;28 break;29 }30 if(top_ch[top_t] == q[q_t])31 {32 a_t++;33 a[a_t] = 2;34 top_t--;35 q_t++;36 }37 if( top_ch[top_t] != q[q_t])38 {39 if(flag)40 {41 return 0;42 break;43 }44 a_t++;45 a[a_t] = 1;46 top_t++;47 p_t++;48 top_ch[top_t] = p[p_t];49 }50 }51 }52 53 int main()54 {55 int n;56 char ch[100];57 char ch1[100];58 while(scanf("%d%s%s",&n,ch,ch1) != EOF)59 {60 memset(a,0,sizeof(a));61 62 if(judge_char(ch,ch1) == 0)63 {64 printf("No.\n");65 printf("FINISH\n");66 continue;67 }68 else69 {70 printf("Yes.\n");71 int i;72 for(i = 0; ; i++)73 if(a[i])74 {75 if(a[i] == 1)76 printf("in\n");77 else78 printf("out\n");79 }80 else81 break;82 printf("FINISH\n");83 }84 }85 return 0;86 }

 

 

转载于:https://www.cnblogs.com/yyroom/archive/2013/04/22/3035595.html

你可能感兴趣的文章
Struts2 请求处理步骤
查看>>
win8.1安装密钥
查看>>
luogu5020 [NOIp2018]货币系统 (完全背包)
查看>>
hdu1007 Quoit Design
查看>>
0909编译原理
查看>>
Linux内核中的双向链表struct list_head
查看>>
alchemy-2.0 make出错
查看>>
样式加载顺序
查看>>
将 SharePoint 网站另存为模板
查看>>
H5 视频作为背景 source src改变后 循环播放的问题笔记
查看>>
git教程 入门
查看>>
erlang-jiffy 安装手记
查看>>
javascript实现页面滚屏效果
查看>>
[Linux]非常方便的上传下载文件工具rz和sz
查看>>
Locust性能测试_百度案例
查看>>
Struts2源码浅析-ResultType
查看>>
apply的理解和数组降维
查看>>
django-rest-framework
查看>>
git的安装和常用命令
查看>>
JS 定义变量
查看>>