Algorithm 仅使用单个堆栈的B C D的可能输出序列?
我想只使用一个堆栈来确定序列A B C D的可能输出 我试图产生输出ADBC,但没有成功Algorithm 仅使用单个堆栈的B C D的可能输出序列?,algorithm,data-structures,stack,queue,Algorithm,Data Structures,Stack,Queue,我想只使用一个堆栈来确定序列A B C D的可能输出 我试图产生输出ADBC,但没有成功 可能的输出序列是什么?给定一个输入序列A B C D和一个空堆栈,您可以执行以下操作: 从输入序列中获取下一个值并将其推送到堆栈上 从堆栈中弹出一个值并输出它 从输入序列中获取下一个值并将其输出 动作3实际上与在执行动作1之后再执行动作2相同,因此我们可以忽略动作3作为一个可能的动作 为了输出所有输入值,我们需要执行四次操作1和四次操作2。考虑到只有在堆栈上仍然有值时才能执行操作2,我们可以使用顺序 因此,
可能的输出序列是什么?给定一个输入序列A B C D和一个空堆栈,您可以执行以下操作:
series of actions | output
------------------+-------------
12121212 | ABCD
12121122 | ABDC
12112212 | ACBD
12112122 | ACDB
12111222 | ADCB
11221212 | BACD
11221122 | BADC
11212212 | BCAD
11212122 | BCDA
11211222 | BDCA
11122212 | CBAD
11122122 | CBDA
11121222 | CDBA
11112222 | DCBA
这个列表可以通过一个小的递归函数生成。例如,通过此JavaScript实现:
函数重现(输入、堆栈、输出、操作){
如果(output.length==4){
日志(操作、输出);
返回;
}
如果(堆栈长度>0){
递归(输入,stack.slice(0,stack.length-1),输出+stack[stack.length-1],操作+2”);
}
如果(input.length>0){
递归(input.slice(1),stack+input[0],output,actions+1”);
}
}
重复(“ABCD”,“ABCD”,“ABCD”,“ABCD”,“ABCD”)代码>A B C D-->A D B C使用堆栈(后进先出)是可能的(案例1和案例2),不可能的案例3(简而言之@trincot answer是案例3的完美选择)
案例1:我们假设序列中的元素已(基于()的重要澄清):
-
- 转到字符串的中间==>ab | cd
-
- 从中间到左边使用索引id_到_,从中间到右边使用索引id_到_
-
- 在堆栈中插入一个元素,使用id\u向右插入,然后使用id\u向左插入一个元素
-
- 重复操作3直到结束
-
- 从堆栈中输出元素
演示:
它1:add(C),add(B)=>C,B
它2:add(D),add(A)=>C,B,D,A
输出:A、D、B、C
使用python的源代码:
my_list = ['A','B','C','D']
id_to_left = int(len(my_list)/2-1)
id_to_right = int(len(my_list)/2)
stack = []
# When number of elements is odd
if len(my_list) % 2 != 0:
stack.append(my_list[id_to_right])
id_to_right = id_to_right +1
while (id_to_left>=0):
stack.append(my_list[id_to_right])
stack.append(my_list[id_to_left])
id_to_left = id_to_left-1
id_to_right = id_to_right +1
for _ in range(len(stack)):
print(stack.pop(), end=" ")
my_list = ['A', 'B', 'C', 'D']
n = len(my_list)
stack = []
# step 1
while (True):
for _ in range(len(my_list)): # in all to stack
stack.append(my_list.pop(0))
n = n - 1
if n < 2:
break
for _ in range(n): # out n from the stack
my_list.append(stack.pop())
# step 2:
for _ in range(len(stack)): # > out all from the stack
my_list.append(stack.pop())
for _ in range(len(my_list)): # > in all to stack
stack.append(my_list.pop(0))
# > out all from stack ==> print result
print("\n The result is : ", end=" ")
for _ in range(len(stack)):
print(stack.pop(), end=" ")
结果将是:
A D B C
情况2:只允许顺序访问(不允许随机访问):
输入:
| |
| |
| |
| |
+--+A、B、C、D
步骤1)
it1:
>总分>总分前3名
|D | ||
|C | ||
|B | ||
|A | | A|
+--++--+D C B
it2:
>总分>总分前2名
|B | ||
|C | ||
|D | | D|
|A | | A|
+--++--+B C
it3:
>总共>停止
|C|
|B|
|D|
|A|
+--+
步骤2)
>全部输出>全部输入>全部输出
|| | A | ||
|| | D | ||
|| | B | ||
|| | C | ||
+--+C B D A++++--+A D B C
结束。
结果=A D B C
伪代码:
input : set of "n" elements
step 1:
while True :
> in all elements from the set to stack (pop elements from the list)
> n = n-1
> if n < 2 stop the loop, (break)
> out n from the stack
step 2:
> out all from the stack
> in all to stack
> out all from stack
> print result
> end.
输入:一组“n”元素
步骤1:
尽管如此:
>在集合到堆栈的所有元素中(从列表中弹出元素)
>n=n-1
>如果n<2,停止回路,(断开)
>从堆栈中取出
步骤2:
>一笔勾销
>总而言之
>一笔勾销
>打印结果
>结束。
使用python的源代码:
my_list = ['A','B','C','D']
id_to_left = int(len(my_list)/2-1)
id_to_right = int(len(my_list)/2)
stack = []
# When number of elements is odd
if len(my_list) % 2 != 0:
stack.append(my_list[id_to_right])
id_to_right = id_to_right +1
while (id_to_left>=0):
stack.append(my_list[id_to_right])
stack.append(my_list[id_to_left])
id_to_left = id_to_left-1
id_to_right = id_to_right +1
for _ in range(len(stack)):
print(stack.pop(), end=" ")
my_list = ['A', 'B', 'C', 'D']
n = len(my_list)
stack = []
# step 1
while (True):
for _ in range(len(my_list)): # in all to stack
stack.append(my_list.pop(0))
n = n - 1
if n < 2:
break
for _ in range(n): # out n from the stack
my_list.append(stack.pop())
# step 2:
for _ in range(len(stack)): # > out all from the stack
my_list.append(stack.pop())
for _ in range(len(my_list)): # > in all to stack
stack.append(my_list.pop(0))
# > out all from stack ==> print result
print("\n The result is : ", end=" ")
for _ in range(len(stack)):
print(stack.pop(), end=" ")
my_list=['A','B','C','D']
n=len(我的列表)
堆栈=[]
#第一步
虽然(正确):
对于范围内的(len(my_列表)):#全部到堆栈
stack.append(my_list.pop(0))
n=n-1
如果n<2:
打破
对于u在范围内(n):#从堆栈中取出n
my_list.append(stack.pop())
#步骤2:
对于uu-in范围(len(stack)):#>从堆栈中取出所有内容
my_list.append(stack.pop())
对于范围内的(len(my_列表)):#>在所有堆栈中
stack.append(my_list.pop(0))
#>从堆栈中全部输出==>打印结果
打印(“\n结果为:”,end=“”)
对于范围内的(透镜(堆栈)):
打印(stack.pop(),end=”“)
注意:这两种情况下,工作元素的个数为偶数或奇数,您可以试试
- A B C D-->A D B C
- A B x C D-->A D B C x
- X A B C D Y-->X Y A D B C
案例3:A)元素类似于流式处理(只允许顺序访问),B)没有可用的空间内存,只有堆栈。如果从堆栈中弹出一个元素,则无法将其推回,因为您没有地方容纳它,如果弹出一个元素,则意味着它直接进入输出对于本例3,请参考非常好的答案。
编辑信息:我添加了案例2,并处理了元素数为奇数的案例。添加没有解决方案的情况3。来自itertools导入置换
seq=置换(['a','b','c','d'])
对于列表中的p(序号):
打印(p)在B C D中,如果D位于堆栈顶部,则弹出顺序将为D C B A,因为堆栈是后进先出的。它不是D C A B?不,堆栈遵循后进先出原则。我认为可以使用堆栈(后进先出)通过简单的步骤从(A,B,C,D)-->(A,D,B,C)生成,请检查我上面的答案。我检查了你的答案,但你没有将输入视为一个序列,也没有将自己限制为一个堆栈。是的,我确实阅读了你的评论,非常感谢你的澄清,我将在我的答案中添加假设。您好:),先生,你能再次检查我的答案吗?案例2:只允许顺序访问(不允许随机访问):我想我找到了解决问题的方法。我先演示了解决方案,然后是伪代码,最后是python的源代码。是的,我用了一个额外的内存