zoukankan      html  css  js  c++  java
  • HDU 1022(火车过站 栈)

    题意是给定火车进站的序列和出站的序列,问能否完成转换,若能输出过程。

    和另一道以火车进站为背景的栈应用题类似,但增加了对于过程的输出,只需要多记录一下进出站顺序即可。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int main()
     4 {
     5     std::ios::sync_with_stdio(false);
     6     int n,pos,cnt,vis[1000];
     7     stack<char> q;
     8     char come[1000],go[1000];
     9     while(cin >> n)
    10     {
    11         cin >> come >> go;
    12         pos = 0;
    13         cnt = 0;
    14         while(!q.empty()) q.pop();
    15         memset(vis,-1,sizeof(vis));
    16         for(int i = 0; i < n; i++)
    17         {
    18             vis[cnt++] = 1;
    19             q.push(come[i]);
    20             while(!q.empty()&&q.top() == go[pos])
    21             {
    22                 q.pop();
    23                 pos++;
    24                 vis[cnt++] = 0;
    25             }
    26         }
    27         if(pos==n)
    28         {
    29             cout << "Yes.
    ";
    30             for(int i = 0; i < cnt; i++)
    31             {
    32                 if(vis[i]) cout << "in
    ";
    33                 else cout << "out
    ";
    34             }
    35         }
    36         else cout << "No.
    ";
    37         cout << "FINISH
    ";
    38     }
    39     return 0;
    40 }
    View Code
    日后若能有更好的想法,再来完善。 希望看到的大神不吝赐教 orz
  • 相关阅读:
    tar命令,vi编辑器
    Linux命令、权限
    Color Transfer between Images code实现
    利用Eclipse使用Java OpenCV(Using OpenCV Java with Eclipse)
    Matrix Factorization SVD 矩阵分解
    ZOJ Problem Set
    Machine Learning
    ZOJ Problem Set
    ZOJ Problem Set
    ZOJ Problem Set
  • 原文地址:https://www.cnblogs.com/Taskr212/p/9531775.html
Copyright © 2011-2022 走看看