测试两种常规语言的交集
我想测试两种语言是否有共同的字符串。这两种语言都来自下面描述的常规语言的子集,我只需要知道两种语言中是否存在字符串,而不是生成示例字符串。
语言由类似 glob 的字符串指定
<代码>/foo/**/bar/*.baz
其中 **
匹配 0 个或多个字符,*
匹配零个或多个字符不是 /
,所有其他字符都是文字。
有什么想法吗?
谢谢, 迈克
编辑:
I want to test whether two languages have a string in common. Both of these languages are from a subset of regular languages described below and I only need to know whether there exists a string in both languages, not produce an example string.
The language is specified by a glob-like string like
/foo/**/bar/*.baz
where **
matches 0 or more characters, and *
matches zero or more characters that are not /
, and all other characters are literal.
Any ideas?
thanks,
mike
EDIT:
I implemented something that seems to perform well, but have yet to try a correctness proof. You can see the source and unit tests
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为两种语言构建 FA
A
和B
,并构造“交集 FA”AnB
。如果 AnB 至少有一个可从开始状态访问的接受状态,则存在一个同时使用两种语言的单词。构造
AnB
可能很棘手,但我确信 FA 教科书会介绍它。我采取的方法是:AnB
的状态分别是A
和B
状态的笛卡尔积。AnB
中的状态写作(a, b)
,其中a
是A
和中的状态b
是B
中的状态。(a, b) ->r (c, d)
(意思是,存在从(a, b)
到(c, d) 符号
存在,当且仅当r
上的a ->r c
是A
中的转换,并且b - >r d
是B
中的转换。(a, b)
是AnB
中的开始状态,当且仅当a
和b
是中的开始状态分别为 A
和B
。(a, b)
是AnB
中的接受状态,当且仅当每个都是其各自 FA 中的接受状态。这都是我的想法,因此完全未经证实!
Build FAs
A
andB
for both languages, and construct the "intersection FA"AnB
. IfAnB
has at least one accepting state accessible from the start state, then there is a word that is in both languages.Constructing
AnB
could be tricky, but I'm sure there are FA textbooks that cover it. The approach I would take is:AnB
is the cartesian product of the states ofA
andB
respectively. A state inAnB
is written(a, b)
wherea
is a state inA
andb
is a state inB
.(a, b) ->r (c, d)
(meaning, there is a transition from(a, b)
to(c, d)
on symbolr
) exists iffa ->r c
is a transition inA
, andb ->r d
is a transition inB
.(a, b)
is a start state inAnB
iffa
andb
are start states inA
andB
respectively.(a, b)
is an accepting state inAnB
iff each is an accepting state in its respective FA.This is all off the top of my head, and hence completely unproven!
我刚刚进行了快速搜索,这个问题是可判定的(又名可以完成),但我不知道有什么好的算法可以做到这一点。一种解决方案是:
我知道这可能有点难以理解,但这是我知道的唯一方法。
I just did a quick search and this problem is decidable (aka can be done), but I don't know of any good algorithms to do it. One is solution is:
I know this might be a little hard to follow but this is only way I know how.