python字符串出栈方法_python-重复删除子字符串的出现,直到主字符...

这个问题已经在这里有了答案:????????????>????????????How to minimize a string’s length by iteratively removing all occurrences of some specified words from the string????????????????????????????????????2个

所以我有堆栈和针:

堆栈=’lllolollololollllolol’

针=’大声笑’

如果我每次以正确的顺序从堆栈中移出一根针,则可以清除堆栈,以便最后将其清空.

例如,每次都会删除黑体字的笑声(请注意,删除后可以进一步创建另一个针):

洛洛洛洛尔

洛洛洛洛尔洛洛尔

洛洛洛洛洛尔

洛洛洛洛尔

洛洛洛尔

洛洛尔

大声笑

明确

要找到上述路线,我想出的使用Python的唯一方法是使用正则表达式(finditer)查找堆栈中的所有指针,然后使用递归探索所有可能的删除组合以找到可以构成堆栈的组合空的.但是我知道这根本没有效率.

有没有一种更有效的方法来找到至少一种使用Python将针移到空堆栈的方法?

谢谢!

以下是我想出的代码(我知道很复杂):

def answer(chunk, word):

if chunk.find(word) != -1:

occ = [m.start() for m in finditer('(?='+word+')', chunk)]

for o in occ:

new = chunk[:o] + chunk[o + len(word):]

answer(new, word)

else:

result.append(chunk)

result.sort()

return chunk

...

#So all the shortest "leftover stack" after the removal are stored in list

#"result". These include empty or non-empty outputs depending on how

#the removal was executed.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值