zoukankan      html  css  js  c++  java
  • HDU 1022 Train Problem I stack 基础题

    题意:

    有一个火车站,只有一个口同时用于进入和出去,但不能有2辆或以上同时进出。

    给出一个n代表有n辆火车(n<=9),编号分别为1到n。

    然后给出2个序列,分别代表火车进站的顺序,火车出站的顺序。

    问按照这个进站的顺序,能不能按照这个出站的顺序出站。

    若能,输出这个口进出的顺序。

    Sample Input
    3 123 321
    3 123 312
     

     

    Sample Output
    Yes.
    in
    in
    in
    out
    out
    out
    FINISH
    No.
    FINISH
     
    直接用stack进行模拟就OK了。
    只要确定好什么情况下为Yes,什么情况下为No。
     
     1 #include<cstdio>
     2 #include<stack>
     3 #include<cstring>
     4 using namespace std;
     5 char in[12];
     6 char out[12];
     7 int ans[24];
     8 int main()
     9 {
    10     int n;
    11     while(scanf("%d",&n)!=EOF)
    12     {
    13         scanf("%s",in+1);
    14         scanf("%s",out+1);
    15         memset(ans,-1,sizeof(ans));
    16         stack<char>s;
    17         while(!s.empty())
    18             s.pop();
    19         int i=1,j=1;
    20         s.push(in[i]);
    21         int tot=1;
    22         ans[tot++]=0;
    23         bool flag=false;
    24         while(i<n+1)
    25         {
    26             while(s.size()&&s.top()==out[j])
    27             {
    28                 s.pop();
    29                 ans[tot++]=1;
    30                 j++;
    31                 if(j==n+1)
    32                     break;
    33             }
    34             if(j==n+1)
    35             {
    36                 flag=true;
    37                 break;
    38             }
    39             i++;
    40             if(i<=n)
    41             {
    42                 s.push(in[i]);
    43                 ans[tot++]=0;
    44             }
    45         }
    46         if(flag)
    47         {
    48             printf("Yes.
    ");
    49             for(int i=1;i<tot;i++)
    50                 if(ans[i]==0)
    51                     printf("in
    ");
    52                 else
    53                     printf("out
    ");
    54             printf("FINISH
    ");
    55         }
    56         else
    57         {
    58             printf("No.
    FINISH
    ");
    59         }
    60     }
    61     return 0;
    62 }
    提交代码
     
     
  • 相关阅读:
    目标跟踪学习笔记1
    求职笔记
    Pycharm使用问题小结-002
    Pycharm使用问题小结-001
    Python基础练习-004-百变乘法表
    Python基础练习-003-求100-999之间所有的水仙花数
    Python基础练习-002-求1000以内的完全数
    Python基础练习-001-猜数字小游戏
    java.io.FileNotFoundException: rmi_keystore.jks (No such file or directory)(转)
    Jmeter
  • 原文地址:https://www.cnblogs.com/-maybe/p/4393982.html
Copyright © 2011-2022 走看看