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}.
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).
The second is similar but ensures that there's an odd number of b
by virtue of the starting a*ba*
.
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:
(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.
Even-Even = (aa+bb+(ab+ba)(aa+bb)*(ab+ba))*
(Even-Even has even number of Aas and b's both)
Even a's and odd b's = Even-Even b Even-Even
This hsould work
(bb)*a(aa)*ab(bb)*
ab(bb)* a(aa)*
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') ?
For even number of a's and b's , we have regex:
E = { (ab + ba) (aa+bb)* (ab+ba) }*
For even number of a
's and odd number of b
's , all we need to do is to add an extra b
in the above expression E
.
The required regex will be:
E = { ((ab + ba) (aa+bb)* (ab+ba))* b ((ab + ba) (aa+bb)* (ab+ba))* }
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:
even -> (a (bb)* a (bb)* | a b (bb)* a b (bb)*)
- regex odd does the same with an odd total number of b's:
odd -> (a b (bb)* a (bb)* | a (bb)* a b (bb)*)
A string of even number of a's and odd number of b's either:
- starts with an odd number of b's, and is followed by an even number of odd patterns amongst even patterns;
- or starts with an even number of b's, and is followed by an odd number of odd patterns amongst even patterns.
Note that even has no incidence on the evenness/oddness of the a/b's in the string.
regex -> (
b (bb)* even* (odd even* odd)* even*
|
(bb)* even* odd even* (odd even* odd)* even*
)
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).
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 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").
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).
This regular expression takes all strings with even number of a's and even number of b's
r1=((ab+ba)(aa+bb)*(ab+ba)+(aa+bb))*
Now to get regular expression for even a's and odd b's
r2=(b+a(aa+bb)*(ab+ba))((ab+ba)(aa+bb)*(ab+ba)+(aa+bb))*
The regular expression are given below :
(aa|bb)*((ab|ba)(aa|bb)*(ab|ba)(aa|bb)*b)*
The structured way to do it is to make one transition diagram and build the regular expression from it. The regex in this case will be
(a((b(aa)*b)*a+b(aa)*ab)+b((a(bb)*a)*b+a(bb)*ba))b(a(bb)*a)*
It looks complicated but it covers all possible cases that may arise.
the answer is (aa+ab+ba+bb)* b (aa+ab+ba+bb)*
(bb)* b (aa)* + (aa)* b (bb)*
This is the answer which handles all kind of strings with odd b's and even a's.
If it is even number of a's followed by odd number of b's (aa)*b(bb)* should work
if it is in any order (aa)*b(bb)* + b(bb)(aa) should work
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)) *)) *
here A is for null string. A can be neglected.
if there is any error plz point it out.
来源:https://stackoverflow.com/questions/3698625/regular-expression-for-strings-with-even-number-of-as-and-odd-no-of-bs