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

    题目链接:https://vjudge.net/problem/HDU-1022

      非常经典的stack模拟,wa了n次才过....

    #pragma G++ optimize(2)
    #include<set>
    #include<map>
    #include<stack>
    #include<queue>
    #include<cmath>
    #include<stdio.h>
    #include<cctype>
    #include<string>
    #include<vector>
    #include<climits>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #define max(a, b) (a > b ? a : b)
    #define min(a, b) (a < b ? a : b)
    #define mst(a) memset(a, 0, sizeof(a))
    #define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> P;
    const double eps = 1e-7;
    const int INF = 0x3f3f3f3f;
    const ll ll_INF = 0x3f3f3f3f3f3f3f;
    const int maxn = 1e3+10;
    int mark[maxn];
    int main(void) {
        //std::ios::sync_with_stdio(false);
        int n;
        char _in[maxn], _out[maxn]; 
        //in存储入栈顺序 out存储出栈顺序
        while(~scanf("%d%s%s", &n, _in, _out)) {
            mst(mark);
            int c1 = 0, c2 = 0;
            stack<char> trains;
            for (int i = 0; i<n; ++i) {
                trains.push(_in[i]);
                mark[c1++] = 1;
                while(!trains.empty() && trains.top() == _out[c2]) { 
                    //如果栈首元素和出栈的元素相对应就弹出
                    trains.pop();
                    ++c2;
                    mark[c1++] = -1;
                }
            }
            if (!trains.empty())
            //如果剩余的还有火车,说明顺序不对,无法完成对应操作
                printf("No.\n");
            else {
                printf("Yes.\n");
                for (int i = 0; i<c1; ++i) {
                    if (mark[i] == 1)
                        printf("in\n");
                    else if (mark[i] == -1)
                        printf("out\n");
                    else 
                        break;
                }
            }
            printf("FINISH\n");
        }
        return 0;
    }
  • 相关阅读:
    PHP获取http头信息
    zend studio 改变背景颜色
    WebApp之Meta标签
    js的左右滑动触屏事件
    越狱的 iPhone、iPad 通过网站实现一键安装 ipa 格式的 APP 应用
    移动平台的meta标签神奇的功效
    HTML5 localStorage本地存储实际应用举例
    移动端开发小结
    openSUSE 安装源
    两种方法求Int最值
  • 原文地址:https://www.cnblogs.com/shuitiangong/p/12210444.html
Copyright © 2011-2022 走看看