[英]how would I build a generalized DFA for decimal Multiples of k?
Lets say I have a language like this: L_k:={w∈Σ∗: (w)10=i·k, i∈N}.假设我有这样的语言:L_k:={w∈Σ∗: (w)10=i·k, i∈N}。 now using k as a parameter
现在使用 k 作为参数
I now want to define a DFA, that accepts L_{k}- and proof then that this DFA is valid.我现在想定义一个接受 L_{k}- 的 DFA,然后证明这个 DFA 是有效的。 Important: k is a parameter, so the conditions and transition function is dependent on k.
重要提示:k 是一个参数,因此条件和转换 function 取决于 k。
I'd greatly appreciate any help!我将不胜感激任何帮助!
A simple solution is to store, in each state, the current and next remainder after division by k;一个简单的解决方案是在每个 state 中存储除以 k 后的当前余数和下一个余数;
the next remainder is after the one you would have after reading a 0.下一个余数是在你读到 0 之后的余数。
So you would use states S={(i,j): 0≤i,j<k\}
, where (0,0) is the initial state.因此,您将使用状态
S={(i,j): 0≤i,j<k\}
,其中 (0,0) 是初始 state。
After reading a digit q, you would move from (i,j)
to (j+q mod k, 10(j+q) mod k)
.读取数字 q 后,您将从
(i,j)
移动到(j+q mod k, 10(j+q) mod k)
。
Every state (0,j)
is final, as the current remainder is 0 there.每个 state
(0,j)
都是最终的,因为那里的当前余数为 0。
To prove that this is correct, let x
denote some decimal number with the next remainder j
.为了证明这是正确的,让
x
表示一些十进制数,下一个余数为j
。 So x0
has remainder j
.所以
x0
数j
。 If we read a digit other than 0, say q
, then we would have xq
, which has remainder j+q mod k
(follows immediately from modulo arithmetic, as xq-x0 = q), and the next remainder is the remainder of xq0 = xq * 10
, so simply (j+q)*10 mod k
.如果我们读取一个非 0 的数字,比如
q
,那么我们将得到xq
,它有余数j+q mod k
(紧跟模算术,如 xq-x0 = q),下一个余数是xq0 = xq * 10
的余数xq0 = xq * 10
,所以很简单(j+q)*10 mod k
。
So i is indeed the current remainder, and j the next remainder.所以 i 确实是当前的余数,而 j 是下一个余数。
The way I am reading this is you want a DFA that accepts the language 0, 4, 8, 12, 16, ..., 4n, ..., for k = 4, or 7, 14, 21, 28, ..., 7n, ... for k = 7.我读这篇文章的方式是你想要一个接受语言 0、4、8、12、16、...、4n、...,对于 k = 4 或 7、14、21、28 的 DFA。 .., 7n, ... 对于 k = 7。
To pull this off, your DFA will always start in an initial state corresponding to a remainder after division by k of 0. This state is always accepting since 0 is an integer multiple of any k.要实现这一点,您的 DFA 将始终从初始 state 开始,对应于除以 0 的 k 后的余数。此 state 始终接受,因为 0 是 Z157DB7DF530023575515D366C9B67 的任意倍数
Now, when we read one digit, what we are doing is updating our understanding of the number we've read so far as follows: the number we've understood so far is actually ten times greater than we thought, plus this digit.现在,当我们读到一个数字时,我们正在做的是更新我们对迄今为止所读到的数字的理解:到目前为止,我们所理解的数字实际上比我们想象的要大十倍,加上这个数字。 Let's say we're in a state that corresponds to a remainder after division by k equal to m, and the next digit we read is d.
假设我们在 state 中,对应于除以 k 后的余数等于 m,我们读取的下一个数字是 d。 Then:
然后:
m' = (10 * m + d) % k
We can get the remainder after division by k of the newly understood number by taking the remainder after division we've understood so far and transforming it.我们可以通过对我们目前已经理解的除法后的余数进行转换来获得新理解的数字除以 k 后的余数。 Because all we need to keep track of for this construction to work is the remainder after division by k of the number understood so far (we get d for free by reading input, and can forget it immediately), we only ever need exactly k states in our DFA.
因为要让这个结构起作用,我们只需要跟踪除以 k 后的余数(我们通过读取输入免费得到 d,并且可以立即忘记它),我们只需要恰好 k 个状态在我们的 DFA 中。
How does this look for k = 7?这如何寻找 k = 7?
m = 0, d = 0: m' = 0; transition from q0 to q0 on symbol 0
d = 1: m' = 1; transition from q0 to q1 on symbol 1
d = 2: m' = 2; transition from q0 to q2 on symbol 2
d = 3: m' = 3; transition from q0 to q3 on symbol 3
d = 4: m' = 4; transition from q0 to q4 on symbol 4
d = 5: m' = 5; transition from q0 to q5 on symbol 5
d = 6: m' = 6; transition from q0 to q6 on symbol 6
d = 7: m' = 0; transition from q0 to q0 on symbol 7
d = 8: m' = 1; transition from q0 to q1 on symbol 8
d = 9: m' = 2; transition from q0 to q2 on symbol 9
...
m = 6, d = 0: m' = 4; transition from q6 to q4 on symbol 0
d = 1: m' = 5; transition from q6 to q5 on symbol 1
d = 2: m' = 6; transition from q6 to q6 on symbol 2
d = 3: m' = 0; transition from q6 to q0 on symbol 3
d = 4: m' = 1; transition from q6 to q1 on symbol 4
d = 5: m' = 2; transition from q6 to q2 on symbol 5
d = 6: m' = 3; transition from q6 to q3 on symbol 6
d = 7: m' = 4; transition from q6 to q4 on symbol 7
d = 8: m' = 5; transition from q6 to q5 on symbol 8
d = 9: m' = 6; transition from q6 to q6 on symbol 9
We get a DFA with 7 states and 70 transitions.我们得到一个具有 7 个状态和 70 个转换的 DFA。 In general, this method will always give a DFA with k states and 10k transitions.
一般来说,这种方法总是会给出一个具有 k 个状态和 10k 个转换的 DFA。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.