Consider a dynamic programming problem that defines how many clear subsequences (not necessarily adjacent) of a sequence S have some property P of value p0.
The range of P is small and finite, and there is an efficient way to calculate P:
P(s1 + s2) = f(P(s1), P(s2))
where +denotes the concatenation of the sequence.
One way to do this is to calculate how many subsequences the prefix S[1] + S[2] + ... + S[k]of S has in the px property. (Save it in Count[px][k])
So recursion is this:
Count[px][k] = Count[px][k-1] // not using element S[k];
P pq = f(px,P(S[k])); // calculate property pq of appending element S[k]
Count[pq][k] += Count[px][k-1] // add count of P(prefix+S[k])
and the answer will be as follows:
return Count[p0][S.length]
This works when the elements of S are pairwise distinct, however he will count two subsequences that are of equal value but use different elements of different positions.
, ? (.. )