Problem Statement
Main Ideas
The Answer looks like a product of different items under a sqrt, therefore a basic tecnique is to use ln.
After this, calculation of product is changed to calculation of sum, which looks like:
Now the problem is to seek the maximum average value of gem sequences that match the model string.
When a problem of strings contains ONE model and MANY patterns, AC Automaton should come to one’s mind at once!
We build the AC Automaton for the model string, and do dynamic programming on it, thus the problem should be solved.
MIND that each node on the AC Automaton should inherit all the information of its ancestors on the fail-pointer tree.
When a problem that asks for minimize/maximize the average for a certain set of values, Linear Programming should come to one’s mind at once!
Assume that the average for the current set of model strings is $C$, it is obvious that we can do binary search to determine its value.
Then if there is a set of {w} that results in an average larger than $C$, the following expression should be true:
Now we can use the sign of the largest possibel to determine if the current is the answer.
The benefit of this algorithm is that we can delete one dimension of our DP on the AC Automaton, thus the Complexity is divided by
We assume represents this state: iteration on the first characters of the model string has been finished, and the pointer on AC Automaton is at node
If the next character of the model string is fixed, we simply update the value for ,
else we’ll have to update all values of
For more Details, refer to the code
1 | /*------------------------------ |