UVa 10860 - Many a Little makes a Mickle

10860 - Many a Little makes a Mickle

 * http://acm.uva.es/p/v108/10860.html

Summary
We can look at the space between the characters as a vertex, the characters are edges, and two vertices are connected if the string between them is located in given shorter strings (or the reverse). Then it's just standard Dynamic Programming or Shortest Path.

Explanation
You can create the explicit graph in $$O(n^2m)$$ (where $$n$$ is the length of the longer string, and $$m$$ is the length of the shorter string), or a careful construction will allow you to use the graph implicitly at no penalty. The problem is strictly $$O(n^2m)$$ as that is the amount of data we're given.

Input
2 aabbabbabbbb 3 a bb abb ewu**bbacsecsc 4 ewu bba cse csc

Output
Set 1: 5. Set 2: Not possible.