Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8328973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:36:54+00:00 2026-06-09T01:36:54+00:00

Consider a dynamic programming problem that asks how many distinct subsequences (not necessarily contiguous)

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:36:56+00:00Added an answer on June 9, 2026 at 1:36 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the 0/1 knapsack problem . The standard Dynamic Programming algorithm applies only when
I have difficuities understanding why 0/1 knapsack using dynamic programming is not polynomial time
a) What do we consider as a dynamic sql statement? Any sql statement that
I have a fairly simple MVC view that allows for dynamic text input that
Hi Experts I have a special question About dynamic Linq to Sql. Consider we
Consider an article inside a div that has many <p> tag. Is there any
Consider the following problem: We have two sequences of cargo loads which can contain
Consider I have the following text in a UILabel (a long line of dynamic
Consider we have a dynamic library ( HelloWorld.dll ) which is compiled with Microsoft
I have this question about c# language's dynamic binding behavior. Consider the following object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.