You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

设计确定性有限自动机(DFA):接受a、b数量均被3整除的字符串

Alright, let's figure out how to build this DFA that accepts strings of as and bs where both the count of as and bs are divisible by 3. Here's a step-by-step breakdown:

Design a DFA for Strings with Count of a and b Divisible by 3

1. Core Idea & State Definition

The key here is tracking two values at the same time: the number of as modulo 3, and the number of bs modulo 3. Since each count can have 3 possible remainders (0, 1, 2), we'll have a total of 3×3=9 states. Each state is represented as a tuple (r_a, r_b) where:

  • r_a: Remainder when the number of as so far is divided by 3

  • r_b: Remainder when the number of bs so far is divided by 3

  • Initial State: (0, 0) (we start with zero as and zero bs, so both remainders are 0)

  • Accept State: Only (0, 0) (we only accept the string if, after processing all characters, both remainders are 0)

2. Transition Rules

For every state (r_a, r_b), we define transitions for input a and b:

  • When we read an a: Increment the a remainder by 1, then take modulo 3. The b remainder stays the same. So the new state is ((r_a + 1) % 3, r_b)
  • When we read a b: Increment the b remainder by 1, then take modulo 3. The a remainder stays the same. So the new state is (r_a, (r_b + 1) % 3)

Transition Table

Here's a clear table summarizing all state transitions:

Current StateInput aInput b
(0,0)(1,0)(0,1)
(1,0)(2,0)(1,1)
(2,0)(0,0)(2,1)
(0,1)(1,1)(0,2)
(1,1)(2,1)(1,2)
(2,1)(0,1)(2,2)
(0,2)(1,2)(0,0)
(1,2)(2,2)(1,0)
(2,2)(0,2)(2,0)

3. Example Walkthrough

Let's test your sample string: abaabbbbb
We'll track the state transitions step by step:

  1. Start at (0,0)
  2. Read a → move to (1,0)
  3. Read b → move to (1,1)
  4. Read a → move to (2,1)
  5. Read a → move to (0,1) (since 2+1=3, 3%3=0)
  6. Read b → move to (0,2)
  7. Read b → move to (0,0) (since 2+1=3, 3%3=0)
  8. Read b → move to (0,1)
  9. Read b → move to (0,2)
  10. Read b → move to (0,0)

We end up in the accept state (0,0), so this string is correctly accepted by the DFA.

4. Textual DFA Flow

If you want a quick simplified view of the transitions:

(0,0) --a--> (1,0) --a--> (2,0) --a--> (0,0)
(0,0) --b--> (0,1) --b--> (0,2) --b--> (0,0)
(1,0) --b--> (1,1) --a--> (2,1) --a--> (0,1)
(1,1) --b--> (1,2) --a--> (2,2) --a--> (0,2)
(2,0) --b--> (2,1) --b--> (2,2) --b--> (2,0)

内容的提问来源于stack exchange,提问作者Amogh Joshi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:34:20