Train Problem I

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

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
题解:火车进站,类似于栈;;;
代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stack>
  4. using namespace std;
  5. char in[],out[],m[][];
  6. int t;
  7. void display(char *s){
  8. strcpy(m[t++],s);
  9. }
  10. int main(){
  11. int n;
  12. while(~scanf("%d",&n)){memset(m,,sizeof(m));
  13. memset(in,,sizeof(in));
  14. memset(out,,sizeof(out));
  15. t=;scanf("%s%s",in,out);
  16. stack<char>train;
  17. for(int j=,i=;i<n;){
  18. if(!train.empty()&&train.top()==out[i])display("out"),i++,train.pop();
  19. else if(in[j])train.push(in[j++]),display("in");
  20. else break;
  21. }//printf("%d\n",train.size());
  22. //while(!train.empty())printf("%c",train.top()),train.pop();
  23. display("FINISH");
  24. if(train.empty()){puts("Yes.");
  25. for(int i=;i<t;++i)printf("%s\n",m[i]);}
  26. else puts("No."),puts("FINISH");
  27. }
  28. return ;
  29. }

另外,自己写了几个关于栈的括号配对问题,贴下:

代码:

  1. #include<stdio.h>
  2. char m[];
  3. int top;
  4. bool pop(){
  5. top--;
  6. if(top<)return false;
  7. else return true;
  8. }
  9. void push(char s){
  10. top++;
  11. m[top]=s;
  12. }
  13. int main(){
  14. char x[];
  15. int T;
  16. scanf("%d",&T);
  17. while(T--){top=;
  18. scanf("%s",x);
  19. for(int i=;x[i];i++){
  20. if(x[i]=='('||x[i]=='[')push(x[i]);
  21. else if(x[i]==')'&&m[top]=='('||x[i]==']'&&m[top]=='['){if(!pop())break;}
  22. else push(x[i]);
  23. }//printf("%d",top);
  24. //while(top)printf("%c",m[top--]);
  25. if(top==)puts("Yes");
  26. else puts("No");
  27. }
  28. return ;
  29. }
  1. #include<stdio.h>
  2. #include<stack>
  3. using namespace std;
  4. char s[];
  5. int main(){
  6. int T,temp;
  7. scanf("%d",&T);
  8. while(T--){temp=;
  9. stack<char>m;
  10. scanf("%s",s);
  11. for(int i=;s[i];i++){if(m.empty()&&(s[i]==')'||s[i]==']')){
  12. temp=;
  13. puts("No");
  14. break;
  15. }
  16. if(s[i]=='('||s[i]=='[')m.push(s[i]);
  17. else if(s[i]==')'&&m.top()=='('||s[i]==']'&&m.top()=='[')m.pop();
  18. else m.push(s[i]);
  19. }
  20. if(m.empty()&&temp)puts("Yes");
  21. else if(temp)puts("No");
  22. }
  23. return ;}

Train Problem I(栈)的更多相关文章

  1. train problem I (栈水题)

    杭电1002http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/ ...

  2. Hdu 1022 Train Problem I 栈

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. hdu Train Problem I(栈的简单应用)

    Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...

  4. Train Problem(栈的应用)

    Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of studen ...

  5. HDU1022 Train Problem I 栈的模拟

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 栈的模拟,题目大意是已知元素次序, 判断出栈次序是否合理. 需要考虑到各种情况, 分类处理. 常 ...

  6. Train Problem I--hdu1022(栈)

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. HDU 1022.Train Problem I【栈的应用】【8月19】

    Train Problem I Problem Description As the new term comes, the Ignatius Train Station is very busy n ...

  8. HDU - 1022 Train Problem I STL 压栈

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. hdu 1022 Train Problem I(栈的应用+STL)

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. 判断一个key 是否在map中存在

    public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-gener ...

  2. web前端之 CSS引入第三方插件

    引入第三方图标插件 - fontawesome 官网地址:http://fontawesome.io/ 1.下载图标插件包 下载地址:https://codeload.github.com/FortA ...

  3. hdu 5063 Operation the Sequence(Bestcoder Round #13)

    Operation the Sequence                                                                     Time Limi ...

  4. 7. 稀疏表示之OMP,SOMP算法及openCV实现

    一.前言 稀疏表示是自上世纪90年代开始,从人眼的视觉感受野获得启示,逐渐被人们所研究.现在已经发展为一种重要的信息表示方法.所谓稀疏表示是指,一个信号在过完备字典中,可以由少数个原子线性表达, 其数 ...

  5. python re 正则

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  6. ASP.NET中多个相同name的控件在后台正确取值

    有兽,   页面上可能有多个相同name的Html表单控件,   一般在后台使用Request.Form[“name”]取值,并用‘,’分隔.   但是当值中包含逗号时,   取值就会出现异常,   ...

  7. 如何更改 Mac OS X 系统默认用户名

    说到 Mac 用户名估计有许多人都不知道在哪个地方修改,其实说简单也简单说麻烦也麻烦看你自己的需求.好比如果你只要用户名的登录更改,那是就非常简单的事了.下面这里就给大家介绍mac osx系统如何更改 ...

  8. 创建对象时引用的关键字,assign,copy,retain

    创建对象时引用的关键字:assign: 简单赋值,不更改索引计数(强引用)copy: 建立一个索引计数为1的对象,然后释放旧对象retain:释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的索 ...

  9. 读jquery.cookie.js源码学到的几个技巧

    一.兼容AMD.CommonJS和普通JS的写法 (function (factory) { if (typeof define === 'function' && define.am ...

  10. JQuery中阻止事件冒泡的两种方式及其区别

    JQuery 提供了两种方式来阻止事件冒泡. 方式一:event.stopPropagation(); $("#div1").mousedown(function(event){ ...