Princeton Algorithms: Part 2 [week 5: Substring Search]

来源:互联网 发布:硬笔入门字帖 知乎 编辑:程序博客网 时间:2024/05/30 23:02

Exercise


Question Explanation

The correct answer is: 1 2 2 4 5 2 7 5


Here is the DFA:

0 1 2 3 4 5 6 7
----------------------
A 1 2 2 4 5 2 7 5
B 0 0 3 0 0 6 0 0
C 0 0 0 0 0 0 0 8


Question Explanation

The correct answer is: T Y T T H I N H


Here is a trace:

i j i+j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
----------------------------------------------------------------------------------------------------
P A N E W A T Y O U R F E E T T H I N K N O W A N D T H E N
0 6 6 O W A N D T H
1 6 7 O W A N D T H
8 6 14 O W A N D T H
9 6 15 O W A N D T H
10 4 14 O W A N D T H
11 6 17 O W A N D T H
18 6 24 O W A N D T H
21 0 21 O W A N D T H



Question Explanation

The correct answer is: 4


hash(3237816904) = 3237816904 (mod 53)
= 52

hash(1000000000) = 1000000000 (mod 53)
= 28

2378169046 = (3237816904 - 3*1000000000) * 10 + 6
hash(2378169046) = (3237816904 - 3*1000000000) * 10 + 6 (mod 53)
hash(2378169046) = (52 - 3*28) * 10 + 6 (mod 53)
= -49 (mod 53)
= -49 + 53 (mod 53)
= 4

Recall that the Java operator % is remainder, and not modulus. When taking
a dividend mod Q, the result is a value between 0 and Q-1; with remainder,
the result can be a negative value if the dividend is negative.


Interview Questions





0 0
原创粉丝点击