zoukankan      html  css  js  c++  java
  • HDU

    https://vjudge.net/problem/HDU-5818

    题意:给你两个栈AB,有常规push,pop操作,以及一个merge操作,merge A B 即将A、B的元素按照入栈顺序全部出栈并推入栈A(merge B A 即反)

    题解:用一个C来辅助,每一次merge将AB中的数据依次压入C中(如何依次?用vector<pair<int,int> >,second 存入栈的id,先按id弹出到tmp,再压入c),然后清空。之后A B若pop到底则从C pop(题目保证C不会pop到底)

    技巧:erase(并没用)

    list<int>::iterator it; 
      for (it = lt.begin(); it != lt.end(); ) {
        if (*it % 2 == 0)
          it = lt.erase(it);//自动返回下一个元素的地址,不用再主动前移指针
        else
          ++it;
      }

    坑:读char、char[]==""

    ac代码:

    #define _CRT_SECURE_NO_WARNINGS
    #include<set>
    #include<vector>
    #include<iostream>
    #include<stdio.h>
    #include<string>
    using namespace std;
    typedef long long ll;
    //ll a[100005];
    vector<pair<int,int> > a, b;
    vector<int> c;
    int main() {
        int n;
        int kase=0;
        while (cin >> n) {
            if (n == 0)break;
            int cnt = 0; a.clear(); b.clear(); c.clear();
            printf("Case #%d:
    ", ++kase);
            for (int i = 1; i <= n; i++) {
                char s[10];
                scanf("%s", s);
                if (s[1] == 'u') {//push
                    char aa[5];
                    scanf("%s", &aa);
                int x ; scanf("%d", &x);
                if (aa[0] == 'A') {
                    a.push_back(make_pair(x,cnt++));
                }
                else {
                    b.push_back(make_pair(x,cnt++));
                }
                }
                else if (s[1] == 'o') {//pop
                    char aa[5];
                    scanf("%s", &aa);
                    if (aa[0] == 'A') {
                        if (!a.empty()) {
                            printf("%d
    ", a.back().first);
                            a.pop_back();
                        }
                        else {
                            printf("%d
    ", c.back());
                            c.pop_back();
                        }
                    }
                    else {
                        if (!b.empty()) {
                            printf("%d
    ", b.back().first);
                            b.pop_back();
                        }
                        else {
                            printf("%d
    ", c.back());
                            c.pop_back();
                        }
                    }
                }
                if (s[1] == 'e') {//merge
                    vector<int> tmp;
                    char aa[5];
                    scanf("%s", &aa); scanf("%s", &aa);
                     {
                        while (!a.empty() || !b.empty()) {
                            if (a.empty()) {
                                while (!b.empty()) {
                                    tmp.push_back(b.back().first);
                                    b.pop_back();
                                }
                            }
                             if (b.empty()) {
                                while (!a.empty()) {
                                    tmp.push_back(a.back().first);
                                    a.pop_back();
                                }
                            }
                             if (!a.empty() && !b.empty()) {
                                 if (a.back().second > b.back().second) {
                                     tmp.push_back(a.back().first);
                                     a.pop_back();
                                 }
                                 else {
                                     tmp.push_back(b.back().first);
                                     b.pop_back();
                                 }
                            }
                        }
                        while (!tmp.empty()) {
                            c.push_back(tmp.back());
                            tmp.pop_back();
                        }
                    }                
                }
            }
        }
    }
    成功的路并不拥挤,因为大部分人都在颓(笑)
  • 相关阅读:
    省市区三级联动
    VUE项目PC端实现自适应rem
    (课堂笔记)第十三章GLSB涉及负载均衡算法
    LTM理解及配置笔记记录
    实验演示---GSLB部分(DC1)
    F5实验模拟拓扑
    (课堂笔记)第十三章:DNS 全局站点
    AbstractList类_Iterator内部类
    Oracle 时间格式化异常:无效数字
    Maven库镜像
  • 原文地址:https://www.cnblogs.com/SuuT/p/8575020.html
Copyright © 2011-2022 走看看