📜  设计确定性有限自动机(第 10 组)

📅  最后修改于: 2021-09-28 09:33:58             🧑  作者: Mango

先决条件:设计有限自动机
在本文中,我们将看到一些确定性有限自动机 (DFA) 的设计。
问题 1:构造一个接受 {a} 上的字符串集的最小 DFA,其中 {a n | n≥0,n≠2,即’n’应大于0且不等于2}。
说明:所需的语言将类似于:

L1 = {ε, a, aaa, aaaa, aaaaa, ..................}

这里 ε 被视为字符串,因为 ‘n’ 的值大于或等于 0 并且其余字符串具有 ‘a’ 的任何正自然数的幂但不是 2。
此 DFA 不接受以下语言,因为某些字符串包含 ‘a’ 的 2 次方。

L2 = {aa, aaaaa, ..........}

这个语言 L2 不被这个所需的 DFA 接受,因为它的字符串包含 ‘a’ 的 2 次方。
所需语言的状态转换图如下所示:

在上面的 DFA 中,初始和最终状态 ‘W’ 在获得 ‘a’ 作为输入时转换为最终状态 ‘X’。将 ‘a’ 作为输入的最终状态 ‘X’ 转换为状态 ‘Y’。将“a”作为输入时的状态“Y”转换为最终状态“Z”,在获得任意数量的“a”时,它保持自身状态。

Python实现:

Python3
def stateW(n):
    #if length of n become 0
    #then print accepted
    if(len(n)==0):
        print("string accepted")
         
    else:
        #if at zero index
        #'a' found call
        #stateX function   
        if (n[0]=='a'):
            stateX(n[1:])
        
             
         
def stateX(n):
    #if length of n become 0
    #then print accepted
    if(len(n)==0):
        print("string accepted")
         
    else: 
        #if at zero index
        #'a' found call
        #stateY function   
        if (n[0]=='a'):
            stateY(n[1:])
         
def stateY(n):
    #if length of n become 0
    #then print not accepted
    if(len(n)==0):
        print("string not accepted")
         
    else:
        #if at zero index
        #'a' found call
        #stateZ function   
        if (n[0]=='a'):
            stateZ(n[1:])
         
def stateZ(n):
    #if length of n become 0
    #then print accepted
    if(len(n)==0):
        print("string accepted")
         
    else:
        #if at zero index
        #'a' found call
        #stateZ function   
        if (n[0]=='a'):
            stateZ(n[1:])
             
#take input
n=input()
 
#call stateW function
#to check the input
stateW(n)


Python3
def stateA(n):
    #if length of n become 0
    #then print accepted
    if(len(n)==0):
        print("string accepted")
         
    else:
        #if at zero index
        #'a' found call
        #stateB function   
        if (n[0]=='a'):
            stateB(n[1:])
        
             
         
def stateB(n):
    #if length of n become 0
    #then print accepted
    if(len(n)==0):
        print("string accepted")
         
    else: 
        #if at zero index
        #'a' found call
        #stateC function   
        if (n[0]=='a'):
            stateC(n[1:])
         
def stateC(n):
    #if length of n become 0
    #then print not accepted
    if(len(n)==0):
        print("string not accepted")
         
    else:
        #if at zero index
        #'a' found call
        #stateD function   
        if (n[0]=='a'):
            stateD(n[1:])
         
def stateD(n):
    #if length of n become 0
    #then print accepted
    if(len(n)==0):
        print("string accepted")
         
    else:
        #if at zero index
        #'a' found call
        #stateE function   
        if (n[0]=='a'):
            stateE(n[1:])
 
def stateE(n):
    #if length of n become 0
    #then print not accepted
    if(len(n)==0):
        print("string not accepted")
         
    else:
        #if at zero index
        #'a' found call
        #stateF function   
        if (n[0]=='a'):
            stateF(n[1:])
             
def stateF(n):
    #if length of n become 0
    #then print accepted
    if(len(n)==0):
        print("string accepted")
         
    else:
        #if at zero index
        #'a' found call
        #stateF function   
        if (n[0]=='a'):
            stateF(n[1:])           
             
#take input
n=input()
 
#call stateA function
#to check the input
stateA(n)


问题 2:构造一个接受 {a} 上的字符串集的最小 DFA,其中 {a n | n≥0,n≠2,n≠4,即’n’应大于0且不等于2和4}。
说明:所需的语言将类似于:

L1 = {ε, a, aa, aaaaa, aaaaaa, .................. }

这里 ε 被视为字符串,因为 ‘n’ 的值大于或等于 0,其余的字符串具有 ‘a’ 的任何正自然数的幂,但不是 2 和 4。
此 DFA 不接受以下语言,因为某些包含 ‘a’ 的 2 和 4 次幂的字符串。

L2 = {aa, aaaaa, aaaaaaaaaa, ............. }

所需语言的状态转换图如下所示:

在上面的 DFA 中,初始和最终状态 ‘A’ 在获得 ‘a’ 作为输入时它会转换为最终状态 ‘B’。将“a”作为输入时的最终状态“B”转换为状态“C”。将“a”作为输入时的状态“C”会转换为最终状态“D”。将“a”作为输入时的最终状态“D”转换为状态“E”。将“a”作为输入时的状态“E”会转换为最终状态“F”。将 ‘a’ 作为输入的最终状态 ‘F’ 保持在自身状态。

Python实现:

蟒蛇3

def stateA(n):
    #if length of n become 0
    #then print accepted
    if(len(n)==0):
        print("string accepted")
         
    else:
        #if at zero index
        #'a' found call
        #stateB function   
        if (n[0]=='a'):
            stateB(n[1:])
        
             
         
def stateB(n):
    #if length of n become 0
    #then print accepted
    if(len(n)==0):
        print("string accepted")
         
    else: 
        #if at zero index
        #'a' found call
        #stateC function   
        if (n[0]=='a'):
            stateC(n[1:])
         
def stateC(n):
    #if length of n become 0
    #then print not accepted
    if(len(n)==0):
        print("string not accepted")
         
    else:
        #if at zero index
        #'a' found call
        #stateD function   
        if (n[0]=='a'):
            stateD(n[1:])
         
def stateD(n):
    #if length of n become 0
    #then print accepted
    if(len(n)==0):
        print("string accepted")
         
    else:
        #if at zero index
        #'a' found call
        #stateE function   
        if (n[0]=='a'):
            stateE(n[1:])
 
def stateE(n):
    #if length of n become 0
    #then print not accepted
    if(len(n)==0):
        print("string not accepted")
         
    else:
        #if at zero index
        #'a' found call
        #stateF function   
        if (n[0]=='a'):
            stateF(n[1:])
             
def stateF(n):
    #if length of n become 0
    #then print accepted
    if(len(n)==0):
        print("string accepted")
         
    else:
        #if at zero index
        #'a' found call
        #stateF function   
        if (n[0]=='a'):
            stateF(n[1:])           
             
#take input
n=input()
 
#call stateA function
#to check the input
stateA(n)