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++ 泽泽
  • 相关阅读:
    ZT Android Debuggerd的分析及使用方法
    使用信号进行同步 sem_post
    linux c编程调用系统的动态库时,要使用dlopen等函数吗?
    Android系统的智能指针(轻量级指针、强指针和弱指针)的实现原理分析
    linux的pthread_self与gettid的返回值和开销的区别
    转贴:参禅与悟道》——浅谈人生
    my target
    转贴:如何学好C++语言.docx
    [EffectiveC++]item24:若所有参数皆需类型转换,请为此采用non-member函数
    [EffectiveC++]item23:Prefer non-member non-friend functions to member functions
  • 原文地址:https://www.cnblogs.com/zeze/p/hdoj1022.html
Copyright © 2011-2022 走看看