Im having a problem in solving the problem:- Its an assignment, i solved it, but it seems to be too long and vague, Can anyboby help me please......
我在解决这个问题上有一个问题:——这是一个任务,我解决了,但它似乎太长,太模糊了,能不能帮我一下……
Regular expression for the strings with even number of a's and odd number of b's where the character set={a,b}.
字符串的正则表达式,偶数为a的偶数和奇数的b,其中的字符集={a,b}。
10 个解决方案
#1
16
One way to do this is to pass it through two regular expressions making sure they both match (assuming you want to use regular expressions at all, see below for an alternative):
一种方法是通过两个正则表达式来确保它们都匹配(假设您想要使用正则表达式,请参阅下面的选项):
^b*(ab*ab*)*$
^a*ba*(ba*ba*)*$
Anything else (and, in fact, even that) is most likely just an attempt to be clever, one that's generally a massive failure.
其他的(事实上,甚至是这样)很可能只是一个聪明的尝试,一个通常是巨大失败的尝试。
The first regular expression ensures there are an even number of a
with b
anywhere in the mix (before, after and in between).
第一个正则表达式确保在混合(before、after和in between)中有偶数个a和b。
The second is similar but ensures that there's an odd number of b
by virtue of the starting a*ba*
.
第二个是相似的,但是通过一个*ba*,确保有奇数个b。
A far better way to do it is to ignore regular expressions altogether and simply run through the string as follows:
一种更好的方法是完全忽略正则表达式,并简单地通过字符串运行如下:
def isValid(s):
set evenA to true
set oddB to false
for c as each character in s:
if c is 'a':
set evenA to not evenA
else if c is 'b':
set oddB to not oddB
else:
return false
return evenA and oddB
Though regular expressions are a wonderful tool, they're not suited for everything and they become far less useful as their readability and maintainability degrades.
虽然正则表达式是一种很好的工具,但是它们并不适合所有的东西,而且它们的可读性和可维护性也会降低。
For what it's worth, a single-regex answer is:
就其价值而言,一个单一的regex答案是:
(aa|bb|(ab|ba)(aa|bb)*(ba|ab))*(b|(ab|ba)(bb|aa)*a)
but, if I caught anyone on my team actually using a monstrosity like that, they'd be sent back to do it again.
但是,如果我发现我的团队中有人真的使用了这样的怪物,他们会被送回去再做一次。
This comes from a paper by one Greg Bacon. See here for the actual inner workings.
这是由格雷格·培根写的一篇论文。这里是实际的内部工作。
#2
1
(bb)*a(aa)*ab(bb)*
- (bb)*(aa)* ab(bb)*
ab(bb)* a(aa)*
- ab(bb)*(aa)*
-
b(aa)*(bb)*
.
.
.
.
.
. - b(aa)*(bb)* . .…
there can be many such regular expressions. Do you have any other condition like "starting with a" or something of the kind (other than odd 'b' and even 'a') ?
可以有很多这样的正则表达式。你是否有其他的条件,比如“以a开头”或其他类似的情况(除了奇数“b”,甚至“a”)?
#3
0
I would do as follows:
我会这样做:
- regex even matches the symbol a, then a sequence of b's, then the symbol a again, then another sequence of b's, such that there is an even number of b's:
- regex甚至匹配符号a,然后是b的序列,然后是符号a,然后是b的另一个序列,这样就有偶数个b:
even -> (a (bb)* a (bb)* | a b (bb)* a b (bb)*)
甚至-> (a (bb)* a (bb)* | a b (bb)* a b (bb)*)
- regex odd does the same with an odd total number of b's:
- regex奇数与奇数的b的总数相同:
odd -> (a b (bb)* a (bb)* | a (bb)* a b (bb)*)
奇数-> (bb)* a (bb)* | a (bb)* a b (bb)*)
A string of even number of a's and odd number of b's either:
A的偶数个数和奇数b的个数
- starts with an odd number of b's, and is followed by an even number of odd patterns amongst even patterns;
- 以奇数个b开头,然后在偶数模式中出现偶数个奇数模式;
- or starts with an even number of b's, and is followed by an odd number of odd patterns amongst even patterns.
- 或者从b的偶数开始,然后在偶数模式中出现奇数个奇数。
Note that even has no incidence on the evenness/oddness of the a/b's in the string.
注意,即使在字符串中a/b的偶数/奇数也没有关联。
regex -> (
正则表达式- >(
b (bb)* even* (odd even* odd)* even*
b (bb)*偶数*(奇数偶数)*偶数。
|
|
(bb)* even* odd even* (odd even* odd)* even*
(bb)*偶数*偶数* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
)
)
Of course one can replace every occurence of even and odd in the final regex to get a single regex.
当然,你可以在最后的正则表达式中替换所有出现的偶数和奇数,从而得到一个正则表达式。
It is easy to see that a string satisfying this regex will indeed have an even number of a's (as symbol a occurs only in even and odd subregexes, and these each use exactly two a's) and an odd number of b's (first case : 1 b + even number of b's + even number of odd; second case : even number of b's + odd number of odd).
很容易看到,一个字符串满足这个正则表达式将确实具有偶数个(作为符号只有在偶数和奇数subregexes发生,这些每次使用两个)和b的奇数(第一个案例:1 b + b的偶数+奇数偶数;第二种情况:偶数为b +奇数的奇数。
A string with an even number of a's and an odd number of b's will satisfy this regex as it starts with zero or more b's, then is followed by [one a, zero or more b's, one more a and zero or more b's], zero or more times.
一个偶数为A的字符串和奇数个b的值满足这个正则表达式,因为它从零或更多的b开始,然后是(1 A, 0或更多b, 1个A, 0或更多b), 0次或更多次。
#4
0
A high-level advice: construct a deterministic finite automaton for the language---very easy, encode parity of the number of a
s and b
s in the states, with q0
encoding even nr. of a
s and even nr. of b
s, and transition accordingly---, and then convert the DFA into a regular expression (either by using well-known algorithms for this or "from scratch").
高层的建议:构建一个确定性有限自动机的语言——-非常简单,编码平价,bs的数量在美国,与q0编码甚至nr.甚至nr. b,并相应地转变- - - - - -,然后DFA转换成一个正则表达式(通过使用著名的算法或“从头开始”)。
The idea here is to exploit the well-understood equivalence between the DFA (an algorithmic description of regular languages) and the regular expressions (an algebraic description of regular languages).
这里的想法是利用DFA(一种规则语言的算法描述)和正则表达式(一种正则语言的代数描述)之间的充分理解的等价性。
#5
0
This regular expression takes all strings with even number of a's and even number of b's
这个正则表达式包含所有的字符串,偶数为a,偶数为b。
r1=((ab+ba)(aa+bb)*(ab+ba)+(aa+bb))*
Now to get regular expression for even a's and odd b's
为了得到a和b的正则表达式。
r2=(b+a(aa+bb)*(ab+ba))((ab+ba)(aa+bb)*(ab+ba)+(aa+bb))*
#6
0
The regular expression are given below :
正则表达式如下:
(aa|bb)*((ab|ba)(aa|bb)*(ab|ba)(aa|bb)*b)*
#7
-1
the answer is (aa+ab+ba+bb)* b (aa+ab+ba+bb)*
答案是(aa+ab+ba+bb)* b (aa+ab+ba+bb)*。
#8
-1
(bb)* b (aa)* + (aa)* b (bb)*
(bb)* (aa)* + (aa)* b (bb)*。
This is the answer which handles all kind of strings with odd b's and even a's.
这是一个答案,它处理所有类型的字符串,包括奇数b,甚至a。
#9
-2
If it is even number of a's followed by odd number of b's (aa)*b(bb)* should work
如果是a的偶数个数,后面加上奇数b(aa)*b(bb)*应该起作用。
if it is in any order (aa)*b(bb)* + b(bb)(aa) should work
如果是在任何顺序(aa)*b(bb)* + b(bb)(aa)应该起作用。
#10
-2
All strings that have even no of a's and odd no of b's (((aa+bb) * b(aa+bb) * ) + (A +((a+b)b(a+b)) *)) *
所有的字符串,即使没有a和奇数的b((aa+bb) * b(aa+bb) *) +(a+ (a+b)b(a+b)) *。
here A is for null string. A can be neglected.
这里A是空字符串。一个可以被忽略的。
if there is any error plz point it out.
如果有任何错误,请指出来。
#1
16
One way to do this is to pass it through two regular expressions making sure they both match (assuming you want to use regular expressions at all, see below for an alternative):
一种方法是通过两个正则表达式来确保它们都匹配(假设您想要使用正则表达式,请参阅下面的选项):
^b*(ab*ab*)*$
^a*ba*(ba*ba*)*$
Anything else (and, in fact, even that) is most likely just an attempt to be clever, one that's generally a massive failure.
其他的(事实上,甚至是这样)很可能只是一个聪明的尝试,一个通常是巨大失败的尝试。
The first regular expression ensures there are an even number of a
with b
anywhere in the mix (before, after and in between).
第一个正则表达式确保在混合(before、after和in between)中有偶数个a和b。
The second is similar but ensures that there's an odd number of b
by virtue of the starting a*ba*
.
第二个是相似的,但是通过一个*ba*,确保有奇数个b。
A far better way to do it is to ignore regular expressions altogether and simply run through the string as follows:
一种更好的方法是完全忽略正则表达式,并简单地通过字符串运行如下:
def isValid(s):
set evenA to true
set oddB to false
for c as each character in s:
if c is 'a':
set evenA to not evenA
else if c is 'b':
set oddB to not oddB
else:
return false
return evenA and oddB
Though regular expressions are a wonderful tool, they're not suited for everything and they become far less useful as their readability and maintainability degrades.
虽然正则表达式是一种很好的工具,但是它们并不适合所有的东西,而且它们的可读性和可维护性也会降低。
For what it's worth, a single-regex answer is:
就其价值而言,一个单一的regex答案是:
(aa|bb|(ab|ba)(aa|bb)*(ba|ab))*(b|(ab|ba)(bb|aa)*a)
but, if I caught anyone on my team actually using a monstrosity like that, they'd be sent back to do it again.
但是,如果我发现我的团队中有人真的使用了这样的怪物,他们会被送回去再做一次。
This comes from a paper by one Greg Bacon. See here for the actual inner workings.
这是由格雷格·培根写的一篇论文。这里是实际的内部工作。
#2
1
(bb)*a(aa)*ab(bb)*
- (bb)*(aa)* ab(bb)*
ab(bb)* a(aa)*
- ab(bb)*(aa)*
-
b(aa)*(bb)*
.
.
.
.
.
. - b(aa)*(bb)* . .…
there can be many such regular expressions. Do you have any other condition like "starting with a" or something of the kind (other than odd 'b' and even 'a') ?
可以有很多这样的正则表达式。你是否有其他的条件,比如“以a开头”或其他类似的情况(除了奇数“b”,甚至“a”)?
#3
0
I would do as follows:
我会这样做:
- regex even matches the symbol a, then a sequence of b's, then the symbol a again, then another sequence of b's, such that there is an even number of b's:
- regex甚至匹配符号a,然后是b的序列,然后是符号a,然后是b的另一个序列,这样就有偶数个b:
even -> (a (bb)* a (bb)* | a b (bb)* a b (bb)*)
甚至-> (a (bb)* a (bb)* | a b (bb)* a b (bb)*)
- regex odd does the same with an odd total number of b's:
- regex奇数与奇数的b的总数相同:
odd -> (a b (bb)* a (bb)* | a (bb)* a b (bb)*)
奇数-> (bb)* a (bb)* | a (bb)* a b (bb)*)
A string of even number of a's and odd number of b's either:
A的偶数个数和奇数b的个数
- starts with an odd number of b's, and is followed by an even number of odd patterns amongst even patterns;
- 以奇数个b开头,然后在偶数模式中出现偶数个奇数模式;
- or starts with an even number of b's, and is followed by an odd number of odd patterns amongst even patterns.
- 或者从b的偶数开始,然后在偶数模式中出现奇数个奇数。
Note that even has no incidence on the evenness/oddness of the a/b's in the string.
注意,即使在字符串中a/b的偶数/奇数也没有关联。
regex -> (
正则表达式- >(
b (bb)* even* (odd even* odd)* even*
b (bb)*偶数*(奇数偶数)*偶数。
|
|
(bb)* even* odd even* (odd even* odd)* even*
(bb)*偶数*偶数* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
)
)
Of course one can replace every occurence of even and odd in the final regex to get a single regex.
当然,你可以在最后的正则表达式中替换所有出现的偶数和奇数,从而得到一个正则表达式。
It is easy to see that a string satisfying this regex will indeed have an even number of a's (as symbol a occurs only in even and odd subregexes, and these each use exactly two a's) and an odd number of b's (first case : 1 b + even number of b's + even number of odd; second case : even number of b's + odd number of odd).
很容易看到,一个字符串满足这个正则表达式将确实具有偶数个(作为符号只有在偶数和奇数subregexes发生,这些每次使用两个)和b的奇数(第一个案例:1 b + b的偶数+奇数偶数;第二种情况:偶数为b +奇数的奇数。
A string with an even number of a's and an odd number of b's will satisfy this regex as it starts with zero or more b's, then is followed by [one a, zero or more b's, one more a and zero or more b's], zero or more times.
一个偶数为A的字符串和奇数个b的值满足这个正则表达式,因为它从零或更多的b开始,然后是(1 A, 0或更多b, 1个A, 0或更多b), 0次或更多次。
#4
0
A high-level advice: construct a deterministic finite automaton for the language---very easy, encode parity of the number of a
s and b
s in the states, with q0
encoding even nr. of a
s and even nr. of b
s, and transition accordingly---, and then convert the DFA into a regular expression (either by using well-known algorithms for this or "from scratch").
高层的建议:构建一个确定性有限自动机的语言——-非常简单,编码平价,bs的数量在美国,与q0编码甚至nr.甚至nr. b,并相应地转变- - - - - -,然后DFA转换成一个正则表达式(通过使用著名的算法或“从头开始”)。
The idea here is to exploit the well-understood equivalence between the DFA (an algorithmic description of regular languages) and the regular expressions (an algebraic description of regular languages).
这里的想法是利用DFA(一种规则语言的算法描述)和正则表达式(一种正则语言的代数描述)之间的充分理解的等价性。
#5
0
This regular expression takes all strings with even number of a's and even number of b's
这个正则表达式包含所有的字符串,偶数为a,偶数为b。
r1=((ab+ba)(aa+bb)*(ab+ba)+(aa+bb))*
Now to get regular expression for even a's and odd b's
为了得到a和b的正则表达式。
r2=(b+a(aa+bb)*(ab+ba))((ab+ba)(aa+bb)*(ab+ba)+(aa+bb))*
#6
0
The regular expression are given below :
正则表达式如下:
(aa|bb)*((ab|ba)(aa|bb)*(ab|ba)(aa|bb)*b)*
#7
-1
the answer is (aa+ab+ba+bb)* b (aa+ab+ba+bb)*
答案是(aa+ab+ba+bb)* b (aa+ab+ba+bb)*。
#8
-1
(bb)* b (aa)* + (aa)* b (bb)*
(bb)* (aa)* + (aa)* b (bb)*。
This is the answer which handles all kind of strings with odd b's and even a's.
这是一个答案,它处理所有类型的字符串,包括奇数b,甚至a。
#9
-2
If it is even number of a's followed by odd number of b's (aa)*b(bb)* should work
如果是a的偶数个数,后面加上奇数b(aa)*b(bb)*应该起作用。
if it is in any order (aa)*b(bb)* + b(bb)(aa) should work
如果是在任何顺序(aa)*b(bb)* + b(bb)(aa)应该起作用。
#10
-2
All strings that have even no of a's and odd no of b's (((aa+bb) * b(aa+bb) * ) + (A +((a+b)b(a+b)) *)) *
所有的字符串,即使没有a和奇数的b((aa+bb) * b(aa+bb) *) +(a+ (a+b)b(a+b)) *。
here A is for null string. A can be neglected.
这里A是空字符串。一个可以被忽略的。
if there is any error plz point it out.
如果有任何错误,请指出来。