【问题标题】:Passing list (stack) in backtracing in python在python的回溯中传递列表(堆栈)
【发布时间】:2016-01-30 21:48:52
【问题描述】:

我正在尝试在 python 中编写hackerrank 的this problem

(如果链接失效,问题如下)

“悟空用一些块写了一条秘密信息。每个块都包含一个字符。 悟饭喜欢玩这些积木。他将这些积木叠在一起。

在任何时候,悟饭都可以将一个块从秘密消息的前面移到堆栈的顶部,或者他可以从顶部删除块并将其添加到他创建的新消息的末尾.

Gohan 通过将他从堆栈顶部删除的块按照从堆栈顶部删除的顺序添加到新字符串的末尾来创建此新消息。

最终消息与原始消息具有相同数量的字符,即必须从堆栈中删除所有块。 悟空担心他的原始信息可能会丢失。

他想知道悟饭可以用多少种方式重新创建原始消息,以及悟饭可以创建多少不同的消息。

示例输入:球

样本输出:2 9"

Gohan 可以创建以下 9 条消息:{llab, lalb, labl, allb, albl, abll, blla, blal, ball}

这是一个回溯问题,我们需要在递归中传递一个堆栈,并且我们在递归中修改(插入/删除)堆栈。我正在使用普通列表作为堆栈。

问题的编辑是:(link)

(如果链接失效,社论在下方)

Times_Recreated=0
Distinct_Messages=0

calculate_ans(OriginalMessage,Index,Stack st,NewMessage)

if Index = Length of the original message
        Pop the remaining characters in the stack and add them to NewMessage
        If NewMessage hasn't been encountered already, increment Distinct_Messages.
        If NewMessage is same as OriginalMessage, increment Times_Recreated.
        Return

Add OriginalMessage[Index] to the stack
Recurse for the remaining string  calculate_ans(OriginalMessage,Index+1,st,NewMessage)

Pop the character from the top of the stack to restore the original state

If the stack isn't empty
    Pop a character from the stack and add the character to NewMessage
    Recurse calculate_ans(OriginalMessage,Index,st,NewMessage)

我正在尝试下面的代码,但我无法将 list(stack) 传递给允许对其进行修改的递归。

我认为这个列表对于函数来说是全局的。它显示错误 - IndexError: pop from empty list

s= str(raw_input()) #string s as message input
words= set() # to store distinct messages Gohan can create
count=0 # number of ways in which Gohan could have recreated the original message

# s=message, i=index of s to be processed, stack= list as stack, new_s= new message 
def backtrack(s, i, stack, new_s):
    if i==len(s): 
        # pop the remaining characters from stack and add them to new_message 
        while stack:
            new_s=new_s+ stack.pop() 
        words.add(new_s) # insert new message to set words 
        if new_s==s:
            count+=1 # increase count if original message is obtained 
        return
    stack.append(s[i]) #push in stack 
    backtrack(s, i+1, stack, new_s) # backtrack 
    stack.pop() #pop from stack 
    if stack:
        new_s= new_s+stack.pop() # new message by appending stack pop 
        backtrack(s, i, stack, new_s) # backtrack 


# function call with i=0, a blank list stack and empty new message new_s
backtrack(s, 0, [], "") 
print count, len(words)  # printing output 

请更正此代码或提供一些方法/代码以在这种情况下传递列表。

【问题讨论】:

    标签: python list recursion stack backtracking


    【解决方案1】:

    python 中的变量是通过引用传递的,这意味着当您对堆栈变量进行递归调用并弹出其所有元素时,它在所有其他递归调用中都是空的,因此将其更改为

    backtrack(s, i+1, [x for x in stack], new_s)
    

    每次都会创建一个新列表,并且在回溯函数中将 count 声明为全局变量

    global count
    

    【讨论】:

    • 成功了。谢谢。所以这个概念是创建一个新的列表来回溯。
    猜你喜欢
    • 2018-07-02
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多