两种思路
(1)将目标栈的数据和待混洗的数据逐个进行比较
#pragma warning(disable:4996)
#include<stdio.h>
#include<string.h>
#define maxn 1600005
using namespace std;
char a[2*maxn][5];//记录操作命令
int stack[maxn];
char pop[5] = "pop", push[5] = "push";
int main()
{
int m = 0, n = 0, compare = 0, cap = 0, length, size = 0;//cap是a中目前元素位置
#ifndef _OJ_
freopen("trainin.txt", "r", stdin);
freopen("trainout.txt", "w", stdout);
#endif
scanf("%d %d", &n, &m);//n是数目,m是栈的容量
length = n;
int num, e;
while (n--&&size < m && compare < length)
{
scanf("%d", &num);
//栈为空的时候,则先push,再pop出来进行比较
if (size == 0) {
stack[size++] = ++compare;
memcpy(a[cap++], push, strlen(push));
}
//将栈的内容pop出来进行比较
e = stack[--size];
//如果栈的内容与目标num相等,则不再push进去
if (e == num)memcpy(a[cap++], pop, strlen(pop));
//先将内容push进去,比较push进去的内容是否大于num,当大于,则结束
//否则,则继续往栈添加元素
//添加完元素之后就pop出来进行比较,若相等,则结束;否则,继续添加,直至,添加了最后一个元素,若仍然不相等,则结束
else
{


582

被折叠的 条评论
为什么被折叠?



