如何枚举模式

发布于 2024-10-29 16:45:22 字数 215 浏览 1 评论 0原文

如何生成以下规则中的所有模式:

# 作为分隔符,{ 和 } 表示在 { 和 } 之间选择一个字符串。

例如:

Input: a string
{a # b} c {d # e {f # g}}

输出应该是:

a c d
b c d
a c e f
a c e g

How to generate all patterns in the following rule:

# as a separator, { and } means choose a string between { and }.

For example:

Input: a string
{a # b} c {d # e {f # g}}

Output should be:

a c d
b c d
a c e f
a c e g

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浊酒尽余欢 2024-11-05 16:45:22

首先,使用任何标准解析算法将输入字符串解析为树。上面示例的树形表达式类似于 (C = concatenate, S = select)

 C(S(a,b),C(c,S(d,C(e,S(f,g)))))

现在,实现一个过程,递归地计算该树(或表达式,您可能会这样称呼它)并返回一个 set< /em> 作为任何子表达式求值结果的字符串。然后评估就像这样

 S(f,g) == "f", "g"
 C(e,S(f,g)) == "ef", "eg"
 S(d,C(e,S(f,g))) = "d", "ef", "eg"
 C(c,S(d,C(e,S(f,g)))) = "cd", "cef", "ceg"
 S(a,b) = "a", "b"
 C(S(a,b),C(c,S(d,C(e,S(f,g))))) = "acd", "bcd", "acef", "bcef", "aceg", "bceg"

(顺便说一句,你的示例中缺少 bcef 和 bceg)

评估规则是:

S(X,Y) :评估 X 和 Y 并取集合

C(X,Y) 的并集:计算 X 和 Y 并通过从集合 X 中取出一个字符串和从集合 Y 中取出一个字符串来形成所有串联

First, parse the input string into a tree using any standard parsing algorithm. The tree-form expression for your example above would be something like (C = concatenate, S = select)

 C(S(a,b),C(c,S(d,C(e,S(f,g)))))

Now then implement a procedure that recursively evaluates this tree (or expression, as you might call it) and returns a set of strings as the result of evaluation any subexpression. Then the evaluation goes like this

 S(f,g) == "f", "g"
 C(e,S(f,g)) == "ef", "eg"
 S(d,C(e,S(f,g))) = "d", "ef", "eg"
 C(c,S(d,C(e,S(f,g)))) = "cd", "cef", "ceg"
 S(a,b) = "a", "b"
 C(S(a,b),C(c,S(d,C(e,S(f,g))))) = "acd", "bcd", "acef", "bcef", "aceg", "bceg"

(by the way, you are missing bcef and bceg from your example)

The evaluation rules are:

S(X,Y) : evaluate X and Y and take the union of the sets

C(X,Y) : evaluate X and Y and form all concatenations from taking one string from set X and one string from set Y

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文