将字符串分解为已知模式
这是 python 字符串列表:
patterns = [ “KBKKB”, “BBBK”, “BKB”, “KBBB”, “KBB”, “BKBB”, “BBKB”, “KKBKB”, “BKBK”, “KBKB”, “KBKBK”, “步步高”, “BB”, “BKKB”, “BBB”, “KBBK”, “BKKBK”, “KB”, “KBKBK”, “KKBKKB”, “KBK”, “BBKBK”, “BBBB”, “BK”, “KKBKBK”, “KBBKB”, “BBKKB”, “KKKKBB”, “KKB” ]
我有一个仅由任意长度的 K 和 B 组成的输入字符串。我想知道输入字符串的所有可能的完整分解。仅举一个 8 B 字符串的例子:
BBBBBBBB
这里有可能的分解
BB BB BB BB BB
BBBB BBBB
BBBB
BB BB BB BB BBBB
BBB BBB BB
BB BBB BBB
谁能指导我如何去做?我现在不太关心效率。
Here's the python list of strings:
patterns = [
"KBKKB",
"BBBK",
"BKB",
"KBBB",
"KBB",
"BKBB",
"BBKB",
"KKBKB",
"BKBK",
"KBKB",
"KBKBK",
"BBK",
"BB",
"BKKB",
"BBB",
"KBBK",
"BKKBK",
"KB",
"KBKBK",
"KKBKKB",
"KBK",
"BBKBK",
"BBBB",
"BK",
"KKBKBK",
"KBBKB",
"BBKKB",
"KKKKBB",
"KKB"
]
I have an input string that consist of K and B only of arbitrary length. I want to know all the possible complete decompositions of the input string. Just an example a string of 8 B:
BBBBBBBB
Here are possible decompositions
BB BB BB BB BB
BBBB BBBB
BBBB BB BB
BB BB BBBB
BBB BBB BB
BB BBB BBB
Can anyone guide me how to go about it? I am not much concerned about efficiency right now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是使用递归的一种方法:
Here's one way using recursion: