zoukankan      html  css  js  c++  java
  • [HDOJ1022]Train Problem I

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022

    手写栈,模拟出入的过程。很简单,不停入栈,如果和当前要求出站车辆相同的话就出站,否则继续入栈。

     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <iostream>
     6 #include <cmath>
     7 #include <queue>
     8 #include <map>
     9 #include <set>
    10 #include <stack>
    11 #include <list>
    12 #include <vector>
    13 
    14 using namespace std;
    15 
    16 const int maxn = 100010;
    17 int n;
    18 char in[maxn];
    19 char out[maxn];
    20 char mystack[maxn];
    21 int ans[maxn];  //记下步骤
    22 int top, head, stp, now;
    23 
    24 void init() {
    25     memset(mystack, 0, sizeof(mystack));
    26     memset(ans, 0, sizeof(ans));
    27     stp = 0;    //步骤
    28     top = 0;    //栈顶
    29     head = 0;   //车头
    30     now = 0;    //待出栈车辆
    31 }
    32 
    33 void solve() {
    34     while(head < n) {
    35             mystack[++top] = in[head++];
    36             ans[stp++] = 1;
    37             while(top && mystack[top] == out[now]) {
    38                 top--;
    39                 now++;
    40                 ans[stp++] = 0;
    41             }
    42     }
    43     if(now == n) {
    44         printf("Yes.
    ");
    45         for(int i = 0; i < stp; i++) {
    46             if(ans[i]) {
    47                 printf("in
    ");
    48             }
    49             else {
    50                 printf("out
    ");
    51             }
    52         }
    53         printf("FINISH
    ");
    54     }
    55     else {
    56         printf("No.
    FINISH
    ");
    57     }
    58 }
    59 int main() {
    60     // freopen("in", "r", stdin);
    61     while(~scanf("%d", &n)) {
    62         scanf("%s %s", in, out);
    63         init();
    64         solve();
    65     }
    66 }
  • 相关阅读:
    hello world之vivado程序解决方法
    FPGA的电源选择重要性分析
    RabbitMQ的简单使用
    RabbitMQ的相关概念
    Spring整合Quartz
    DisallowConcurrentExecution注解
    Quartz框架中的监听器
    JobStore使用
    quartz基本介绍
    java自定义注解
  • 原文地址:https://www.cnblogs.com/kirai/p/4781073.html
Copyright © 2011-2022 走看看