zoukankan      html  css  js  c++  java
  • hdu1022 Train Problem I---模拟栈

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

    题目大意:

    车的进出站问题,先给出N个火车,再按序列一的方式进站,判断能否以序列二的方式出站,若能先输出“Yes.”,再输出出站步骤,以FINISH结束,若不能,输出“No.”,仍以FINISH结束。

    思路:

    直接模拟栈,注意细节!

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<queue>
     7 #include<stack>
     8 using namespace std;
     9 typedef long long ll;
    10 const int maxn = 1e6 + 10;
    11 const int INF = 1 << 30;
    12 int T, n, m;
    13 int v[20];
    14 int main()
    15 {
    16     string s1, s2;
    17     while(cin >> n >> s1 >> s2)
    18     {
    19         int j = 0;
    20         int tot = 0, ok = 1;
    21         stack<char>q;
    22         for(int i = 0; i < s1.size(); i++)
    23         {
    24             q.push(s1[i]);
    25             v[tot++] = 1;
    26             while(!q.empty() && q.top() == s2[j])//一开始将j++写在这里,是错的,因为每次判断条件都会加一
    27             {
    28                 q.pop();
    29                 v[tot++] = 0;
    30                 j++;
    31             }
    32         }
    33         if(j == n && tot == 2 * n)printf("Yes.
    ");
    34         else printf("No.
    ");
    35         if(j == n && tot == 2 * n)
    36         {
    37             for(int i = 0; i < tot; i++)if(v[i])printf("in
    ");
    38             else printf("out
    ");
    39         }
    40         printf("FINISH
    ");
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    redis 学习笔记
    导数据方法
    数据库常用操作
    zepto.js
    shopnc
    vue.js
    laravel
    mysql进阶学习
    Python基础------生成器表达式形式、面向过程编程、内置函数部分
    Python基础----生成器、三元表达式、列表生成式、生成器表达式
  • 原文地址:https://www.cnblogs.com/fzl194/p/8698354.html
Copyright © 2011-2022 走看看