Editorial for COCI '13 Contest 4 #2 Gmo


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

It is possible to solve this task with dynamic programming, which we lack memory space for at first glance. We leave this option for the reader to implement for practice and continue to explain a simpler solution.

We use a for loop to choose the position where the swine gene would begin in the apple's DNA. Next, using an inner loop, we build this gene from the chosen beginning in a natural way: if the next character of the apple equals the next character of the swine gene we want to achieve, we move to the next character; else we have to insert the character we want to achieve. It never pays off to insert a character if we don't need to insert it. This is why this (greedy) algorithm is going to provide the best total cost for a chosen beginning. Finally, the solution is the minimum of costs for all possible beginnings.


Comments

There are no comments at the moment.