[英]RE: Number of a's is divisible by 6 and Number of b's is divisible by 8
Find a regular expression which represents strings made of {
a,
b
}, where number ofa
's is divisible by 6 and number ofb
's is divisible by8
.找到一个表示由{
a,
b
}组成的字符串的正则表达式,其中a
的数字可以被6整除,b
的数量可以被8
整除。
I tried to create a DFA which accepts such strings. 我试图创建一个接受这种字符串的DFA。 My idea was to use all the remainders mod 6 and mod 8 leading to a total of 48 remainders.
我的想法是使用所有余数mod 6和mod 8导致总共48个余数。 Thus each state in DFA is a pair
(r, s)
where r
varies from 0 to 6 and s
varies from 0 to 7. Start state (as well as accepting state) is (0, 0)
and by we can easily give transitions to states by noting that if we input "a"
the state (r, s)
transitions to (r + 1, s)
and on input "b"
it transitions to state (r, s + 1)
. 因此,DFA中的每个状态都是一对
(r, s)
,其中r
从0到6变化, s
从0到7变化。开始状态(以及接受状态)是(0, 0)
,我们可以很容易地给出转换通过注意到如果我们输入"a"
,状态(r, s)
转换为(r + 1, s)
并且输入"b"
它转换到状态(r, s + 1)
。
However it is too difficult to work with a DFA of 48 states and I am not sure if this can be minimized by using the DFA minimization algorithm by hand. 然而,使用48个状态的DFA太难了,我不确定是否可以通过手动使用DFA最小化算法来最小化这种情况。
I am really not sure how then we can get to a regular expression representing such strings. 我真的不确定我们怎么能得到一个表示这种字符串的正则表达式。
If you are allowed to use lookaheads: 如果允许使用前瞻:
^(?=b*((ab*){6})+$)a*((ba*){8})+$
Example of matched string: bbaabbaabbaabb
匹配字符串的示例:
bbaabbaabbaabb
Idea is simple: We know how to match string having number of a
s divisible by 6 - ^((b*ab*){6})+$
, also we know how to match string having number of b
s divisible by 8 - ^((a*ba*){8})+$
. 想法很简单:我们知道如何搭配串具有数
a
由6 S整除- ^((b*ab*){6})+$
,我们也知道如何搭配具有数字串b
小号被8整除- ^((a*ba*){8})+$
。 So we just put one regex to lookahead, and another one to matching part. 所以我们只是将一个正则表达式用于预测,另一个用于匹配部分。
In case if you also need to match strings consisting only of a
s or only of b
s, then the following regex will do: 如果您还需要匹配仅
a
s或仅包含b
s的字符串,则以下正则表达式将执行以下操作:
^(?=b*((ab*){6})*$)a*((ba*){8})*$
Examples of matched strings: aaaaaa
, bbbbbbbb
, bbaabbaabbaabb
匹配字符串的示例:
aaaaaa
, bbbbbbbb
, bbaabbaabbaabb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.