zoukankan      html  css  js  c++  java
  • hdu 1022 Train Problem I

    Train Problem I

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


    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
    Hint
    Hint
    For the first Sample Input, we let train 1 get in, then train 2 and train 3. So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1. In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3. Now we can let train 3 leave. But after that we can't let train 1 leave before train 2, because train 2 is at the top of the railway at the moment. So we output "No.".
     
    该题就是模拟栈的操作,检验是否能按后面的那个栈出栈,能的话输出栈的操作,
    我使用一个数组a[]来保存进栈和出栈的的过程(1表示进栈,2表示出栈)
    结束条件,当第一个字符串全部进入栈中操作,又出现没有不相等时这种情况是不可满足的(no)
    当这个栈指针 <= 0 题目给我的出栈顺序指针指向等该大小时 这种出栈顺序可以,并且这个顺序保存在了数组a中
     
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 using namespace std;
     5 
     6 int a[100];
     7 
     8 bool judge_char(const char *p, const char *q)
     9 {
    10     char top_ch[100];
    11     top_ch[0] = '\0';//
    12     int a_t = 0;
    13     int p_t = 0;
    14     int q_t = 0;
    15     int top_t = 0;
    16     int i;
    17     a[a_t] = 1;
    18     top_ch[top_t] = p[0];
    19 
    20     int flag = 0;
    21     while(1)
    22     {
    23         if(top_t == strlen(q))
    24             flag =1;
    25          if(q_t == strlen(p) && top_t <= 0)
    26            {
    27                return 1;
    28                break;
    29            }
    30        if(top_ch[top_t] == q[q_t])
    31           {
    32                 a_t++;
    33               a[a_t] = 2;
    34               top_t--;
    35               q_t++;
    36           }
    37         if( top_ch[top_t] != q[q_t])
    38         {
    39             if(flag)
    40               {
    41                   return 0;
    42                   break;
    43               }
    44               a_t++;
    45             a[a_t] = 1;
    46             top_t++;
    47             p_t++;
    48             top_ch[top_t] = p[p_t];
    49         }
    50     }
    51 }
    52 
    53 int main()
    54 {
    55     int n;
    56     char ch[100];
    57     char ch1[100];
    58     while(scanf("%d%s%s",&n,ch,ch1) != EOF)
    59      {
    60          memset(a,0,sizeof(a));
    61 
    62          if(judge_char(ch,ch1) == 0)
    63             {
    64                 printf("No.\n");
    65                 printf("FINISH\n");
    66                 continue;
    67             }
    68         else
    69         {
    70             printf("Yes.\n");
    71             int i;
    72             for(i = 0; ; i++)
    73              if(a[i])
    74              {
    75                  if(a[i] == 1)
    76                    printf("in\n");
    77                 else
    78                   printf("out\n");
    79              }
    80              else
    81               break;
    82               printf("FINISH\n");
    83         }
    84      }
    85      return 0;
    86 }
     
  • 相关阅读:
    EasyUI datagrid 的多条件查询
    js将时间戳转换为日期类型
    js限制字符串长度,超出的部分补...
    js打印WEB页面内容代码大全
    js倒计时一分钟
    java获取上个星期第一天和最后一天
    两个Html页面之间值得传递
    jS处理中英文时间格式化问题
    JS获取当月第一天和最后一天
    Java 计算两个日期相差月数、天数
  • 原文地址:https://www.cnblogs.com/yyroom/p/3035595.html
Copyright © 2011-2022 走看看