Grokking Algorithms



Download 6,4 Mb.
Pdf ko'rish
bet89/120
Sana21.12.2022
Hajmi6,4 Mb.
#893167
1   ...   85   86   87   88   89   90   91   92   ...   120
Bog'liq
Grokking Algorithms An Illustrated Guide for Programmers and Other

Chapter 9
 
 
I
 
 
Dynamic programming
The solution
Here’s th
e inal grid.
Here’s my formula for illing in each cell.
Here’s how the formula looks in pseudocode:
if
word_a[i] == word_b[j]:
The letters match.
cell[i][j] = cell[i-1][j-1] + 1
else

The letters don’t match.
cell[i][j] = 0 


183
Longest common substring
Here’s the grid for 
hish vs. vista.
One thing to note: for this problem, th
e inal solution may not be in 
the last cell! For the knapsack problem, this last cell always had the 
inal solution. But for the longest common substring, the solution is the 
largest number in the grid—and it may not be the last cell.
Let’s go back to the original question: which string has more in 
common with 
hish? hish
and 
ish
have a substring of three letters in 
common. 
hish
and 
vista
have a substring of two letters in common. 
Alex probably meant to type 
ish
.
Longest common subsequence
Suppose Alex accidentally searched for 
fosh
. Which word did he mean: 
ish
or
fort

Let’s compare them using the longest-common-substring formula.


184
Chapter 9
 
 
I
 
 
Dynamic programming
hey’re both the same: two letters! But
fosh
is closer to 
ish
.
You’re comparing the longest common 
substring
, but you really need 
to compare the longest common 
subsequence
: the number of letters in 
a sequence that the two words have in common. How do you calculate 
the longest common subsequence? 
Here’s the partial grid for 
ish
and
fosh
.
Can you igure out the formula for this grid? he longest common 
subsequence is very similar to the longest common substring, and 
the formulas are pretty similar, too. Try to solve it yourself—I give the 
answer next.

Download 6,4 Mb.

Do'stlaringiz bilan baham:
1   ...   85   86   87   88   89   90   91   92   ...   120




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish