HDU - 5818 Joint Stacks 比较大の模拟,stack,erase

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 % == )
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=;
while (cin >> n) {
if (n == )break;
int cnt = ; a.clear(); b.clear(); c.clear();
printf("Case #%d:\n", ++kase);
for (int i = ; i <= n; i++) {
char s[];
scanf("%s", s);
if (s[] == 'u') {//push
char aa[];
scanf("%s", &aa);
int x ; scanf("%d", &x);
if (aa[] == 'A') {
a.push_back(make_pair(x,cnt++));
}
else {
b.push_back(make_pair(x,cnt++));
}
}
else if (s[] == 'o') {//pop
char aa[];
scanf("%s", &aa);
if (aa[] == 'A') {
if (!a.empty()) {
printf("%d\n", a.back().first);
a.pop_back();
}
else {
printf("%d\n", c.back());
c.pop_back();
}
}
else {
if (!b.empty()) {
printf("%d\n", b.back().first);
b.pop_back();
}
else {
printf("%d\n", c.back());
c.pop_back();
}
}
}
if (s[] == 'e') {//merge
vector<int> tmp;
char aa[];
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();
}
}
}
}
}
}
上一篇:Spring Boot设置上传文件大小


下一篇:J2EE开发时的包命名规则,养成良好的开发习惯