example=[]
example.append(input("请输入第一个样例"))
example.append(input("请输入第二个样例"))
example.append(input("请输入第三个样例"))
for i in range(len(example)):
value=1;
if len(example[i])%2==1:
print('字符串不可以被有限自动机接受')
value=0;
if(value==0):
continue
for j in range(len(example[i])):
#print('@',i,'@')
if example[i][j]!='1' and example[i][j]!='0':
print('字符串中的字符不在输入符号集中')
value=0;
break
if(value==0):
continue
status =1
road=[]
for j in range(len(example[i])):
road.append(status)
if status == 1:
if int(example[i][j]) == 1:
status = 2
else:
status = 4
elif status == 2:
if int(example[i][j]) == 1:
status = 1
else:
status = 3
elif status == 3:
if int(example[i][j]) == 1:
status = 4
else:
status = 2
elif status == 4:
if int(example[i][j]) == 1:
status = 3
else:
status = 1
road.append(status)
print('状态序列为:')
for j in range(len(road)):
if(road[j]==1):
print('q0',end='')
elif(road[j]==2):
print('q1',end='')
elif(road[j]==3):
print('q3',end='')
elif(road[j]==4):
print('q2',end='')
if(j!=len(road)-1):
print('->',end='')
print(' ')
road.clear()
记录小作业nlp 超简单自动机的代码实现
于 2024-04-28 20:27:49 首次发布