zoukankan      html  css  js  c++  java
  • 杭电acm 1022题

    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
     
    本题的大意是,有一个火车站,只有一个入口一个出口...随便输入一组进站火车编号(不超过10),一组出战火车编号。要求判断能否按照出站编号出站,如果可以接把顺序输出。如果
    不能输出No.。
    开始拿到本题,感觉无从下手,想了两人,最后写出一个程序能够通过给出的测试数据。但是WA...后来上网查找资料,大神们都说很简单,只是一个栈的简单应用,(因为没有学过C++)顿时感觉自己懂的东西太少了...查阅了相关资料...
    原来栈是定义在头文件stack里面的一个容器类型,想要定义一个栈一般有
    1 stack<char> s1;
    2 stack<int> s2;
    3 stack<string> s3;

    毋庸置疑,和我们定义一个整形很像,但是尖括号里面是栈类型的说明....栈的基本操作有

     1 入栈
     2 s.push(x);
     3 出栈
     4 s.pop();//这里括号里面没有元素,弹出的是栈顶的元素
     5 判断栈是否为空
     6 s.empty();//如果为空,返回true值
     7 栈顶的元素
     8 s.top();
     9 访问栈元素的个数
    10 s.size();
    11 等等.....
    View Code

    掌握了这些知识后,再根据网上大神写的代码,自己再敲了一个代码...贴出如下

     1 /********************************************
     2 杭电acm 1022 已AC
     3   *****************************************/
     4 #include "iostream"
     5 #include "string"
     6 #include "stack"
     7 using namespace std;
     8 #define Max 10
     9 int main(void)
    10 {
    11     int len;
    12     char inarr[Max],outarr[Max];
    13     int i=0,j=0;
    14     int flag[20]={0};
    15     while(scanf("%d %s %s",&len,inarr,outarr)!=EOF)
    16     {
    17         stack<char> temp;
    18         i=0;j=0;
    19         for(i;i<len;)
    20         {
    21             if(temp.empty())
    22             {
    23                 temp.push(inarr[i]);
    24                 flag[i+j]=1;
    25                 i++;
    26             }
    27             if(!temp.empty()&&temp.top()!=outarr[j])
    28             {
    29                 temp.push(inarr[i]);
    30                 flag[i+j]=1;
    31                 i++;
    32             }
    33             //这里要注意,不能使用if,开始使用if,一直不能AC,这里使用while是要将所有符合规则的
    34             //元素都弹出栈...
    35             while(!temp.empty()&&temp.top()==outarr[j])
    36             {
    37                 temp.pop();
    38                 flag[i+j]=0;
    39                 j++;
    40             }
    41         }
    42         if(temp.empty())
    43         {
    44             cout<<"Yes."<<endl;
    45             for(int m=0;m<2*len;m++)
    46             {
    47                 if(flag[m]==1)
    48                     cout<<"in"<<endl;
    49                 else cout<<"out"<<endl;
    50             }
    51             cout<<"FINISH"<<endl;
    52         }
    53         else cout<<"No."<<endl<<"FINISH"<<endl;
    54 
    55     }
    56     return 0;
    57 }

    特别要注意35行的代码...

    继续努力.....

  • 相关阅读:
    【题解】Red-Blue Graph Codeforces 1288F 上下界费用流
    【题解】The Magician HDU 6565 大模拟
    HAOI2018游记
    【题解】【THUSC 2016】成绩单 LOJ 2292 区间dp
    【题解】【雅礼集训 2017 Day5】远行 LOJ 6038 LCT
    【题解】Catering World Finals 2015 上下界费用流
    《无问西东...》
    为了世界的和平~一起上caioj~~~!
    新征程~起航!
    bzoj4240: 有趣的家庭菜园(树状数组+贪心思想)
  • 原文地址:https://www.cnblogs.com/kb342/p/3676321.html
Copyright © 2011-2022 走看看