我想编写一个非确定性自动机,该自动机接受使用Python以'01'结尾的字符串。我想跟踪输入可能离开的不同状态。以'01'结尾的字符串的NFA如下: enter image description here
例如,如果我想在Q0的前一个0处评估字符串'00101',我想遵循状态Q0和Q1。在不知不觉中,我已经使用NFA作为列表和具有False值的子列表的元素来实现功能,以破坏自动机的过程。但是我不知道如何解决采用代表Q0的子列表的两个元素来划分过程的问题。
def nfa(string):
nfa01l =[ [[0,1],0],[False,2],[False,False]]
while True:
for c in string:
if c == "0" and state == 0:
state = nfa01l[state][0]
elif c == "1" and state == 0:
state = nfa01l[state][1]
elif c == "0" and state == 1:
state = nfa01l[state][0]
elif c == "1" and state == 1:
state = nfa01l[state][1]
elif c == "0" and state == 2:
state = nfa01l[state][0]
elif c == "1" and state == 2:
state = nfa01l[state][1]
我才刚刚开始学习编程。我希望对此有适当的解释。 谢谢!