2
8-3
(a) Let
f
[
i,j
] denote the length of the matching substring that ends at the
i
th element of
X
and the jthelement of
Y
. then the recurrence function is defined as:
f
[
i,j
] =
f
[
i
−
1
,j
−
1] + 1 if
X
[
i
] ==
Y
[
i
]
f
[
i,j
] = 0 if
X
[
i
]! =
Y
[
i
]Build the matrix in
O
(
mn
) time. Search though the matrix for the largest value
k
in
O
(
mn
) time. If it’s at (
i,j
) of the matrix, output
X
[
i
−
k
] to
X
[
i
].(b) For every element in
X
search in
Y
for th longest substring that ends or starts from that element.
8-7
(a) 7 ways, 2(10),1(10)+1(6)+4(1),1(10)+10(1),3(6)+2(1),2(6)+8(1),1(6)+14(1),20(1)(b)
C
(
n,d
) =
C
(
n,d
−
1) if
d
∈ {
d
1
,...,d
k
}
C
(
n,d
) =
C
(
n,d
−
1) +
C
(
n
−
d,d
) if
d
∈ {
d
1
,...,d
k
}
C
(
n,
0) = 0It take
O
(
nd
k
) time, as for each
C
(
n,d
),it can be calculated in
O
(1) time.2
Leave a Comment