Gutenberg's algorithmic printing press - 1

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 ]


The answer is 88.

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.

1 solution

Ryandk St
Jun 25, 2014

classical bit mask dymanic programming, use d p [ 1 < < w ] [ w ] dp[1<<w][w] represt for the state ( d o n e , l w ) (done,lw) , 1 s 1's in d o n e s done's binary representation shows words has beed printed, and the last printed word was l w lw ,

we enum the next word n w nw which hasn't beed printed, and built a array like d i s [ i ] [ j ] dis[i][j] , represented for the number of operations to print word j 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 length(i)+length(j)-2*prefix ,

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 ] ) dp[set(done,new)]=min(dp[set(done,new)],dp[set][last]+dis[last][new]) while she s e t set function means set the n e w new 's bit to be 1 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 ] {dp[all\_the\_word\_printed\_state][last]}

but could you please explain it more simply . I am not able to understand this as i am a beginner

Dhruva Kothari - 6 years, 11 months ago

Log in to reply

Err...let's see an example similiar to the problem provided, there're 4 words: print, the, poem, aaaaa , we label them as 0, 1, 2, 3 , and an obivous 4 ! 4! brute search can solve the problem,but if the number increases, the brute search's time cost tremendously increased.

Let's read the problem again, luckily we found that among all the n ! n! possibilities, a lot of them were calculated repeatedly, for example, from the problem statement we can infer that the order the,poem,print,aaaaa must be no worser than poem, the, print, aaaaa and any other print order ends in word aaaaa , why?Because the order the,poem,print is the best among all permutation of there 3 3 words.

Now use the above idea to optimize our program:among all the combinations of the printed word permutation combines with the last printed word, we just need to record the best one, because the other combinations wiill never gain less operations then the best one.

Still remember we labeled the words?Yeah here they came, we use a positive integer to represent the printed state, for example, 0 0 means none has beed print, 1 1 means the word with label 0 printed, 2 2 for word labeled 2 printed, 6 6 means the words with label 1 , 2 1,2 have beed printed.Do you find the pattern?If the word with label i i has beed printed, then we set the i s i's bit in state to be 1 1 . Remember we need to record the state of the last printed word, otherwise we can't get the new state, this can be solved by simply add another variable.

Finally, the code includes 3 nested for loops, we enumerate the printed
words, then last printed word, the a new word hasn't beed printed, and it works……

ryandk st - 6 years, 11 months ago

Log in to reply

thank you so much

Dhruva Kothari - 6 years, 11 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...