算法问题:查找从堆栈中弹出的所有序列

Algorithm problem: find all sequences popped from the stack

本文关键字:问题 查找 堆栈 算法      更新时间:2023-10-16

假设一个[1, 2, 3, 4]列表要推送到堆栈,从堆栈弹出的可能顺序是:

1 2 3 4                                                                                  
1 2 4 3                                                                                  
1 3 2 4                                                                                  
1 3 4 2                                                                                  
1 4 3 2                                                                                  
2 1 3 4                                                                                  
2 1 4 3                                                                                  
2 3 1 4                                                                                  
2 3 4 1                                                                                  
2 4 3 1                                                                                  
3 2 1 4                                                                                  
3 2 4 1                                                                                  
3 4 2 1                                                                                  
4 3 2 1

我找到了一个C++版本的算法,它给出了正确的输出:

#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <stdlib.h>
#include <cctype>
#include <stack>
#include <queue>

using namespace std;


void printAllOutStackSeq( queue<int> inQueue, int n, stack<int> st, queue<int> out )
{
if( n <= 0 || ( inQueue.empty() && st.empty() && out.empty() ) )
{
return;
}

if( out.size() == n )
{
while( !out.empty() )
{
cout << out.front() << ' ';
out.pop();
}

cout << endl;
return;
}

stack<int> stCopy = st;
queue<int> outCopy = out;

if( !st.empty() )
{
out.push( st.top() );
st.pop();
printAllOutStackSeq( inQueue, n, st, out ); 
}


if( !inQueue.empty() )
{
stCopy.push( inQueue.front() );
inQueue.pop();
printAllOutStackSeq( inQueue, n, stCopy, outCopy ); 
}

return;
}


int main()
{
int ret = 0;

int a[] = { 1, 2, 3, 4 };
queue<int> inQueue;

for( int i = 0; i < 4; i++ )
{
inQueue.push( a[i] );
}

int n;
stack<int> st;
queue<int> out;

printAllOutStackSeq( inQueue, 4, st, out );

return ret;
}

在我将C++转换为Python后,如下所示:

from typing import List
import copy
def print_out_stack_seq(to_push: List[int], n,
stack: List[int],
popped: List[int]):
if n <= 0 or (not to_push and not stack and not popped):
return

if len(popped) == n:
while len(popped):
print(popped.pop(0), end=' ')
print()
return

stack_copy = copy.copy(stack)
popped_copy = copy.copy(popped)

if stack:
popped.append(stack.pop())
print_out_stack_seq(to_push, n, stack, popped)

if to_push:
stack_copy.append(to_push.pop(0))
print_out_stack_seq(to_push, n, stack_copy, popped_copy)

return

to_push = [1, 2, 3, 4]
n = len(to_push)
stack = []
popped = []
print_out_stack_seq(to_push, n, stack, popped)

它只打印第一行:

1 2 3 4

我真的不知道为什么,有人能帮我吗?

在C++版本中,inQueue是通过递归调用中的副本传递的。在您的python版本中,inQueue不是to_push。

在调用print_out_stack_seq之前添加to_push的副本似乎可以解决您的问题。

from typing import List
import copy
def print_out_stack_seq(to_push: List[int], n,
stack: List[int],
popped: List[int]):
if n <= 0 or (not to_push and not stack and not popped):
return
if len(popped) == n:
while len(popped):
print(popped.pop(0), end=' ')
print()
return
stack_copy = copy.copy(stack)
popped_copy = copy.copy(popped)
if stack:
popped.append(stack.pop())
print_out_stack_seq(copy.copy(to_push), n, stack, popped)
if to_push:
stack_copy.append(to_push.pop(0))
print_out_stack_seq(copy.copy(to_push), n, stack_copy, popped_copy)
return

to_push = [1, 2, 3, 4]
n = len(to_push)
stack = []
popped = []
print_out_stack_seq(to_push, n, stack, popped)

输出:

1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 3 1
3 2 1 4
3 2 4 1
3 4 2 1
4 3 2 1

然而,这个输出似乎不是正确的答案,因为序列(4 3 1 2(是有效的,但没有打印出来。正如其他人提到的,你打印的是所有排列的列表。在python中,可以使用以下代码完成:

import itertools
to_push = [1, 2, 3, 4]
for p in itertools.permutations(to_push):
print(p)

输出:

(1, 2, 3, 4)
(1, 2, 4, 3)
(1, 3, 2, 4)
(1, 3, 4, 2)
(1, 4, 2, 3)
(1, 4, 3, 2)
(2, 1, 3, 4)
(2, 1, 4, 3)
(2, 3, 1, 4)
(2, 3, 4, 1)
(2, 4, 1, 3)
(2, 4, 3, 1)
(3, 1, 2, 4)
(3, 1, 4, 2)
(3, 2, 1, 4)
(3, 2, 4, 1)
(3, 4, 1, 2)
(3, 4, 2, 1)
(4, 1, 2, 3)
(4, 1, 3, 2)
(4, 2, 1, 3)
(4, 2, 3, 1)
(4, 3, 1, 2)
(4, 3, 2, 1)