23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

C-12.13<br />

Describe an efficient algorithm to f<strong>in</strong>d the longest pal<strong>in</strong>drome that is a suffix of<br />

a str<strong>in</strong>g T of length n. Recall that apal<strong>in</strong>drome is a str<strong>in</strong>g that is equal to its<br />

reversal. What is the runn<strong>in</strong>g time of your method?<br />

C-12.14<br />

Given a sequence S = (x 0 , x 1 , x 2 ,…, x n−1 ) of numbers, describe an O(n 2 )-time<br />

algorithm for f<strong>in</strong>d<strong>in</strong>g a longest subsequence T = (x i 0,x i 1, x i 2,…x i k−1) of numbers,<br />

such that i j < i j+1 <strong>and</strong> x ij > x ij+1 . That is, T is a longest decreas<strong>in</strong>g subsequence of<br />

S.<br />

C-12.15<br />

Def<strong>in</strong>e the edit distance between two str<strong>in</strong>gs X <strong>and</strong> Y of length n <strong>and</strong> m,<br />

respectively, to be the number of edits that it takes to change X <strong>in</strong>to Y. An edit<br />

consists of a character <strong>in</strong>sertion, a character deletion, or a character<br />

replacement. For example, the str<strong>in</strong>gs "algorithm" <strong>and</strong> "rhythm" have<br />

edit distance 6. Design an O(nm)-time algorithm for comput<strong>in</strong>g the edit distance<br />

between X <strong>and</strong> Y.<br />

C-12.16<br />

Design a greedy algorithm for mak<strong>in</strong>g change after someone buys some c<strong>and</strong>y<br />

cost<strong>in</strong>g x cents <strong>and</strong> the customer gives the clerk $1. Your algorithm should try<br />

to m<strong>in</strong>imize the number of co<strong>in</strong>s returned.<br />

a.<br />

Show that your greedy algorithm returns the m<strong>in</strong>imum number of co<strong>in</strong>s if<br />

the co<strong>in</strong>s have denom<strong>in</strong>ations $0.25, $0.10, $0.05, <strong>and</strong> $0.01.<br />

b.<br />

Give a set of denom<strong>in</strong>ations for which your algorithm may not return the<br />

m<strong>in</strong>imum number of co<strong>in</strong>s. Include an example where your algorithm fails.<br />

C-12.17<br />

Give an efficient algorithm for determ<strong>in</strong><strong>in</strong>g if a pattern P is a subsequence (not<br />

substr<strong>in</strong>g) of a text T. What is the runn<strong>in</strong>g time of your algorithm?<br />

C-12.18<br />

Let x <strong>and</strong> y be str<strong>in</strong>gs of length n <strong>and</strong> m respectively. Def<strong>in</strong>e B(i,j) to be the<br />

length of the longest common substr<strong>in</strong>g of the suffix of length i <strong>in</strong> x <strong>and</strong> the<br />

786

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!