In Gutenberg's printing press, each line of text is assembled by placing individual metal letters in a rack, applying ink to the letters and then pressing them onto paper. Gutenberg needs to print N words using his printing press, one word at a time. The printing press allows the following operations: Add a letter to the end of the word currently in the rack. Remove the last letter from the word currently in the rack. Print the word currently in the rack.
Initially, the rack is empty; it contains no metal letters. At the end of printing, Gutenberg is allowed to leave letters in the rack. Also, he is allowed to print the words in any order that he likes. As each operation requires time, he wants to minimize the total number of operations. For instance, if Gutenberg is supposed to print out three words, {print, the, poem}, he could do it using 20 operations: add t, add h, add e, print, remove last letter (three times), add p, add o, add e, add m, print, remove last letter (three times), add r, add i, add n, add t, print. In each of the following cases, determine the minimum number of operations required to print out all the words in the set, in any order, one word at a time.
{ there, theirs, her, shore, three, tree, rest, hence, thorium, therefore, threshold }
[This is question from zonal Informatics Olympiad , India ]
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
classical bit mask dymanic programming, use d p [ 1 < < w ] [ w ] represt for the state ( d o n e , l w ) , 1 ′ s in d o n e ′ s binary representation shows words has beed printed, and the last printed word was l w ,
we enum the next word n w which hasn't beed printed, and built a array like d i s [ i ] [ j ] , represented for the number of operations to print word j after we have wrote word (\i ), which is the l e n g t h ( i ) + l e n g t h ( j ) − 2 ∗ p r e f i x ,
the transform equation looks like: d p [ s e t ( d o n e , n e w ) ] = m i n ( d p [ s e t ( d o n e , n e w ) ] , d p [ s e t ] [ l a s t ] + d i s [ l a s t ] [ n e w ] ) while she s e t function means set the n e w 's bit to be 1 and the final answer is min of d p [ a l l _ t h e _ w o r d _ p r i n t e d _ s t a t e ] [ l a s t ]