简体   繁体   English

{0,1} Every5-Two0 的确定性有限自动机

[英]Deterministic Finite Automata for {0,1} Every5-Two0

Question:问题:

Define a DFA that accepts all strings over {0,1} such that every block of five consecutive positions contains at least two 0s.定义一个 DFA,它接受超过 {0,1} 的所有字符串,这样五个连续位置的每个块至少包含两个 0。 Please read the question carefully.请仔细阅读问题。 Ask yourselves: Does this allow e (epsilon (empty string)) to be accepted?问问自己:这是否允许接受 e(epsilon(空字符串))? How about 0101? 0101呢? Such English descriptions are found in various books, and I want to make sure you know how to read and interpret.这样的英文描述在各种书籍中都可以找到,我想确保你知道如何阅读和解释。

Instructor Hint: "The "blocks of 5" DFA can be programmatically generated without much trouble. I did it both ways (by hand and programmatically). Because I'm good with Emacs and Keyboard Macros, I could do even the 'by hand' mechanically and quite fast. But programmatic is less error-prone and compact."指导员提示:““5 块”DFA 可以通过编程方式生成而不会遇到太多麻烦。我用两种方式(手动和编程方式)完成了。因为我很擅长 Emacs 和键盘宏,所以我什至可以“手动” ’机械且相当快。但程序化不太容易出错且紧凑。”


I'm drawing this thing out, and I think I'm doing it wrong, as it is getting out of control.我正在画出这个东西,我认为我做错了,因为它正在失控。

My sketch of the DFA before I make it in python:我在 python 中制作 DFA 之前的草图:在此处输入图像描述

However, this isn't right, because index 2, 3, 4, 5, and 6 constitute a block of five consecutive positions, so I need to account for at least two zeroes in that.然而,这是不对的,因为索引 2、3、4、5 和 6 构成了五个连续位置的块,所以我需要在其中考虑至少两个零。 Oh, great, and I have been thinking it needs two 1s, not two 0s.哦,太好了,我一直认为它需要两个 1,而不是两个 0。 Am I going about this the entirely wrong way?我会以完全错误的方式解决这个问题吗? Because the way I'm thinking, this is going to have a huge amount of states.因为按照我的想法,这将有大量的状态。

(goes back to drawing this large DFA) (回到绘制这个大型 DFA)

The way I would go about this would be to define a state for each possible 5-bit string, representing the last 5 bits seen. 我要解决的方法是为每个可能的5位字符串定义一个状态,代表最后看到的5位。 Start off at the state representing 00000, move from state to state naturally, and mark each state with 2 or more zeroes as accepting. 从代表00000的状态开始,自然地从一个状态移到另一个状态,并用2个或多个零标记每个状态为可接受。

The way I would go about this would be to define a state for each possible 5-bit string, representing the last 5 bits seen. 我要解决的方法是为每个可能的5位字符串定义一个状态,代表最后看到的5位。 Start off at the state representing 00000, move from state to state naturally, and mark each state with more than 2 zeroes as accepting. 从代表00000的状态开始,自然地从一个状态移到另一个状态,并将每个状态标记为2个以上的零作为接受状态。

If you want to do it the old school way: 如果您想以传统方式进行:

def check(s):
    buffer = s[:5]
    i = 5
    count0, count1 = 0, 0
    while i < len(s):
        if len(buffer) == 5:
            first = buffer[0]
            if first == '0':
                count0 -= 1
            else:
                count1 -= 1
            buffer = buffer[1:]
        buffer += s[i]
        if buffer[-1] == '0':
            count0 += 1
        else:
            count1 += 1
        if count0 < 2:
            return "REJECT"
        i += 1
    if buffer.count('0') >= 2:
        return "ACCEPT"
    else:
        return "REJECT"

A slightly smarter way: 一种更聪明的方法:

def check(s):
    return all(ss.count('0')>=2 for ss in (s[i:i+5] for i in xrange(len(s)-4)))

The verbose code of the above method: 上述方法的详细代码:

def check(s):
    subs = (s[i:i+5] for i in xrange(len(s)-4))
    for sub in subs:
        if sub.count('0') < 2:
            return "REJECT"
    return "ACCEPT"

Haven't tested this code, but it should likely work. 尚未测试此代码,但它应该可以工作。 Your professor probably wants the third method. 您的教授可能想要第三种方法。

Hope this helps 希望这可以帮助

There are actually 11 16 states, including the single rejecting state. 实际上有 11 16个状态,包括单个拒绝状态。 The states correspond to the up-to-four-character histories, truncated at the second most-recent zero. 这些状态对应于最多四个字符的历史记录,在最近的第二个零处被截断。 Only four characters are needed because the transition constitutes the fifth character in the block; 只需要四个字符,因为转换构成了块中的第五个字符; if the transition character is not a 0 and there are not two zeros in the four-character history, then the transition is to failure. 如果过渡字符不是0,并且在四个字符的历史记录中没有两个零,则过渡为失败。

I generated the transitions by hand, because it was faster to type than to write Python, so I'll leave the generalized (k, n) (k zeros in blocks of n) problem as a coding exercise. 我手动生成转换,因为键入比编写Python更快,所以我将广义(k,n)(n个块中的k个零)留给编码练习。 (I inserted x's into the state names to make it line up better.) (我将x插入状态名称以使其排列更好。)

sxx00 (0)->sxx00 (1)->sx001
sx001 (0)->sx010 (1)->s0011
sx010 (0)->sxx00 (1)->s0101
s0011 (0)->s0110 (1)->s0111
s0101 (0)->sx010 (1)->s1011
s0110 (0)->sxx00 (1)->s1101
s0111 (0)->s1110 (1)->sFAIL
s1011 (0)->s0110 (1)->sFAIL
s1101 (0)->sx010 (1)->sFAIL
s1110 (0)->sxx00 (1)->sFAIL
sFAIL (0)->sFAIL (1)->sFAIL

[EDIT]: That was actually not quite correct, because (as I read the question), the string '1111' should be accepted. [编辑]:这实际上不是很正确,因为(在我阅读问题时),应该接受字符串'1111'。 (Every five-character block in it has two zeros, trivially, since there are no five-character blocks.) So there are some additional start-up states: (由于没有五个字符块,因此每个五个字符块都有两个零,这很简单。)因此,还有一些其他的启动状态:

start (0)->sxx00 (1)->s1
s1    (0)->sx010 (1)->s11
s11   (0)->s0110 (1)->s111
s111  (0)->s1110 (1)->s1111
s1111 (0)->sFAIL (1)->sFAIL

That last state, which looks a lot like sFAIL, is different because it is an accepting state. 最后一个状态看起来很像sFAIL,它有所不同,因为它是一个接受状态。

每 5 个连续的字母在一个单词中至少有两个 0 (2) <=> w 不包含四个连续的 1。

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

相关问题 确定性有限自动机 (DFA) 实施 - Deterministic Finite Automata (DFA) Implementation Python 中 {ab, abc}* 的非确定性有限自动机 - Non-Deterministic Finite Automata of {ab, abc}* in Python 验证确定性有限自动机转换函数对于每个唯一输入字母都恰好具有一个输出状态 - validate that deterministic finite automata transition function has exactly one output state for each unique input alphabet Python有限自动机库 - Python Finite Automata library 有限自动机是如何在代码中实现的? - How are finite automata implemented in code? 有限状态自动机(FSA):重复输出问题 - Finite State Automata (FSA): Repeated output problem L = a^nb^n 的确定性下推自动机 | n &gt;=0) Python 程序 - Deterministic Pushdown Automata for L = a^nb^n | n >=0) Python Program 根据 Python 中两行之间的匹配创建一个具有 [0,1] 的新列 - Create a new column with [0,1] based on match between two rows in Python 如何在 python 中的两个不同 colors 中显示来自两个标签 (0,1) 的数值? - How do I display numerical values that come from two labels (0,1) in two different colors in python? (0,1)-矩阵与(0,1)-矢量的相乘 - Multiplication of (0,1)-matrix with a (0,1)-vector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM