简体   繁体   English

以下条件的正则表达式

[英]Regular expression for the following condition

Define a regular expression for the alphabet {a,b} where subsequences of a's that aren´t empty are always even and subsequences of b's are always odd.为字母表 {a,b} 定义一个正则表达式,其中非空的 a 的子序列总是偶数,b 的子序列总是奇数。

I have something like this but I think it works such that the number of a's and b's in the expression are even and odd respectively without taking into account that each subsequence of a's has to be even numbered and b's has to be odd numbered:我有这样的东西,但我认为它的工作原理是表达式中 a 和 b 的数量分别为偶数和奇数,而不考虑 a 的每个子序列必须是偶数而 b 必须是奇数:

(a|b(aa|bb) (ab|ba)) (aa|bb|(ab|ba)(aa|bb) (ab|ba))* (a|b(aa|bb) (ab|ba)) (aa|bb|(ab|ba)(aa|bb) (ab|ba))*

I'll take the long way to an answer and see if I get what you did...我会走很长的路来回答,看看我是否明白你所做的......

We can make a DFA for this language as follows:我们可以为这种语言创建一个 DFA,如下所示:

       /---a---\       /---\
       |       |       |   | a,b
       V       |       V   |
----->q0--a-->q1--b-->q2---/
       |       ^       ^
       b       |       |
       |       a       |
       V       |       |
   /->q3-------/       |
   |   |               |
   |   b               |
   b   |               |
   |   V               |
   \--q4------a--------/
       

In this DFA, states q0 and q3 are accepting, since we have just seen an odd string of b's or an even string of a's, and we have not messed up yet and landed in q2, nor are we still needing an extra a/b to get an even/odd streak.在这个 DFA 中,状态 q0 和 q3 正在接受,因为我们刚刚看到一个奇数串 b 或一个偶数串 a,我们还没有搞砸并落在 q2,我们也不需要额外的 a/b获得偶数/奇数连胜。

With a DFA in hand, we can write some equations for strings that lead to each state:有了 DFA,我们可以为导致每个 state 的字符串编写一些方程式:

(q0) = e + (q1)a
(q1) = (q0)a + (q3)a
(q2) = (q1)b + (q4)a + (q2)(a+b)
(q3) = (q0)b + (q4)b
(q4) = (q3)b

We want to solve for (q0) and (q3).我们想求解 (q0) 和 (q3)。 We can pretty much ignore (q2) since we don't care about strings not accepted and it's not helpful for getting anywhere else.我们几乎可以忽略 (q2),因为我们不关心未接受的字符串并且它对获取其他任何地方都没有帮助。

(q0) = e + (q1)a
(q1) = (q0)a + (q3)a
(q3) = (q0)b + (q4)b
(q4) = (q3)b

(q0) = e + (q1)a
(q1) = (q0)a + (q3)a
(q3) = (q0)b + (q3)bb
     = (q0)b(bb)*

(q0) = e + (q1)a
(q1) = (q0)a + (q0)b(bb)*a
     = (q0)[a + b(bb)*a]

(q0) = e + (q0)[a + b(bb)*a]a
     = e + (q0)[aa + b(bb)*aa]
     = [aa + b(bb)*aa]*

So, the regular expression [aa + b(bb) aa] gives strings that lead to q0, and [aa + b(bb)*aa] b(bb) gives strings that lead to q3.因此,正则表达式 [aa + b(bb) aa]给出指向 q0 的字符串,而 [aa + b(bb)*aa] b(bb)给出指向 q3 的字符串。 The union of these is:这些的联合是:

  [aa + b(bb)*aa]* + [aa + b(bb)*aa]*b(bb)*
= [aa + b(bb)*aa]*[e + b(bb)*]

This isn't very close to your expression, but that doesn't mean yours is wrong... but I think yours is wrong since if I'm reading it right, we have this derivation:这与您的表达方式不太接近,但这并不意味着您的表达方式是错误的……但我认为您的表达方式是错误的,因为如果我没看错的话,我们可以得出以下推导:

(a|b(aa|bb)(ab|ba)) (aa|bb|(ab|ba)(aa|bb)(ab|ba))*     // initial
(a|b(aa|bb)(ab|ba))                                    // take * = 0
a                                                      // choose a

And a is not a string you want.而且 a 不是您想要的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM