zoukankan      html  css  js  c++  java
  • HDOJ 1022 模拟栈

    Train Problem I

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

    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<iostream>
     2 #include<stack>
     3 #include<vector>
     4 #include<string>
     5 using namespace std;
     6 int main()
     7 {
     8     stack<char>s;
     9     vector<string>steps;
    10     char in[11],out[11];
    11     int i,j,n;    
    12     memset(in,0,sizeof(in));
    13     memset(out,0,sizeof(out));
    14     while(cin>>n>>in>>out)
    15     {    
    16         i=j=0;
    17         while(i<n)
    18         {
    19             s.push(in[i++]);//进栈
    20             steps.push_back("in");
    21             while(!s.empty())//栈不为空
    22             {
    23                 if(s.top()==out[j]&&j<n)//栈顶元素和出栈序列元素相同
    24                 {
    25                     s.pop();//出栈
    26                     steps.push_back("out");
    27                     j++;
    28                 }
    29                 else
    30                     break;
    31             }
    32         }    
    33         if(s.empty())
    34         {
    35             cout<<"Yes."<<endl;
    36             for(i=0;i<steps.size();i++)
    37                 cout<<steps[i]<<endl;
    38             printf("FINISH
    ");
    39         }
    40         else
    41         {
    42             printf("No.
    ");
    43             printf("FINISH
    ");
    44         }
    45         steps.clear();
    46         for(i=s.size();i>0;i--)
    47             s.pop();
    48     }
    49     return 0;
    50 }
    View Code
    9860133 2013-12-19 16:27:02 Accepted 1022 0MS 316K 834 B C++ 泽泽
  • 相关阅读:
    SQL Server 执行参数化脚本时的一个性能问题
    2021 年终总结
    循序渐进——NAnt构建实例
    用C#实现单链表(创建单链表,在头部插入)
    用C#实现单链表(插入,在第i个前插,在第i个后插)
    用C#实现单链表(merge两个有序单链表)
    用C#实现单链表(取第i个结点元素,删除第i个结点)
    播放器03:以文件夹的形式添加整个文件夹里面的文件到播放列表,播放刚加进来的第一首歌曲,默认顺序播放
    用C#实现单链表(初始化数据,返回链表元素个数)
    ObjectiveC中创建单例方法的步骤
  • 原文地址:https://www.cnblogs.com/zeze/p/hdoj1022.html
Copyright © 2011-2022 走看看