DFA 确定状态有限自动机
NFA 非确定状态有限自动机
目标实现如下的简单DFA的实现
代码如下:
'''
DFA 的实现
-->0 --a-->1 --a--> [2]
b b a,b
表如下
---------------------------------
|状态\字符| a | b |
--------------------------------
|0 | 1 |0 |
---------------------------------
|1 | 2 |1 |
---------------------------------
|2 | 2 |2 |
---------------------------------
'''
class DFA:
def __init__(self):
self.dfa={
"0":{
"a":"1",
"b":"0"
},
"1":{
"a":"2",
"b":"1"
},
"2":{
"a":"2",
"b":"2"
}
}
#接受的状态是
self.accept_status=["a","b"]
#当前状态
self.start="0"
self.accept="2"
#读取状态转移到另外的状态
def read(self,str):
if str in self.dfa[self.start]:
self.start=self.dfa[self.start][str]
def is_accept(self):
return self.start ==self.accept
def start_dfa(strs):
dfa=DFA()
for str in strs:
dfa.read(str)
return dfa.is_accept()
print(start_dfa("bbbbbbabca"))