#include <stdlib.h>
#include <string.h>
#define edge(_) ((_) - 'a')
struct Node { int isEnd struct Node *tr[26] }
struct Node mc[100]
int main()
{
吵盯 mc[0].tr[edge('a')] = mc + 1
mc[0].tr[edge('b')] = mc + 2
mc[1].tr[edge('a')] = mc + 3
mc[1].tr[edge('b')] = mc + 2
mc[2].tr[edge('a')] = mc + 1
mc[2].tr[edge('b')] = mc + 3
mc[3].tr[edge('a')] = mc[3].tr[edge('b')] = mc + 3
mc[3].isEnd = 1
char buff[256]
while(gets(buff))
{
int i, curr = 0, len = strlen(buff)
for(i = 0 i < len ++i)
{
液枝 if(mc[curr].tr[edge(buff[i])] != NULL)
{
curr = mc[curr].tr[edge(buff[i])] - mc
}
升埋和 else break
}
printf(mc[curr].isEnd == 1 ? "Accept\n" : "Refuse\n")
}
return 0
}
有穷自动机,或有穷状态的机器,是描述(或“机器”)特定类型算法的数学方法。特别地,有穷自动机可用作描述在输入串中识别模式的过程,因此也能用作构造扫描程序。当然有穷做袭激自动机与正则表达式之间有着很密切的关系,在下一节中大家将会看到如何从正则表达式中构造有穷自动机。但在学习有穷自禅烂动机之前,先看一个说明的示例。通过下面的正则表达式可在程序设计语言中给出标识符模式的一般定义(假设已定义了letter 和digit):identifier = letter ( letter | digit ) *它代表以一个字母开头且其后为任意字母和/ 或数字序列的串。有穷自动机通过标明数字1 和2 的圆圈表示的是状态(state),它们表示其中记录已被发现的模式的数量在识别过程中的位置。带有箭头的线代表由记录一个状态向另一个状态的转换(transition),该转换依赖纯袜于所标字符的匹配。有穷自动机又分为确定型的有穷自动机(DFA)与非确定型的有穷自动机(NFA)两种。http://www.douban.com/note/74804011/
public class Model {Integer a[]
Integer b[]
Integer rule[]
Integer TIME_MAX = 41
Integer NUM_OF_CELL = 41
public Model() {
a = new Integer[NUM_OF_CELL]
b = new Integer[NUM_OF_CELL]
rule = new Integer[8]
rule[0] = 0
rule[1] = 1
rule[2] = 1
rule[3] = 0
rule[4] = 1
rule[5] = 1
rule[6] = 0
rule[7] = 0
for (int i = 0i <NUM_OF_CELLi++) {
a[i] = 0
}
a[NUM_OF_CELL / 2] = 1
}
public static void main(String[] args) {
Model sm = new Model ()
sm.doIt()
}
private void doIt() {
String str = ""
for (int t = 0t <TIME_MAXt++) {
for (int i = 0i <NUM_OF_CELLi++) {
b[i] = function(a[(NUM_OF_CELL + i - 1) % NUM_OF_CELL],a[i],a[(i + 1) % NUM_OF_CELL])
if (a[i] == 0) {
str = "#"
} else {
str = "*"
}
System.out.print(str + " ")
}
System.out.println("")
for (int j = 0j <NUM_OF_CELLj++) {
a[j] = b[j]
}
}
}
private Integer function(Integer i1, Integer i2, Integer i3) {
if (i1 == 0 &i2 == 0 &i3 == 0) {
return rule[0]
}
if (i1 == 0 &i2 == 0 &i3 == 1) {
return rule[1]
}
if (i1 == 0 &i2 == 1 &i3 == 0) {
return rule[2]
}
if (i1 == 0 &i2 == 1 &i3 == 1) {
return rule[3]
}
if (i1 == 1 &i2 == 0 &i3 == 0) {
return rule[4]
}
if (i1 == 1 &i2 == 0 &i3 == 1) {
return rule[5]
}
if (i1 == 1 &i2 == 1 &i3 == 0) {
return rule[6]
}
if (i1 == 1 &i2 == 1 &i3 == 1) {
return rule[7]
}
return 0
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)