As a first attempt, I will probably try to use a greedy approach.
So, using your first example, we start with this:
-1*d*d*l*l*q -1*b*b*l*l*q 2*d*f*j*l*q 2*b*f*h*l*q -1*f*f*j*j*q ...
Now try to find the most repeating pattern in terms. This is q , which, fortunately, is present in all of them. Let me remove it and it leaves us with
-1*d*d*l*l -1*b*b*l*l 2*d*f*j*l 2*b*f*h*l -1*f*f*j*j ...
Now we do the same, this time we get l , and the problem is divided into two subtasks.
-1*d*d*l -1*b*b*l 2*d*f*j 2*b*f*h --------- -1*f*f*j*j ...
Repeat recursively until you repeat the repetition and check your steps back, you can recursively restore the simplified version of the expression:
q*(l*<first subproblem>+<second subproblem>)
As you can see, the solution will not necessarily be optimal, but it is easy to implement and can be quite good. If you need the best, you probably need to learn more combinations and rank them according to the number of copies you save, but the general concept is the same.
source share