Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Programming a Count of Subsequences with Property?

Consider a dynamic programming problem that asks how many distinct subsequences (not necessarily contiguous) of a sequence S have a certain property P of value p0.

The range of P is small and finite, and there is an efficient way of calculating P:

P(s1 + s2) = f(P(s1), P(s2))

where + denotes sequence concatenation.

One way to do this would be to count how many subsequences there are of the prefix S[1] + S[2] + ... + S[k] of S that have property px. (Store this in Count[px][k])

So the recursion is:

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 is then:

return Count[p0][S.length]

This works when the elements of S are pairwise distinct, however it will count twice subsequences that have equal value but use different elements of different positions.

How can I modify this algorithm so that it counts equal subsequences exactly once ? (ie only counts distinct subsequences)

like image 209
Andrew Tomazos Avatar asked Aug 01 '12 13:08

Andrew Tomazos


People also ask

How do you count the number of subsequences?

Given a string, find the count of distinct subsequences of it. The problem of counting distinct subsequences is easy if all characters of input string are distinct. The count is equal to nC0 + nC1 + nC2 + … nCn = 2n.

How many subsequences does a string have?

@RossMillikan So the total number of subsequences in a string is 2n, where n is the length of the string.

How do you find the subsequences of length k?

Given an array arr[]and an integer K, the task is to find the sum of all K length subsequences from the given array. Explanation: There are 6 subsequences of length 2 which are {7, 8}, {7, 9}, {7, 2}, {8, 9}, {8, 2} and {9, 2}.

What is a distinct subsequence?

Distinct Subsequences. Given two strings s and t , return the number of distinct subsequences of s which equals t . A string's subsequence is a new string formed from the original string by deleting some (can be none) of the characters without disturbing the remaining characters' relative positions.


1 Answers

Suppose the sequence is of characters and S[k] is the letter x.

The problem is that you have double counted all sequences that don't use S[k], but nevertheless end with x (this can only happen if x happened earlier in the sequence).

Suppose the last time x appeared was at element S[j]. All the distinct sequences that end with x is simply given by counting all distinct sequences up to position j-1, and then adding x onto all of these.

We can therefore correct for the double counting by subtracting this count.

Count[px][k] = Count[px][k-1] // not using element S[k]
P pq = f(px,P(S[k])) // property pq of appending element S[k]
j = index of previous location in string where S[j]==S[k]
Count[pq][k] += Count[px][k-1] // using element S[k]
Count[pq][k] -= Count[px][j-1] // Removing double counts
like image 154
Peter de Rivaz Avatar answered Oct 02 '22 15:10

Peter de Rivaz