2 个版本
0.1.1 | 2022 年 4 月 19 日 |
---|---|
0.1.0 | 2022 年 4 月 19 日 |
#2362 在 算法
5KB
110 行
deterministic-finite-automaton
确定有限自动机的 Rust 实现
用法
// Accepts strings that contain 1 twice
let states = [0, 1, 2];
let alphabet = [0, 1];
let transition_fn = |s, c| match (s, c) {
(0, 0) => 0,
(0, 1) => 1,
(1, 0) => 1,
(1, 1) => 2,
(2, 0) => 2,
(2, 1) => 2,
_ => panic!("Invalid (state, char)"),
};
let start_state = 0
let accept_states = [2];
let dfa = DFA::new(
states,
alphabet,
transition_fn,
start_state,
accept_states,
);
assert!(dfa.input([1, 1, 0]) State::Accept(2));
assert!(dfa.input([0]) State::Reject(0));