偶数个 1 的位串的正则表达式

发布于 2024-08-30 10:21:37 字数 271 浏览 1 评论 0原文

设 L= { w in (0+1)* | w有偶数个1},即L是所有有偶数个1的比特串的集合。下面哪一个正则表达式代表 L?

A) (0*10*1)*
B) 0*(10*10*)*
C) 0*(10*1)* 0*
D) 0*1(10*1)* 10*

根据我的说法,选项 D 永远不会正确,因为它不代表零个 1 的位串。但其他选择又如何呢?我们关心的是 1 的数量(偶数与否),而不是 0 的数量。

那么哪个是正确的选项,为什么?

Let L= { w in (0+1)* | w has even number of 1s}, i.e. L is the set of all bit strings with even number of 1s. Which one of the regular expressions below represents L?

A) (0*10*1)*
B) 0*(10*10*)*
C) 0*(10*1)* 0*
D) 0*1(10*1)* 10*

According to me option D is never correct because it does not represent the bit string with zero 1s. But what about the other options? We are concerned about the number of 1s(even or not) not the number of zeros doesn't matter.

Then which is the correct option and why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

゛时过境迁 2024-09-06 10:21:37

A 如果为假。它不会与 0110(或任何仅零的非空字符串)匹配,

B 表示 OK。由于页边距太小,我不想在这里证明这一点。

C 不会与 010101010 匹配(中间的零不匹配)

D 正如你所说,不会与 00 或任何其他没有匹配的 # 匹配。

所以只有B

A if false. It doesn't get matched by 0110 (or any zeros-only non-empty string)

B represents OK. I won't bother proving it here since the page margins are too small.

C doesn't get matched by 010101010 (zero in the middle is not matched)

D as you said doesn't get matched by 00 or any other # with no ones.

So only B

未央 2024-09-06 10:21:37

要解决此类问题,您应该

  1. 为所有“不正确”的正则表达式提供反例模式。这将是 L 中不匹配的字符串,或者是 L 中的匹配字符串。
  2. 为了证明剩余的“正确”模式,您应该回答两个问题:

    • 每个与模式匹配的字符串都属于L吗?这可以通过设计每个匹配字符串应满足的属性来完成 - 例如,某些字符出现的次数......
    • L 中的每个字符串都与正则表达式匹配吗?这是通过将 L 划分为易于分析的子类,并显示每个子类以自己的方式匹配模式来完成的。

(由于[家庭作业],没有具体答案)。

To solve such a problem you should

  1. Supply counterexample patterns to all "incorrect" regexps. This will be either a string in L that is not matched, or a matched string out of L.
  2. To prove the remaining "correct" pattern, you should answer two questions:

    • Does every string that matches the pattern belong to L? This can be done by devising properties each of matched strings should satisfy--for example, number of occurrences of some character...
    • Is every string in L matched by the regexp? This is done by dividing L into easily analyzable subclasses, and showing that each of them matches pattern in its own way.

(No concrete answers due to [homework]).

残疾 2024-09-06 10:21:37

检查模式 B

^0*(10*10*)*$

^          # match beginning of string
0*         # match zero or more '0'
(          # start group 1
 10*       # match '1' followed by zero or more '0'
 10*       # match '1' followed by zero or more '0'
)*         # end group 1 - match zero or more times
$          # end of string

很明显,该模式只会匹配具有 0,2,4,... 1 的字符串。

Examining the pattern B:

^0*(10*10*)*$

^          # match beginning of string
0*         # match zero or more '0'
(          # start group 1
 10*       # match '1' followed by zero or more '0'
 10*       # match '1' followed by zero or more '0'
)*         # end group 1 - match zero or more times
$          # end of string

Its pretty obvious that this pattern will only match strings who have 0,2,4,... 1's.

浅忆 2024-09-06 10:21:37

寻找应该匹配但不匹配的示例。 0110111100 应该全部匹配,但这四个之一都失败

Look for examples that should match but don't. 0, 11011, and 1100 should all match, but each one fails for one of those four

C 是不正确的,因为它不允许一组的第二个 1 和下一组的第一个 1 之间有任何 0。

C is incorrect because it does not allow any 0s between the second 1 of one group and the first 1 of the next group.

热情消退 2024-09-06 10:21:37

一个快速的Python脚本实际上消除了所有的可能性:

import re

a = re.compile("(0*10*1)*")
b = re.compile("0*(10*10*)*")
c = re.compile("0*(10*1)* 0*")
d = re.compile("0*1(10*1)* 10*")

candidates = [('a',a),('b',b),('c',c),('d',d)]
tests = ['0110', '1100', '0011', '11011']
for test in tests:
    for candidate in candidates:
        if not candidate[1].match(test):
            candidates.remove(candidate)
            print "removed %s because it failed on %s" % (candidate[0], test)

ntests = ['1', '10', '01', '010', '10101']
for test in ntests:
    for candidate in candidates:
        if candidate[1].match(test):
            candidates.remove(candidate)
            print "removed %s because it matched on %s" % (candidate[0], test)

输出:

  • 删除c因为它在0110上失败
  • 删除d因为它在0110上失败
  • 删除a因为它在1上匹配
  • 删除b因为它在10上匹配

a quick python script actually eliminated all the possibilities:

import re

a = re.compile("(0*10*1)*")
b = re.compile("0*(10*10*)*")
c = re.compile("0*(10*1)* 0*")
d = re.compile("0*1(10*1)* 10*")

candidates = [('a',a),('b',b),('c',c),('d',d)]
tests = ['0110', '1100', '0011', '11011']
for test in tests:
    for candidate in candidates:
        if not candidate[1].match(test):
            candidates.remove(candidate)
            print "removed %s because it failed on %s" % (candidate[0], test)

ntests = ['1', '10', '01', '010', '10101']
for test in ntests:
    for candidate in candidates:
        if candidate[1].match(test):
            candidates.remove(candidate)
            print "removed %s because it matched on %s" % (candidate[0], test)

the output:

  • removed c because it failed on 0110
  • removed d because it failed on 0110
  • removed a because it matched on 1
  • removed b because it matched on 10
温柔戏命师 2024-09-06 10:21:37

这个答案最适合这种语言

(0*10*10*)

This answer would be best for this language

(0*10*10*)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文