偶数个 1 的位串的正则表达式
设 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
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
要解决此类问题,您应该
L
中不匹配的字符串,或者是L
中的匹配字符串。为了证明剩余的“正确”模式,您应该回答两个问题:
L
吗?这可以通过设计每个匹配字符串应满足的属性来完成 - 例如,某些字符出现的次数......L
中的每个字符串都与正则表达式匹配吗?这是通过将L
划分为易于分析的子类,并显示每个子类以自己的方式匹配模式来完成的。(由于[家庭作业],没有具体答案)。
To solve such a problem you should
L
that is not matched, or a matched string out ofL
.To prove the remaining "correct" pattern, you should answer two questions:
L
? This can be done by devising properties each of matched strings should satisfy--for example, number of occurrences of some character...L
matched by the regexp? This is done by dividingL
into easily analyzable subclasses, and showing that each of them matches pattern in its own way.(No concrete answers due to [homework]).
检查模式
B
:很明显,该模式只会匹配具有 0,2,4,...
1
的字符串。Examining the pattern
B
:Its pretty obvious that this pattern will only match strings who have 0,2,4,...
1
's.寻找应该匹配但不匹配的示例。
0
、11011
和1100
应该全部匹配,但这四个之一都失败Look for examples that should match but don't.
0
,11011
, and1100
should all match, but each one fails for one of those fourC 是不正确的,因为它不允许一组的第二个 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.
一个快速的Python脚本实际上消除了所有的可能性:
输出:
a quick python script actually eliminated all the possibilities:
the output:
这个答案最适合这种语言
This answer would be best for this language